Skip to content

Commit 8a20634

Browse files
authored
Merge branch 'master' into ahmed-01
2 parents 0cfbce3 + 4e92258 commit 8a20634

12 files changed

Lines changed: 222 additions & 200 deletions

File tree

subjects/borrow_box/README.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Game time.
66

77
You will implement some **CRUD** functionality for a game session. You will need to implement the `GameSession` structure with the following associated functions:
88

9-
- `new`: which initializes a game session state with player names and some other information. This function returns the structure wrapped in a `Box`.
9+
- `new`: which initializes a game session state with player names and some other information.
1010

11-
- `read_winner`: which returns a tuple with the name and score of the player who is currently winning. In the case that no player is winning, it should return the same tuple with the string `"Same score! tied"` and the tied score.
11+
- `read_winner`: which returns a tuple referencing the player who is currently winning. In the case that no player is winning, it should return `None`.
1212

1313
- `update_score`: which receives the name of a player, and increments their score. This function should **do nothing** if the the game session is already finished or if the name received doesn't match any player.
1414

15-
- `delete`: which takes ownership of the boxed game session and returns a string: `"game deleted: id -> 0"`, where `0` is the id of the `GameSession`.
15+
- `delete`: which takes ownership of the game session and returns a string: `"game deleted: id -> {id}"`, where `{id}` is the id of the `GameSession`.
1616

1717
> Examples for `nb_games`:
1818
>
@@ -25,23 +25,26 @@ You will implement some **CRUD** functionality for a game session. You will need
2525
#[derive(Debug, Clone, Eq, PartialEq)]
2626
pub struct GameSession {
2727
pub id: u32,
28-
pub p1: (String, u16),
29-
pub p2: (String, u16),
30-
pub nb_games: u16
28+
pub p1: (String, u32),
29+
pub p2: (String, u32),
30+
pub nb_games: u32,
3131
}
3232

3333
impl GameSession {
34-
pub fn new(id: u32, p1_name: String, p2_name: String, nb_games: u16) -> Box<GameSession> {
35-
34+
pub fn new(id: u32, p1_name: String, p2_name: String, nb_games: u32) -> GameSession {
35+
todo!()
3636
}
37-
pub fn read_winner(&self) -> (String, u16) {
3837

38+
pub fn read_winner(&self) -> Option<&(String, u32)> {
39+
todo!()
3940
}
40-
pub fn update_score(&mut self, user_name: String) {
4141

42+
pub fn update_score(&mut self, user_name: &str) {
43+
todo!()
4244
}
43-
pub fn delete(self) -> String {
4445

46+
pub fn delete(self) -> String {
47+
todo!()
4548
}
4649
}
4750
```
@@ -56,38 +59,33 @@ use borrow_box::*;
5659
fn main() {
5760
let mut game = GameSession::new(0, String::from("Joao"), String::from("Susana"), 5);
5861
println!("{:?}", game.read_winner());
59-
// output : ("Same score! tied", 0)
6062

61-
game.update_score(String::from("Joao"));
62-
game.update_score(String::from("Joao"));
63-
game.update_score(String::from("Susana"));
64-
game.update_score(String::from("Susana"));
63+
game.update_score("Joao");
64+
game.update_score("Joao");
65+
game.update_score("Susana");
66+
game.update_score("Susana");
6567
println!("{:?}", game.read_winner());
66-
// output : ("Same score! tied", 2)
6768

68-
game.update_score(String::from("Joao"));
69-
// this one will not count because it already 5 games played, the nb_games
70-
game.update_score(String::from("Susana"));
69+
game.update_score("Joao");
70+
// This one will not count because it already 5 games played, the `nb_games`
71+
game.update_score("Susana");
7172

7273
println!("{:?}", game.read_winner());
73-
// output : ("Joao", 3)
7474

7575
println!("{:?}", game.delete());
76-
// output : "game deleted: id -> 0"
7776

7877
// game.read_winner();
79-
// this will give error
80-
// because the game was dropped, no longer exists on the heap
78+
// This will give an error as the game was dropped with `delete` and no longer exists
8179
}
8280
```
8381

8482
And its output:
8583

8684
```console
8785
$ cargo run
88-
("Same score! tied", 0)
89-
("Same score! tied", 2)
90-
("Joao", 3)
86+
None
87+
None
88+
Some(("Joao", 3))
9189
"game deleted: id -> 0"
9290
$
9391
```

subjects/bowling_score/README.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,19 @@ pub enum Error {
4343
GameComplete,
4444
}
4545

46-
pub struct BowlingGame {
47-
...
48-
}
46+
pub struct BowlingGame {}
4947

5048
impl BowlingGame {
5149
pub fn new() -> Self {
52-
unimplemented!();
50+
todo!();
5351
}
5452

55-
pub fn roll(&mut self, pins: u16) -> Result<(), Error> {
56-
unimplemented!();
53+
pub fn roll(&mut self, pins: u32) -> Result<(), Error> {
54+
todo!();
5755
}
5856

59-
pub fn score(&mut self) -> Some(u16) {
60-
unimplemented!();
57+
pub fn score(&self) -> Option<u32> {
58+
todo!();
6159
}
6260
}
6361
```

subjects/box_it/README.md

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44

55
Create the following **functions**:
66

7-
- `transform_and_save_on_heap`: which accepts a string of numbers separated by spaces. If a number has a `'k'` as a suffix it should be multiplied by 1000. The function transforms those numbers into a vector of `u32`, and saves them in the heap using `Box`.
7+
- `parse_into_boxed`: which accepts a string of numbers separated by spaces. If a number has a `k` as a suffix it should be multiplied by 1000. The function parses these numbers and boxes them into a vector of `Box<u32>`.
88

9-
- `take_value_ownership`: which accepts the return value from `transform_and_save_on_heap`, unboxes the value, and returns it.
9+
- `into_unboxed`: which accepts the value returned from `parse_into_boxed` and unboxes each element into another vector.
1010

1111
### Expected Functions
1212

1313
```rust
14-
pub fn transform_and_save_on_heap(s: String) -> Box<Vec<u32>> {
15-
14+
pub fn parse_into_boxed(s: String) -> Vec<Box<u32>> {
15+
todo!()
1616
}
17-
pub fn take_value_ownership(a: Box<Vec<u32>>) -> Vec<u32> {
1817

18+
pub fn into_unboxed(a: Vec<Box<u32>>) -> Vec<u32> {
19+
todo!()
1920
}
2021
```
2122

@@ -24,31 +25,33 @@ pub fn take_value_ownership(a: Box<Vec<u32>>) -> Vec<u32> {
2425
Here is a program to test your functions
2526

2627
```rust
28+
use std::mem;
29+
2730
use box_it::*;
2831

2932
fn main() {
30-
let new_str = String::from("5.5k 8.9k 32");
33+
let s = "5.5k 8.9k 32".to_owned();
34+
35+
let boxed = parse_into_boxed(s);
36+
println!("Element value: {:?}", boxed[0]);
37+
println!("Element size: {:?} bytes", mem::size_of_val(&boxed[0]));
3138

32-
// creating a variable and we save it in the Heap
33-
let a_h = transform_and_save_on_heap(new_str);
34-
println!("Box value : {:?}", &a_h);
35-
println!("size occupied in the stack : {:?} bytes", (std::mem::size_of_val(&a_h)));
39+
let unboxed = into_unboxed(boxed);
40+
println!("Element value: {:?}", unboxed[0]);
41+
println!("Element size: {:?} bytes", mem::size_of_val(&unboxed[0]));
3642

37-
let a_b_v = take_value_ownership(a_h);
38-
println!("value : {:?}", &a_b_v);
39-
println!("size occupied in the stack : {:?} bytes", (std::mem::size_of_val(&a_b_v)));
40-
// whenever the box, in this case "a_h", goes out of scope it will be deallocated, freed
43+
// As with everything related to regular Rust memory management, both the `Vec` and the `Box`es will be properly dropped when out of scope and freed, ensuring no leaks
4144
}
4245
```
4346

4447
And its output:
4548

4649
```console
4750
$ cargo run
48-
Box value : [5500, 8900, 32]
49-
size occupied in the stack : 8 bytes
50-
value : [5500, 8900, 32]
51-
size occupied in the stack : 24 bytes
51+
Element value: 5500
52+
Element size: 8 bytes
53+
Element value: 5500
54+
Element size: 4 bytes
5255
$
5356
```
5457

subjects/box_recursion/README.md

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,32 @@
55
Using the given code, create the following **associated functions**:
66

77
- `new`: which will initialize the `WorkEnvironment` with `grade` set to `None`.
8-
- `add_worker`: which receives two strings, one being the role and the other the name of the worker. It will add the worker at the start of the list.
9-
- `remove_worker`: which removes the last worker that was placed in the `WorkEnvironment`, this function returns an `Option` with the name of the worker.
8+
- `add_worker`: which receives a name of a worker and a role. It will add the worker at the start of the list.
9+
- `remove_worker`: which removes the last worker that was placed in the `WorkEnvironment`, this function returns an `Option` with the name of the removed worker.
1010
- `last_worker`: which returns an `Option` with a tuple containing the name and role of the last added worker.
1111

12-
You must also create a type named `Link`. This will be the connection between the `WorkEnvironment` and `Worker` structures. This will be a recursion type, and it must point to `None` if there is no `Worker` to point to.
12+
You must also create a `Role` enum which can be `CEO`, `Manager`, or `Worker`, and will represent the role of a worker. This enum should implement the `From` trait for `&str`.
13+
14+
Additionally, create a type named `Link`, which will be the connection between the `WorkEnvironment` and `Worker` structures. It will point to `None` if there is no `Worker` to point to.
1315

1416
### Expected Functions and structures
1517

1618
```rust
19+
#[derive(Debug, PartialEq)]
20+
pub enum Role {
21+
CEO,
22+
Manager,
23+
Worker,
24+
}
25+
26+
impl From<&str> for Role {}
27+
1728
#[derive(Debug)]
1829
pub struct WorkEnvironment {
1930
pub grade: Link,
2031
}
2132

22-
pub type Link =
33+
pub type Link;
2334

2435
#[derive(Debug)]
2536
pub struct Worker {
@@ -29,10 +40,21 @@ pub struct Worker {
2940
}
3041

3142
impl WorkEnvironment {
32-
pub fn new() -> WorkEnvironment {}
33-
pub fn add_worker(&mut self, role: String, name: String) {}
34-
pub fn remove_worker(&mut self) -> Option<String> {}
35-
pub fn last_worker(&self) -> Option<(String, String)> {}
43+
pub fn new() -> Self {
44+
todo!()
45+
}
46+
47+
pub fn add_worker(&mut self, name: &str, role: &str) {
48+
todo!()
49+
}
50+
51+
pub fn remove_worker(&mut self) -> Option<String> {
52+
todo!()
53+
}
54+
55+
pub fn last_worker(&self) -> Option<(String, Role)> {
56+
todo!()
57+
}
3658
}
3759
```
3860

@@ -45,19 +67,24 @@ use box_recursion::*;
4567

4668
fn main() {
4769
let mut list = WorkEnvironment::new();
48-
list.add_worker(String::from("CEO"), String::from("Marie"));
49-
list.add_worker(String::from("Manager"), String::from("Monica"));
50-
list.add_worker(String::from("Normal Worker"), String::from("Ana"));
51-
list.add_worker(String::from("Normal Worker"), String::from("Alice"));
70+
71+
list.add_worker("Marie", "CEO");
72+
list.add_worker("Monica", "Manager");
73+
list.add_worker("Ana", "Normal Worker");
74+
list.add_worker("Alice", "Normal Worker");
75+
5276
println!("{:#?}", list);
5377

5478
println!("{:?}", list.last_worker());
5579

5680
list.remove_worker();
5781
list.remove_worker();
5882
list.remove_worker();
83+
5984
println!("{:?}", list);
85+
6086
list.remove_worker();
87+
6188
println!("{:?}", list);
6289
}
6390
```
@@ -69,19 +96,19 @@ $ cargo run
6996
WorkEnvironment {
7097
grade: Some(
7198
Worker {
72-
role: "Normal Worker",
99+
role: Worker,
73100
name: "Alice",
74101
next: Some(
75102
Worker {
76-
role: "Normal Worker",
103+
role: Worker,
77104
name: "Ana",
78105
next: Some(
79106
Worker {
80-
role: "Manager",
107+
role: Manager,
81108
name: "Monica",
82109
next: Some(
83110
Worker {
84-
role: "CEO",
111+
role: CEO,
85112
name: "Marie",
86113
next: None,
87114
},
@@ -93,8 +120,8 @@ WorkEnvironment {
93120
},
94121
),
95122
}
96-
Some(("Alice", "Normal Worker"))
97-
WorkEnvironment { grade: Some(Worker { role: "CEO", name: "Marie", next: None }) }
123+
Some(("Alice", Worker))
124+
WorkEnvironment { grade: Some(Worker { role: CEO, name: "Marie", next: None }) }
98125
WorkEnvironment { grade: None }
99126
$
100127
```

0 commit comments

Comments
 (0)