From b5b6ecc51ea2cde653d10769d8826a9a8cc7f81f Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Mon, 16 May 2022 16:26:29 +0100 Subject: [PATCH 1/5] roman numbers iter update --- subjects/roman_numbers_iter/README.md | 51 +++++---------------------- 1 file changed, 8 insertions(+), 43 deletions(-) diff --git a/subjects/roman_numbers_iter/README.md b/subjects/roman_numbers_iter/README.md index 4277708a1..8aa4e4702 100644 --- a/subjects/roman_numbers_iter/README.md +++ b/subjects/roman_numbers_iter/README.md @@ -2,50 +2,19 @@ ### Instructions -Implement the `IntoIterator` trait for the `RomanNumber` type to enable using a for loop notation. This implementation must allow taking ownership, borrowing and borrowing mutably. +Implement the `Iterator` trait for the `RomanNumber` type. You should use the code from the previous exercise roman_numbers. -1. Taking ownership (this consumes the RomanNumber) - -```rust -for digit in number { - ... -} -``` - -2. Borrowing immutably (this preserves the RomanNumber) - -```rust - for digit in &number { - - } -``` - -3. Borrowing mutably (this allow you to modify the RomanNumber without having to return the ownership) - -```rust - for digit in &mut number { - - } -``` ### Notions -- https://doc.rust-lang.org/std/iter/trait.IntoIterator.html -- https://doc.rust-lang.org/std/iter/index.html +- [Trait Iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html) ### Expected Functions ```rust -use roman_numbers::{RomanDigit, RomanNumber}; +//... -impl IntoIterator for &RomanNumber { -} - -impl IntoIterator for &mut RomanNumber { -} - -impl IntoIterator for RomanNumber { -} +impl Iterator for RomanNumber {} ``` ### Usage @@ -56,12 +25,10 @@ Here is a program to test your function. use roman_numbers::RomanNumber; fn main() { - let number = RomanNumber::from(15); + let mut number = RomanNumber::from(15); - for digit in &number { - println!("{:?}", digit); - } println!("{:?}", number); + println!("{:?}", number.next()); } ``` @@ -69,9 +36,7 @@ And its output ```console $ cargo run -RomanNumber([X, X, X, I, I]) -RomanNumber([I, X]) -RomanNumber([X, L, V]) -RomanNumber([Nulla]) +RomanNumber([X, V]) +Some(RomanNumber([X, V, I])) $ ``` From 94419a83f3babc09c0df2fee79faa785d8e9fa76 Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Tue, 17 May 2022 09:46:18 +0100 Subject: [PATCH 2/5] update rpn --- subjects/rpn/README.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/subjects/rpn/README.md b/subjects/rpn/README.md index 9e3a5f6a8..92c7b25ff 100644 --- a/subjects/rpn/README.md +++ b/subjects/rpn/README.md @@ -2,17 +2,16 @@ ### Instructions -Write a **program** which takes a `string` which contains an equation written in `Reverse Polish Notation` (RPN) as its first argument, -which evaluates the equation, and which prints the result on the standard output followed by a newline (`'\n'`). +Write a **program** which takes a `string` that contains an equation written in `Reverse Polish Notation` (RPN) as an argument. The **program** must evaluate the equation, and then: -`Reverse Polish Notation` is a mathematical notation in which every operator follows all of its operands. In RPN, -every operator encountered evaluates the previous 2 operands, and the result of this operation then becomes the first of -the two operands for the subsequent operator. Operands and operators must be spaced by at least one space. +- If the `string` is not valid or if there is not exactly one argument, `Error` must be printed on the standard output followed by a newline. +- If the `string` has extra spaces it is still considered valid. + + +`Reverse Polish Notation` is a mathematical notation in which every operator follows all of its operands. In RPN, every operator encountered evaluates the previous 2 operands, and the result of this operation then becomes the first of the two operands for the subsequent operator. Operands and operators must be spaced by at least one space. The following operators must be implemented : `+`, `-`, `*`, `/`, and `%`. -If the `string` is not valid or if there is not exactly one argument, `Error` must be printed on the standard output followed by a newline. -If the `string` has extra spaces it is still considered valid. All the given operands must fit in a `i64`. From 1fa89efe132d6985dce892665734658bca237748 Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Mon, 23 May 2022 09:51:48 +0100 Subject: [PATCH 3/5] update format --- subjects/rpn/README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/subjects/rpn/README.md b/subjects/rpn/README.md index 92c7b25ff..dc1612055 100644 --- a/subjects/rpn/README.md +++ b/subjects/rpn/README.md @@ -7,12 +7,10 @@ Write a **program** which takes a `string` that contains an equation written in - If the `string` is not valid or if there is not exactly one argument, `Error` must be printed on the standard output followed by a newline. - If the `string` has extra spaces it is still considered valid. - `Reverse Polish Notation` is a mathematical notation in which every operator follows all of its operands. In RPN, every operator encountered evaluates the previous 2 operands, and the result of this operation then becomes the first of the two operands for the subsequent operator. Operands and operators must be spaced by at least one space. The following operators must be implemented : `+`, `-`, `*`, `/`, and `%`. - All the given operands must fit in a `i64`. Examples of formulas converted in RPN: From 0d19091ef505ea2feedb3942bf0cc34d003c94dc Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Mon, 23 May 2022 09:57:44 +0100 Subject: [PATCH 4/5] update format --- subjects/rpn/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/subjects/rpn/README.md b/subjects/rpn/README.md index dc1612055..213dd90b9 100644 --- a/subjects/rpn/README.md +++ b/subjects/rpn/README.md @@ -52,7 +52,7 @@ For receiving arguments from the command line you should use something like: ### Usage -````console +```console $ cargo run "1 2 * 3 * 4 +" 10 $ cargo run "1 2 3 4 +" @@ -64,4 +64,3 @@ $ cargo run " 1 3 * 2 -" $ cargo run " 1 3 * ksd 2 -" Error ``` -```` From abaab7a3b553b168139aef2da9b30ce92488983e Mon Sep 17 00:00:00 2001 From: davidrobert99 Date: Mon, 23 May 2022 10:00:34 +0100 Subject: [PATCH 5/5] update roman_numbers_iter --- subjects/roman_numbers_iter/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/subjects/roman_numbers_iter/README.md b/subjects/roman_numbers_iter/README.md index 8aa4e4702..5596bf29d 100644 --- a/subjects/roman_numbers_iter/README.md +++ b/subjects/roman_numbers_iter/README.md @@ -4,7 +4,6 @@ Implement the `Iterator` trait for the `RomanNumber` type. You should use the code from the previous exercise roman_numbers. - ### Notions - [Trait Iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html)