Skip to content

Commit

Permalink
fix(rust-quest02): applied proposed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodesu authored Dec 19, 2024
1 parent 0c8eb48 commit fd49608
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 77 deletions.
4 changes: 3 additions & 1 deletion subjects/armstrong_number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ pub fn is_armstrong_number(nb: u32) -> Option<u32> {

### Usage

Here is a possible program to test your function,
Here is a possible program to test your function:

```rust
use armstrong_number::*;

fn main() {
println!("{:?}", is_armstrong_number(0));
println!("{:?}", is_armstrong_number(1));
Expand Down
7 changes: 3 additions & 4 deletions subjects/arrange_it/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
Create a **function** named `arrange_phrase`, that takes a string literal, _sorts_ the words and returns it. Each word will contain a number that indicates the position of that word.

### Expected Functions

```rust
pub fn arrange_phrase(phrase: &str) -> String {
}
```

> Your heap allocations will be monitored to ensure that you do not make too many allocations, and that your allocations are reasonably sized.


### Usage

Here is a program to test your function
Expand All @@ -22,15 +21,15 @@ Here is a program to test your function
use arrange_it::*;

fn main() {
println!("{:?}", arrange_phrase("is2 Thi1s T4est 3a"));
println!("{}", arrange_phrase("is2 Thi1s T4est 3a"));
}
```

And its output

```console
$ cargo run
"This is a Test"
This is a Test
$
```

Expand Down
6 changes: 5 additions & 1 deletion subjects/borrow/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

### Instructions

Create a **function** named `str_len`, you'll need to complete the function signature. Your function should accept a string or a string literal, and return its length without taking ownership of the value (i.e, borrowing the value).
Create a **function** named `str_len`, you'll need to complete the function signature. Your function should accept a borrowed string, and return its length (in characters).

### Expected functions

```rust
pub fn str_len(s: ) -> usize {
}
Expand All @@ -20,9 +21,11 @@ use borrow::*;
fn main() {
let s = "hello";
let s1 = "camelCase".to_string();
let s2 = "olá!";

println!("\tstr_len(\"{}\") = {}", s, str_len(s));
println!("\tstr_len(\"{}\") = {}", s1, str_len(&s1));
println!("\tstr_len(\"{}\") = {}", s2, str_len(s2));
}
```

Expand All @@ -32,6 +35,7 @@ And its output:
$ cargo run
str_len("hello") = 5
str_len("camelCase") = 9
str_len("olá!") = 4
$
```

Expand Down
28 changes: 14 additions & 14 deletions subjects/borrow_me_the_reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

### Instructions

> Ownership is Rust's most unique feature. It enables Rust to make memory safety guarantees without needing a garbage collector.
> Ownership is arguably Rust's most unique feature. It enables Rust to make memory safety guarantees without needing a garbage collector.
Understanding ownership is essential to take full advantage of Rust capabilities, it influences almost all aspects of the language.
Understanding ownership is essential to take full advantage of Rust's capabilities, as it influences almost all aspects of the language.

Create the following functions:

- `delete_and_backspace`: which receives a borrowed string, and processes it. `-` represents the backspace key and `+` represents the delete key, so that `"helll-o"` and `"he+lllo"` are both converted to `"hello"`. The `-` and `+` characters should be removed from the string.

- `do_operations`: which borrows a Vector of string literals representing simple addition and subtraction equations. The function should replace the operation with the result.
- `do_operations`: which borrows a vector of string literals representing simple addition and subtraction equations. The function should replace the operation with the result.

### Expected Functions

```rust
pub fn delete_and_backspace(s: &mut String) {
}

pub fn do_operations(v: &mut Vec<String>) {
pub fn do_operations(v: &mut [String]) {
}
```

Expand All @@ -27,21 +27,21 @@ pub fn do_operations(v: &mut Vec<String>) {
Here is a program to test your function

```rust
use borrow_me_the_reference::{delete_and_backspace, do_operations};
use borrow_me_the_reference::*;

fn main() {
let mut a = String::from("bpp--o+er+++sskroi-++lcw");
let mut b: Vec<String> = vec![
"2+2".to_string(),
"3+2".to_string(),
"10-3".to_string(),
"5+5".to_string(),
let mut a = "bpp--o+er+++sskroi-++lcw".to_owned();
let mut b = [
"2+2".to_owned(),
"3+2".to_owned(),
"10-3".to_owned(),
"5+5".to_owned(),
];

delete_and_backspace(&mut a);
do_operations(&mut b);
delete_and_backspace(&mut a);
do_operations(&mut b);

println!("{:?}", (a, b));
println!("{:?}", (a, b));
}
```

Expand Down
17 changes: 9 additions & 8 deletions subjects/copy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Create the following **functions**. The objective is to know how ownership works
- and the `natural logarithm` of each `absolute` value.

### Expected functions

```rust
pub fn nbr_function(c: i32) -> (i32, f64, f64) {
}
Expand All @@ -29,19 +30,19 @@ pub fn vec_function(b: Vec<i32>) -> (Vec<i32>, Vec<f64>) {

### Usage

Here is a possible program to test your function :
Here is a possible program to test your function:

```rust
use copy::*;

fn main() {
let a: i32 = 0;
let b = String::from("1 2 4 5 6");
let c = vec![1, 2, 4, 5];
let a = "1 2 4 5 6".to_owned();
let b = vec![1, 2, 4, 5];
let c = 0;

println!("{:?}", nbr_function(a));
println!("{:?}", str_function(b));
println!("{:?}", vec_function(c));
println!("{:?}", nbr_function(c));
println!("{:?}", vec_function(b));
println!("{:?}", str_function(a));
}
```

Expand All @@ -50,8 +51,8 @@ And its output:
```console
$ cargo run
(0, 1.0, -inf)
("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])
("1 2 4 5 6", "2.718281828459045 7.38905609893065 54.598150033144236 148.4131591025766 403.4287934927351")
$
```

Expand Down
11 changes: 7 additions & 4 deletions subjects/doubtful/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Create a function named `doubtful` which appends a question mark to every string passed to it. It must not return a value.

### Expected functions

```rust
pub fn doubtful(s: /*give the correct type*/ ) {
}
Expand All @@ -17,14 +18,16 @@ You'll need to complete the function signature, so that it works properly with t
Here is a program to test your function

```rust
use doubtful::*;

fn main() {
let mut s = String::from("Hello");
let mut s = "Hello".to_owned();

println!("Before changing the string: {}", s);
println!("Before changing the string: {}", s);

doubtful(/*add your code here*/);
doubtful(&mut s);

println!("After changing the string: {}", s);
println!("After changing the string: {}", s);
}
```

Expand Down
4 changes: 2 additions & 2 deletions subjects/name_initials/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
Create a **function** named `initials`. This function will receive a vector of string literals with names, and return a vector of Strings with the initials of each name.

### Expected Functions

```rust
pub fn initials(names: Vec<&str>) -> Vec<String> {
}
```

> Your heap allocations will be monitored to ensure that you do not make too many allocations, and that your allocations are reasonably sized.

### Usage

Here is a program to test your function:

```rust
use name_initials::initials;
use name_initials::*;

fn main() {
let names = vec!["Harry Potter", "Someone Else", "J. L.", "Barack Obama"];
Expand Down
21 changes: 11 additions & 10 deletions subjects/ownership/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

### Instruction

Create a **function** named `first_subword`, that takes ownership of a string and returns the first sub-word in it. It should work for `camelCase`, `PascalCase`, and `snake_case`.
Create a **function** named `first_subword`, that **moves** a string (takes ownership of it) and returns the first sub-word in it. You can mutate the original string. It should work for `camelCase`, `PascalCase`, and `snake_case`.

### Expected functions

```rust
pub fn first_subword(mut s: String) -> String {
}
Expand All @@ -18,15 +19,15 @@ Here is a program to test your function:
use ownership::first_subword;

fn main() {
let s1 = String::from("helloWorld");
let s2 = String::from("snake_case");
let s3 = String::from("CamelCase");
let s4 = String::from("just");

println!("first_subword({}) = {}", s1.clone(), first_subword(s1));
println!("first_subword({}) = {}", s2.clone(), first_subword(s2));
println!("first_subword({}) = {}", s3.clone(), first_subword(s3));
println!("first_subword({}) = {}", s4.clone(), first_subword(s4));
let s1 = "helloWorld";
let s2 = "snake_case";
let s3 = "CamelCase";
let s4 = "just";

println!("first_subword({}) = {}", s1, first_subword(s1.to_owned()));
println!("first_subword({}) = {}", s2, first_subword(s2.to_owned()));
println!("first_subword({}) = {}", s3, first_subword(s3.to_owned()));
println!("first_subword({}) = {}", s4, first_subword(s4.to_owned()));
}
```

Expand Down
3 changes: 2 additions & 1 deletion subjects/string_literals/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Create the following functions:
- `find`: that returns the index of the first character of a given string that matches the pattern.

### Expected functions

```rust
pub fn is_empty(v: &str) -> bool {
}
Expand All @@ -28,7 +29,7 @@ pub fn find(v: &str, pat: char) -> usize {
}
```

> Your heap allocations will be monitored to ensure that you do not make too many allocations, and that your allocations are reasonably sized.
> You mustn't allocate to the heap in any of these functions.
### Usage

Expand Down
46 changes: 18 additions & 28 deletions subjects/tic_tac_toe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,67 +6,57 @@ You must create some functions for a tic-tac-toe checker.

Create a function named `tic_tac_toe`, which receives a tic-tac-toe table. It should return the appropriate string: `"player O won"`, `"player X won"` or `"tie"`.


Also create the following functions, which each accept a player and a table. These functions should return `true` if the player has completed one of the diagonals, rows or columns:

### Expected functions

```rust
pub fn tic_tac_toe(table: Vec<Vec<&str>>) -> String {
pub fn tic_tac_toe(table: [[char; 3]; 3]) -> String {
}

pub fn diagonals(player: &str, table: &Vec<Vec<&str>>) -> bool {
pub fn diagonals(player: char, table: [[char; 3]; 3]) -> bool {
}

pub fn horizontal(player: &str, table: &Vec<Vec<&str>>) -> bool {
pub fn horizontal(player: char, table: [[char; 3]; 3]) -> bool {
}

pub fn vertical(player: &str, table: &Vec<Vec<&str>>) -> bool {
pub fn vertical(player: char, table: [[char; 3]; 3]) -> bool {
}
```

### Usage

Here is a program to test your `tic_tac_toe`. You'll need to test the other functions yourself. But they'll probably be useful for implementing your `tic_tac_toe` checker.
Here is a program to test your `tic_tac_toe`. You'll need to test the other functions yourself.

```rust
use tic_tac_toe::*;

fn main() {
println!(
"{:?}",
tic_tac_toe(vec![
vec!["O", "X", "O"],
vec!["O", "O", "X"],
vec!["X", "#", "X"]
])
"{}",
tic_tac_toe([['O', 'X', 'O'], ['O', 'P', 'X'], ['X', '#', 'X']])
);

// tie
println!(
"{:?}",
tic_tac_toe(vec![
vec!["X", "O", "O"],
vec!["X", "O", "O"],
vec!["#", "O", "X"]
])
"{}",
tic_tac_toe([['X', 'O', 'O'], ['X', 'O', 'O'], ['#', 'O', 'X']])
);
// player O won

let dig = vec![
vec!["O", "O", "X"],
vec!["O", "X", "O"],
vec!["X", "#", "X"]
];
let diag = [['O', 'O', 'X'], ['O', 'X', 'O'], ['X', '#', 'X']];

println!("{:?}",tic_tac_toe(dig));
println!("{}", tic_tac_toe(diag));
// player X won
}
```

And its output

```console
$ cargo run
"tie"
"player O won"
"player X won"
tie
player O won
player X won
$
```

Expand Down
Loading

0 comments on commit fd49608

Please sign in to comment.