Browse Source

Merge pull request #710 from 01-edu/rust-subject-review-quest-02

Rust subject review quest 02
content-update
augusto-mantilla 3 years ago committed by GitHub
parent
commit
2d6562aad6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      subjects/arrange_it/README.md
  2. 7
      subjects/borrow/README.md
  3. 6
      subjects/borrow_me_the_reference/README.md
  4. 13
      subjects/changes/README.md
  5. 45
      subjects/copy/README.md
  6. 16
      subjects/doubtful/README.md
  7. 5
      subjects/looping/README.md
  8. 12
      subjects/name_initials/README.md
  9. 12
      subjects/ownership/README.md
  10. 15
      subjects/string_literals/README.md
  11. 30
      subjects/tic_tac_toe/README.md

14
subjects/arrange_it/README.md

@ -2,8 +2,8 @@
### Instructions
Create a function called `arrange_phrase` that takes a string literal as a phrase and returns it organized
Each word will have a number that indicates the position of that word
Create a **function** called `arrange_phrase` that takes a string literal as a phrase and returns it organized
Each word will have a number that indicates the position of that word.
> This exercise will test the **heap allocation** of your function!
> So try your best to allocate the minimum data on the heap!
@ -11,22 +11,24 @@ Each word will have a number that indicates the position of that word
### Expected Function
```rust
fn arrange_phrase(phrase: &str) -> String {
pub fn arrange_phrase(phrase: &str) -> String {
}
```
### Notions
- https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html
- https://doc.rust-lang.org/std/primitive.str.html#method.split
- [stack and heap](https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html)
- [str](https://doc.rust-lang.org/std/primitive.str.html)
### Usage
Here is a program to test your function
```rust
use arrange_it::*;
fn main() {
println!("{:?}", initials("is2 Thi1s T4est 3a"));
println!("{:?}", arrange_phrase("is2 Thi1s T4est 3a"));
}
```

7
subjects/borrow/README.md

@ -2,14 +2,17 @@
### Instructions
Complete the signature and the body of the `str_len` that receives a string or a string literal and returns its length without taking ownership of the value (i.e, borrowing the value)
Complete the signature and the body of the `str_len` **function** that receives a string or a string literal and returns its length without taking ownership of the value (i.e, borrowing the value).
### Expected Function
### Expected Function (The signature needs to be completed)
```rust
pub fn str_len(s: ) -> usize {
}
```
### Notions
[Primitive Type str](https://doc.rust-lang.org/std/primitive.str.html)
### Usage

6
subjects/borrow_me_the_reference/README.md

@ -7,7 +7,11 @@ needing garbage collector. Therefore you must understand ownership in rust.
Create the following functions :
- `delete_and_backspace`, imagine that `-` represents the backspace key and the `+` represents the delete key, this function must receive a borrowed string and turn this string into the string without the backspaces and the deletes.
- `delete_and_backspace`, imagine that the `-` represents the `backspace key` and the `+` represents the `delete key`, this function must receive a borrowed string and turn this string into the string that applies the `backspaces` and the `deletes`.
- For example:
- "helll-o" turns into "hello"
- "he+lllo" turns into "hello"
- `is_correct` that borrows a Vector of string literals with some correct and incorrect math equations and replaces the correct equations with `✔` and the wrong with `✘` and returns a `usize` with the percentage of correct equations.

13
subjects/changes/README.md

@ -2,11 +2,16 @@
### Instructions
Imagine you are working in a software to control smart lights in a house. You have access to an array of all the lights in a house.
Imagine you are working on a software to control smart lights in a house. You have access to an array of all the lights in a house.
Define the associated function `new` to the data structure `Light` which creates a new light with the alias passed in the arguments and a brightness of 0.
Define the associated **function** `new` to the data structure `Light` which creates a new light with the alias passed in the arguments and a brightness of 0.
Define the function `change_brightness` that receives a Vec of lights, an alias and a u8 value and sets the u8 value as the new brightness of the light identified by the alias in the Vec of lights.
Define the **function** `change_brightness` that receives a `Vec` of lights, an `alias` and a `u8`value. The **function** then sets the `u8` value as the new brightness of the light identified by the alias in the Vec of lights.
### Notions
[Example of Structs](https://doc.rust-lang.org/book/ch05-02-example-structs.html)
[Keyword Self](https://doc.rust-lang.org/std/keyword.Self.html)
### Expected Functions and Structure
@ -31,6 +36,8 @@ pub fn change_brightness(lights: &mut Vec<Light>, alias: &str, value: u8) {
Here is an incomplete program to test your function
```rust
use changes::*;
fn main() {
// bedroom
let mut lights = vec![

45
subjects/copy/README.md

@ -2,17 +2,25 @@
### Instructions
Your objective is to fix the program so that all functions work.
Your objective is to fix the **functions** work so that the program works.
- `nbr_function`, returns a tuple with the original value, the exponential and logarithm of that value
- `str_function`, returns a tuple with the original value and the exponential of that value as a string
- `vec_function`, returns a tuple with the original value and the logarithm of each value
- `nbr_function` returns a tuple:
- with the `original` value
- the `exponential function` of the value
- and the `natural logarithm` of this `absolute` value
- `str_function` returns a tuple:
- with the `original` value
- and the `exponential function` each value as a string (see the example)
- `vec_function` returns a tuple:
- with the `original` value
- and the `natural logarithm` of each `absolute` value
The objective is to now how ownership works with different types.
The objective is to know how ownership works with different types.
### Notions
- https://doc.rust-lang.org/rust-by-example/scope/move.html
- [scope](https://doc.rust-lang.org/rust-by-example/scope/move.html)
- [Primitive Type f64](https://doc.rust-lang.org/std/primitive.f64.html)
### Expected Functions
@ -32,18 +40,21 @@ pub fn vec_function(b: Vec<u32>) -> (Vec<u32>, Vec<f64>) {
Here is a possible program to test your function :
```rust
use copy::*;
fn main() {
let a = String::from("1 2 4 5 6");
let b = vec![1, 2, 4, 5];
let c: u32 = 0;
println!("{:?}", nbr_function(c));
// output: (12, 162754.79141900392, 2.4849066497880004)
println!("{:?}", vec_function(b));
// output: ([1, 2, 4], [0.0, 0.6931471805599453, 1.3862943611198906])
println!("{:?}", str_function(a));
// output: ("1 2 4", "2.718281828459045 7.38905609893065 54.598150033144236")
let a: u32 = 0;
let b = String::from("1 2 4 5 6");
let c = vec![1, 2, 4, 5];
println!("{:?}", nbr_function(a));
// output : (0, 1.0, inf)
println!("{:?}", str_function(b));
// output : ("1 2 4 5 6", "2.718281828459045 7.38905609893065 54.598150033144236 148.4131591025766 403.4287934927351")
println!("{:?}", vec_function(c));
// output : ([1, 2, 4, 5], [0.0, 0.6931471805599453, 1.3862943611198906, 1.6094379124341003])
}
```
And its output:
@ -51,7 +62,7 @@ And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run
(0, 1.0, inf)
([1, 2, 4, 5], [0.0, 0.6931471805599453, 1.3862943611198906, 1.6094379124341003])
("1 2 4 5 6", "2.718281828459045 7.38905609893065 54.598150033144236 148.4131591025766 403.4287934927351")
([1, 2, 4, 5], [0.0, 0.6931471805599453, 1.3862943611198906, 1.6094379124341003])
student@ubuntu:~/[[ROOT]]/test$
```

16
subjects/doubtful/README.md

@ -2,14 +2,20 @@
### Instructions
Write a function called `doubtful` that adds to every string passed to it a question mark (?)
Write a function called `doubtful` that adds to every string passed to it a question mark (?).
You have to fix the code to make it compile an for that you can only modify the code where is indicated
You have to complete the signature the functions to make it fit the usage and you also have to modify the usage to be able to do what is expected.
- Restrictions:
- `doubtful` cannot return any value.
- And in the example of the usage you can only modify the argument of the function
### Expected
```rust
pub fn doubtful(s: &mut String) {
pub fn doubtful(s: /*give the correct type*/ ) {
}
```
@ -22,7 +28,9 @@ fn main() {
let mut s = String::from("Hello");
println!("Before changing the string: {}", s);
doubtful(&mut s);
doubtful(/*add your code here*/);
println!("After changing the string: {}", s);
}
```

5
subjects/looping/README.md

@ -12,6 +12,11 @@ Riddle: I am the beginning of the end, and the end of time and space. I am essen
Answer: The letter e
### Notions
- [Moduler std::io](https://doc.rust-lang.org/std/io/index.html)
- [loop](https://doc.rust-lang.org/std/keyword.loop.html)
### Usage
```console

12
subjects/name_initials/README.md

@ -2,7 +2,7 @@
### Instructions
Create a function called `initials`, this function will receive a vector of string literals
Create a **function** called `initials`, this function will receive a vector of string literals
with names and return a vector of Strings with the initials of each name.
> This exercise will test the **heap allocation** of your function!
@ -10,25 +10,25 @@ with names and return a vector of Strings with the initials of each name.
### Notions
- https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html
- [stack and heap](https://doc.rust-lang.org/1.22.0/book/first-edition/the-stack-and-the-heap.html)
### Expected Function
```rust
fn initials(names: &mut Vec<&str>) -> Vec<String> {
pub fn initials(names: &mut Vec<&str>) -> Vec<String> {
}
```
### Usage
Here is a program to test your function
Here is a program to test your function:
```rust
use name_initials::initials;
fn main() {
let names = vec!["Harry Potter", "Someone Else", "J. L.", "Barack Obama"]
println!("{:?}", initials(names));
let mut names = vec!["Harry Potter", "Someone Else", "J. L.", "Barack Obama"]
println!("{:?}", initials(&mut names));
}
```

12
subjects/ownership/README.md

@ -2,10 +2,14 @@
### Instruction
- Create a function that takes ownership of a string and returns the first sub-word in it
- Create a **function** that takes ownership of a string and returns the first sub-word in it
- It should work for `camelCase` as well as `snake_case`
### Notions
- [ownership](https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html)
### Expected Function
```rust
@ -15,9 +19,11 @@ pub fn first_subword(mut s: String) -> String {
### Usage
Here is a program to test your function
Here is a program to test your function:
```rust
use ownership::first_subword;
fn main() {
let s1 = String::from("helloWorld");
let s2 = String::from("snake_case");
@ -31,7 +37,7 @@ fn main() {
}
```
And its output
And its output:
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run

15
subjects/string_literal/README.md → subjects/string_literals/README.md

@ -42,12 +42,25 @@ fn find(v: &str, pat: char) -> usize {
Here is a program to test your function
```rust
use string_literal::*;
fn main() {
println!("{}", is_empty(""));
println!("{}", is_ascii("rust"));
println!("{}", contains("rust", "ru"));
println!("{:?}", split_at("rust", 2));
println!("{}", find("rust", 'u'));
}
```
And its output
```console
student@ubuntu:~/[[ROOT]]/test$ cargo run
true
true
true
("ru", "st")
1
student@ubuntu:~/[[ROOT]]/test$
```

30
subjects/tic_tac_toe/README.md

@ -2,32 +2,40 @@
### Instructions
You must create a tic tac toe checker.
You must create a `tic tac toe` checker.
Create the following functions:
- `tic_tac_toe` that receives a table of vectors (Vec<Vec<&str>>) and returns a string : `player O won` or `player X won` or `Tie`
- `diagonals` that will receive a player and a table. It should return a boolean, this must return true if all the diagonals are completed by the player
- `horizontal` that will receive a player and a table. It should return a boolean, this must return true if one of the horizontal lines are completed by the player
- `vertical` that will receive a player and a table. It should return a boolean, this must return true if one of the vertical lines are completed by the player
- `tic_tac_toe` which receives:
- a table of vectors (Vec<Vec<&str>>).
- It should return a String `player O won` or `player X won` or `Tie`.
- `diagonals` which will receive:
- a player and a table.
- It should return a boolean, this must return `true` if all the diagonals are completed by the player.
- `horizontal` which will receive:
- a player and a table.
- It should return a boolean, this must return `true` if one of the horizontal lines are completed by the player.
- `vertical` which will receive:
- a player and a table.
- It should return a boolean, this must return `true` if one of the vertical lines are completed by the player.
### Notions
- https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
- [references and borrowing](https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html)
### Expected Functions
```rust
fn tic_tac_toe(table: Vec<Vec<&str>>) -> String {
pub fn tic_tac_toe(table: Vec<Vec<&str>>) -> String {
}
fn diagonals(player: &str, table: &Vec<Vec<&str>>) -> bool {
pub fn diagonals(player: &str, table: &Vec<Vec<&str>>) -> bool {
}
fn horizontal(player: &str, table: &Vec<Vec<&str>>) -> bool {
pub fn horizontal(player: &str, table: &Vec<Vec<&str>>) -> bool {
}
fn vertical(player: &str, table: &Vec<Vec<&str>>) -> bool {
pub fn vertical(player: &str, table: &Vec<Vec<&str>>) -> bool {
}
```
@ -36,6 +44,8 @@ fn vertical(player: &str, table: &Vec<Vec<&str>>) -> bool {
Here is a program to test your function
```rust
use tic_tac_toe::*;
fn main() {
println!(
"{:?}",

Loading…
Cancel
Save