Browse Source

Merge pull request #758 from 01-edu/rust-subject-review-quest-08

first pass on quest 08
content-update
augusto-mantilla 3 years ago committed by GitHub
parent
commit
8a8be6fcad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      subjects/adding/README.md
  2. 16
      subjects/adding_twice/README.md
  3. 12
      subjects/closures/README.md
  4. 13
      subjects/get_products/README.md
  5. 19
      subjects/highest/README.md
  6. 17
      subjects/iterators/README.md
  7. 18
      subjects/project_motion/README.md
  8. 36
      subjects/sales/README.md
  9. 5
      subjects/slices_to_map/README.md
  10. 12
      subjects/step_iterator/README.md

6
subjects/adding/README.md

@ -10,20 +10,20 @@ The purpose is to curry the add method to create more variations.
Here is a program to test your function.
```rust
use adding::adding;
use adding::add_curry;
fn main() {
let add10 = add_curry(-10);
let add20 = add_curry(2066);
let add30 = add_curry(300000);
println!("{}", add10(5));
println!("{}", add20(195));
println!("{}", add30(5696));
}
```
And its output
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run

16
subjects/adding_twice/README.md

@ -2,21 +2,23 @@
### Instructions
In this exercise you will have to reuse your `add_curry` function
Then you have to complete the function `twice` using closures, this function will
take a function f(x) as parameter and return a function f(f(x))
In this exercise you will have to reuse your `add_curry` function (copy and paste it directly in your lib.rs file).
Then you have to create the function `twice` using closures, this function will
take a function f(x) as parameter and return a function f(f(x)).
So, the purpose of this function is to add two times the value in `add_curry` to the original value.
### Notions
- https://doc.rust-lang.org/rust-by-example/fn/hof.html#higher-order-functions
- [higher order function](https://doc.rust-lang.org/rust-by-example/fn/hof.html#higher-order-functions)
### Expected functions
The type of the arguments are missing use the example `main` function to determine the correct type.
The type of the arguments are missing. Use the example `main` function to determine the correct type.
```rust
pub fn twice<T>(F: _) -> _{}
pub fn twice<T>(F: _) -> _{
}
```
### Usage
@ -24,7 +26,7 @@ pub fn twice<T>(F: _) -> _{}
Here is a program to test your function.
```rust
use adding_twice::adding_twice;
use adding_twice::*;
fn main() {
let add10 = add_curry(10);

12
subjects/closures/README.md

@ -2,12 +2,18 @@
### Instructions
Using closures and iterators create a function, `first_fifty_even_square` that returns the first 50
Using closures and iterators create a **function**, `first_fifty_even_square` that returns the first 50 even numbers squared.
in a `Vec<i32>`.
### Notions
[Iterators and Closures](https://doc.rust-lang.org/book/ch13-00-functional-features.html)
### Expected Functions
```rust
fn first_fifty_even_square() -> Vec<i32> {
}
```
@ -16,6 +22,8 @@ fn first_fifty_even_square() -> Vec<i32> {
Here is a program to test your function.
```rust
use closures::*;
fn main() {
println!("Hello, world!");
let v1 = first_fifty_even_square();
@ -24,7 +32,7 @@ fn main() {
}
```
And its output
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run

13
subjects/get_products/README.md

@ -6,14 +6,21 @@ Create a function `get_products` that takes a vector of integers, and returns a
of each index. For this exercise to be correct you will have to return the product of every index
except the current one.
Examples: [1,2,3,4]
- for the number `1` we get `2*3*4 = 24`
- for the number `3` we get `1*2*4 = 8`
### Notions
- https://doc.rust-lang.org/std/iter/trait.Iterator.html
- [Trait iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html)
### Expected functions
```rust
pub fn get_products(arr: Vec<usize>) -> Vec<usize> {}
pub fn get_products(arr: Vec<usize>) -> Vec<usize> {
}
```
### Usage
@ -30,7 +37,7 @@ fn main() {
}
```
And its output
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run

19
subjects/highest/README.md

@ -2,22 +2,25 @@
### Instructions
In this exercise you will be given a `Numbers` struct.
In this exercise a `Numbers` struct will be given.
Your task is to write these methods:
These methods have to be written:
- `List` that returns an `array` with every number in the struct
- `Latest` that returns an `Option<u32>` with the last added number
- `Highest` that return an `Option<u32>` with the highest number from the list,
- `new` create a new instance of Number.
- `List` that returns an `array` with every number in the struct.
- `Latest` that returns an `Option<u32>` with the last added number.
- `Highest` that return an `Option<u32>` with the highest number from the list.
- `Highest_Three` that returns a `Vec<u32>` with the three highest numbers.
### Notions
- https://doc.rust-lang.org/std/iter/trait.Iterator.html
- [Trait iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html)
### Expected functions
```rust
pub fn new(&[u32]) -> Self {}
pub fn List(&self) -> &[u32] {}
pub fn Latest(&self) -> Option<u32> {}
@ -32,7 +35,7 @@ pub fn Highest_Three(&self) -> Vec<u32> {}
Here is a program to test your function.
```rust
use highest::highest;
use highest::*;
#[derive(Debug)]
struct Numbers<'a> {
@ -49,7 +52,7 @@ fn main() {
}
```
And its output
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run

17
subjects/iterators/README.md

@ -2,26 +2,27 @@
### Instructions
Create a method `new` that takes one number `usize` and initializes the `Number` struct.
Create a method `new` that takes one number `usize` and initializes the struct `Number`.
This method will have to determinate if the given number is even or odd, if it is even you will have to increment one to the odd number and
if it is odd you have to increment one to the even number.
This method will have to determinate if the given number is even or odd. If it is even you will have to increment it by one to the next odd number and if it is odd you have to increment by one to the next even number.
After that you will implement an iterator for the struct `Number` that returns a tuple (usize,usize,usize) containing each field of the struct Number.
The first position of the tuple will be the even number, the second will be the odd number, and the third will be the factorial number.
So the purpose is to return the given number in the right position, if it is even it will be at the first position, and if it is odd it will be in the second position. Apart from that you have to return the factorial of the given number in the third position.
So the purpose is to return the given number in the right position. If it is even it will be at the first position, and if it is odd it will be in the second position. Apart from that you have to return the factorial of the given number in the third position.
### Notions
- https://doc.rust-lang.org/std/iter/trait.Iterator.html
- [Trait Iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html)
### Expected functions
```rust
impl Number {
pub fn new(nbr: usize) -> Number {}
pub fn new(nbr: usize) -> Number {
}
}
impl Iterator for Number {
@ -34,7 +35,7 @@ impl Iterator for Number {
Here is a program to test your function.
```rust
use iterators::iterators;
use iterators::*;
struct Number {
even: usize,
@ -55,7 +56,7 @@ fn main() {
}
```
And its output
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run

18
subjects/project_motion/README.md

@ -4,14 +4,19 @@
For this exercise you will have to create a [projectile motion](https://cimg2.ck12.org/datastreams/f-d%3Abb024be6673110b31e78b46819e792adaed8dc661e082a61f0a6d64e%2BIMAGE%2BIMAGE.1).
You will be provided with a structure called `Object` that will have all variables that are
A structure called `Object` will be provided which will have all variables that are
essential for the projectile physics. (distance, velocity, height, time)
You must implement :
- A function `throw_object` that will initialize the Object with a given velocity and height.
- The trait Iterator with the `.next()` in which it must calculate the next position of the object after 1 second.
It will return an `Option` with the Object, It will return `None` if the object already reached the floor.
- The trait Iterator with the `.next()` in which,the next position of the object after 1 second, must be calculated.
It will return an `Option` with the Object or it will return `None` if the object already reached the floor.
### Notions
- [trait Iterator](https://doc.rust-lang.org/std/iter/trait.Iterator.html)
- [iter](https://doc.rust-lang.org/rust-by-example/trait/iter.html)
### Expected Function
@ -40,6 +45,8 @@ impl Iterator for Object {
Here is a program to test your function
```rust
use project_motion::*;
fn main() {
let mut obj = Object::throw_object(50.0, 150.0);
println!("{:?}", obj.next());
@ -63,8 +70,3 @@ None
None
student@ubuntu:~/[[ROOT]]/test$
```
### Notions
- https://doc.rust-lang.org/std/iter/trait.Iterator.html
- https://doc.rust-lang.org/rust-by-example/trait/iter.html

36
subjects/sales/README.md

@ -2,13 +2,17 @@
### Instructions
You will have to create a shopping system, where you will have a :
In this exercise a shopping system will have to be created. There will be :
- Store that will save all the products in it
- Cart that will have `items`, that the client will buy, and a `receipt`
- A store that will save all the products in it
- A cart that will have `items`, that the client will buy, and a `receipt`
This store is having a promotion, "Buy three and get one for free" (the free item must be the cheapest). The receipt must not present
any value as 0, so you will have to apply the promotion to all items instead.
any value as 0, so the promotion must be a reduction to be applied to all items instead.(see the example)
### Notions
- [closures](https://doc.rust-lang.org/rust-by-example/fn/closures.html)
### Expected Function
@ -39,34 +43,36 @@ impl Cart {
### Example
`[1.23, 3.12, 23.1]` -> receipt will be `[1.17, 2.98, 22.07]`
`[1.23, 3.12, 23.1]` -> the receipt will be `[1.17, 2.98, 22.07]`
So `1.17 + 2.98 + 22.07 == 3.12 + 23.1 + 0`
This is a percentage calculation, and it can be applied to a set of three items.
If the client purchase 9 items it will be applied the promotion, three for free, to all items
If the client purchase 9 items, the promotion will be applied, three for free, to all items
|--------------| |---------------| |---------------|
`[1.23, 23.1, 3.12, 9.75, 1.75, 23.75, 2.75, 1.64, 15.23]` -> receipt will be `[1.16, 1.55, 1.65, 2.6, 2.94, 9.2, 14.38, 21.8, 22.42]`
|--------------| |---------------| |---------------|
`[1.23, 23.1, 3.12, 9.75, 1.75, 23.75, 2.75, 1.64, 15.23]` -> the receipt will be `[1.16, 1.55, 1.65, 2.6, 2.94, 9.2, 14.38, 21.8, 22.42]`
|--------| |--------| |--------|
`[3.12, 9.75, 1.75, 23.75, 2.75, 1.64, 15.23]` -> receipt will be `[1.54, 1.65, 2.59, 2.94, 9.18, 14.34, 22.36]`
|--------| |--------| |--------|
`[3.12, 9.75, 1.75, 23.75, 2.75, 1.64, 15.23]` -> the receipt will be `[1.54, 1.65, 2.59, 2.94, 9.18, 14.34, 22.36]`
and so on... (hint: Closures is the way)
You will have to implement for the Cart structure the following function:
You will have to implement for the Cart structure the following **functions**:
- `new`, that will initialize the cart
- `insert_item`, that will receive a reference to `Store` and a `String`. Just like the name says you will
have to insert the item to the cart
- `generate_receipt`, that returns a vector of sorted floats. This function must generate the receipt just
like the example above, using the promotion. Also saving the result in the filed `receipt`.
like the example above, using the promotion. Also saving the result in the filed `receipt`.
### Usage
Here is a program to test your function
Here is a program to test your function,
```rust
use sales::*;
fn main() {
let store = Store::new(vec![
(String::from("product A"), 1.23),
@ -95,7 +101,3 @@ Store { products: [("product A", 1.23), ("product B", 23.1), ("product C", 3.12)
Cart { items: [("product A", 1.23), ("product B", 23.1), ("product C", 3.12)], receipt: [1.17, 2.98, 22.07] }
student@ubuntu:~/[[ROOT]]/test$
```
### Notions
- https://doc.rust-lang.org/rust-by-example/fn/closures.html

5
subjects/slices_to_map/README.md

@ -8,6 +8,7 @@ Create a function that borrows two slices and returns a hashmap where the first
```rust
pub fn slices_to_map(&[T], &[U]) -> HashMap<&T, &U> {
}
```
@ -16,6 +17,8 @@ pub fn slices_to_map(&[T], &[U]) -> HashMap<&T, &U> {
Here is a program to test your function.
```rust
use slices_to_map::*;
fn main() {
let keys = ["Olivia", "Liam", "Emma", "Noah", "James"];
let values = [1, 3, 23, 5, 2];
@ -27,6 +30,6 @@ And its output
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run
{"Liam": 3, "James": 2, "Emma": 23, "Noah": 5, "Olivia": 1}
{"James": 2, "Liam": 3, "Emma": 23, "Noah": 5, "Olivia": 1}
student@ubuntu:~/[[ROOT]]/test$
```

12
subjects/step_iterator/README.md

@ -2,13 +2,13 @@
### Instructions
- Create an Iterator (by implementing the `std::iter::Iterator` trait) that iterates through the values from `beg` to `end` (including end) in the indicated `steps`.
- Create an `Iterator` (by implementing the `std::iter::Iterator` trait) that iterates through the values from `beg` to `end` (including end) in the indicated `steps`.
- The name of you're iterator will be `StepIterator` and it must be generic so you can use any integer value: i8,..,i64, u8,..,u64 or floating point number f32,..,f64
- The name of your iterator will be `StepIterator` and it must be generic so you can use any integer value: i8,..,i64, u8,..,u64 or floating point number f32,..,f64
- If the steps don't allow to arrive until the end of the sequence only the last value inferior to the end of the series will be returned (See Usage)
- If the steps do not allow to attain the end of the sequence, only the last value inferior to the end of the series will be returned (See Usage)
- Define the associated function: `new` that creates a new Step iterator:
- Define the associated function: `new` which creates a new Step iterator:
### Expected Functions and Structures
@ -32,6 +32,8 @@ impl std::iter::Iterator for StepIterator<T> {
Here is a program to test your function.
```rust
use step_iterator::*;
fn main() {
for v in StepIterator::new(0, 100, 10) {
print!("{},", v);
@ -45,7 +47,7 @@ fn main() {
}
```
And its output
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run

Loading…
Cancel
Save