Skip to content

Commit

Permalink
fix(rust-quest03): applied proposed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodesu authored Jan 29, 2025
1 parent e3feb44 commit 942b3c9
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 227 deletions.
34 changes: 15 additions & 19 deletions subjects/arrays/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

Define a **function** named `thirtytwo_tens` that returns an array with 32 positions filled with only the value `10`, so that `[10, 10, 10, ... 10].len()` is equal to 32.

Write a **function** that takes an array of `i32` and returns the sum of the elements (make it work with the main).
Write a **function** that takes an array of `i32` and returns the sum of the elements.

### Expected functions

The type of one of the arguments is missing. Use the example `main` function to determine the correct type.

```rust
pub fn sum(a: _) -> i32 {
//type of argument missing in the signature here
}

pub fn thirtytwo_tens() -> [i32; 32] {
Expand All @@ -26,32 +25,29 @@ Here is a program to test your function.
> It's incomplete. Use the output and the other available information to figure out what is missing.
```rust
use arrays::{sum, thirtytwo_tens};
use arrays::*;

fn main() {
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let a1: Vec<i32> = (1..11).; //missing info here
let b = [_; 10]; //missing info here

println!("The Sum of the elements in {:?} = {}", a, sum(a));//missing info here
println!("The Sum of the elements in {:?} = ", a1, sum(a1));//missing info here
println!("The Sum of the elements in {:?} = {}", b, sum(b));//missing info here
println!(
"Array size {} with only 10's in it {:?}",
thirtytwo_tens().len(),
thirtytwo_tens()
);
let a = (1..=10)._;
let b = [_];

println!("The sum of the elements in {:?} is {}", a, sum(&a));
println!("The sum of the elements in {:?} is {}", b, sum(&b));
println!(
"Array of {} elements filled with 10 = {:?}",
thirtytwo_tens().len(),
thirtytwo_tens()
);
}
```

And its output:

```console
$ cargo run
The Sum of the elements in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = 55
The Sum of the elements in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] = 55
The Sum of the elements in [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] = 50
Array size 32 with only 10's in it [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
The sum of the elements in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is 55
The sum of the elements in [5, 5, 5, 5, 5, 5, 5, 5, 5, 5] is 50
Array of 32 elements filled with 10 = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
$
```

Expand Down
21 changes: 12 additions & 9 deletions subjects/bigger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ pub fn bigger(h: HashMap<&str, i32>) -> i32 {
Here is a program to test your function.

```rust
use bigger::*;
use std::collections::HashMap;
use bigger::bigger;

fn main() {

let mut hash = HashMap::new();
hash.insert("Daniel", 122);
hash.insert("Ashley", 333);
hash.insert("Katie", 334);
hash.insert("Robert", 14);

println!("The biggest of the elements in the HashMap is {}", bigger(hash));
let hash = HashMap::from_iter([
("Daniel", 122),
("Ashley", 333),
("Katie", 334),
("Robert", 14),
]);

println!(
"The biggest of the elements in the HashMap is {}",
bigger(hash)
);
}
```

Expand Down
4 changes: 2 additions & 2 deletions subjects/capitalizing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ use capitalizing::*;
fn main() {
println!("{}", capitalize_first("joe is missing"));
println!("{}", title_case("jill is leaving A"));
println!("{}",change_case("heLLo THere"));
println!("{}", change_case("heLLo THere"));
}
```

And its output

```consoole
```console
$ cargo run
Joe is missing
Jill Is Leaving A
Expand Down
44 changes: 20 additions & 24 deletions subjects/card_deck/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ Define:
- The associated **function** `random` for `Rank` and `Suit` returns a random `Rank` and `Suit` respectively.
- Finally define the **function** `winner_card` which returns `true` if the card passed as an argument is an ace of spades.

### Dependencies

[rand = "0.3.14"](https://docs.rs/crate/rand/0.3.14)

### Expected Functions and Structures

```rust
Expand All @@ -33,24 +29,24 @@ pub enum Rank {
}

impl Suit {
pub fn random() -> Suit {
}
pub fn random() -> Suit {
}

pub fn translate(value: u8) -> Suit {
}
pub fn translate(value: u8) -> Suit {
}
}

impl Rank {
pub fn random() -> Rank {
}
pub fn random() -> Rank {
}

pub fn translate(value: u8) -> Rank {
}
pub fn translate(value: u8) -> Rank {
}
}

pub struct Card {
pub suit: Suit,
pub rank: Rank,
pub suit: Suit,
pub rank: Rank,
}

pub fn winner_card(card: &Card) -> bool {
Expand All @@ -63,18 +59,18 @@ Here is a program to test your function

```rust
use card_deck::*;

fn main() {
let your_card = Card {
rank: Rank::random(),
suit: Suit::random(),
};
let your_card = Card {
rank: Rank::random(),
suit: Suit::random(),
};

println!("Your card is {:?}", your_card);
println!("Your card is {:?}", your_card);

// Now if the card is an Ace of Spades print "You are the winner"
if card_deck::winner_card(&your_card) {
println!("You are the winner!");
}
if card_deck::winner_card(your_card) {
println!("You are the winner!");
}
}
```

Expand All @@ -88,5 +84,5 @@ $

### Notions

- [Crate rand](https://docs.rs/rand/0.3.14/rand/)
- [Crate rand](https://docs.rs/rand/latest/rand/)
- [Enums](https://doc.rust-lang.org/book/ch06-00-enums.html)
20 changes: 9 additions & 11 deletions subjects/changes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Imagine you are working on some software to control smart lights in a house. You

Define the associated **function** `new`, and add it to the data structure `Light`. It should create a new light with the alias passed as an argument, with a brightness of 0.

Define the **function** `change_brightness`, which receives a `Vec` of lights, an `alias` and a `u8`value. It should find the light in the `Vec` by its alias, and set the value of the brightness.
Define the **function** `change_brightness`, which receives a slice of lights, an `alias` and a `u8`value. It should attempt to find the correct light by its alias, and change the value of the brightness if found.

### Expected Functions and Structure

Expand All @@ -22,7 +22,7 @@ impl Light {
}
}

pub fn change_brightness(lights: &mut Vec<Light>, alias: &str, value: u8) {
pub fn change_brightness(lights: &mut [Light], alias: &str, value: u8) {
}
```

Expand All @@ -34,15 +34,13 @@ Here is an incomplete program to test your function
use changes::*;

fn main() {
// bedroom
let mut lights = vec![
Light::new("living_room"),
Light::new("bedroom"),
Light::new("rest_room"),
];
println!("brightness = {}", lights[0].brightness);
change_brightness(&mut lights, "living_room", 200);
println!("new brightness = {}", lights[0].brightness);
let mut lights = ["living_room", "bedroom", "rest_room"].map(Light::new);

println!("brightness = {}", lights[0].brightness);

change_brightness(&mut lights, "living_room", 200);

println!("new brightness = {}", lights[0].brightness);
}
```

Expand Down
70 changes: 33 additions & 37 deletions subjects/circle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Instructions

Create the structures `Circle` and `Point`. You'll need to create the necessary methods for the code in the [usage](#usage) to compile, and give the expected output.
Create the structures `Circle` and `Point`. You'll need to create the necessary methods for the code to compile correctly.

#### Methods:

Expand All @@ -11,19 +11,19 @@ Create the structures `Circle` and `Point`. You'll need to create the necessary
- `Circle`:
- `diameter()` -> returns the diameter of the circle.
- `area()` -> returns the area of the circle.
- `intersect()` -> which returns `true`, if 2 circles intersect.
- `intersect()` -> returns if two circles intersect.

#### Associated Functions

- `Circle`:
- `new()` -> receives three 64bit floating point numbers in the following order: x, y and radius (x and y are the coordinates of the center of the new circle). The function returns a new circle.
- `new()` -> receives three 64-bit floating point numbers in the following order: x, y and radius (x and y are the coordinates of the center of the new circle). The function returns a new circle.

### Expected Functions and Structures

This snippets are incomplete, you'll need to complete them. You'll find some useful information in the [usage](#usage).
This snippet is incomplete, you'll need to complete it. Base yourself from the usage section below.

```rust
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub struct Circle {
pub center //..
pub radius //..
Expand All @@ -33,10 +33,8 @@ impl Circle {
// ...
}

#[derive(Debug)]
pub struct Point {
// ...
}
#[derive(Debug, Clone, Copy)]
pub struct Point(/* */);

impl Point {
// ...
Expand All @@ -48,44 +46,42 @@ impl Point {
Here is a program to test your function

```rust
use std::f64::consts;
use circle::{Circle, Point};
use circle::*;

fn main() {
let circle = Circle::new(500.0, 500.0, 150.0);
let circle1 = Circle {
center: Point { x: 80.0, y: 115.0 },
radius: 30.0,
};
let point_a = Point { x: 1.0, y: 1.0 };
let point_b = Point { x: 0.0, y: 0.0 };
println!("circle = {:?} area = {}", circle, circle.area());
println!("circle = {:?} diameter = {}", circle, circle.diameter());
println!("circle1 = {:?} diameter = {}", circle1, circle1.diameter());
println!(
"circle and circle1 intersect = {}",
circle.intersect(&circle1)
);

println!(
"distance between {:?} and {:?} is {}",
point_a,
point_b,
point_a.distance(&point_b)
);

let circle = Circle::new(500.0, 500.0, 150.0);
let circle1 = Circle {
center: Point(80.0, 115.0),
radius: 30.0,
};
let point_a = Point(1.0, 1.0);
let point_b = Point(0.0, 0.0);
println!("circle = {:?} area = {}", circle, circle.area());
println!("circle = {:?} diameter = {}", circle, circle.diameter());
println!("circle1 = {:?} diameter = {}", circle1, circle1.diameter());
println!(
"circle and circle1 intersect = {}",
circle.intersect(circle1)
);

println!(
"distance between {:?} and {:?} is {}",
point_a,
point_b,
point_a.distance(point_b)
);
}
```

And its output

```console
$ cargo run
circle = Circle { center: Point { x: 500.0, y: 500.0 }, radius: 150.0 } area = 70685.83470577035
circle = Circle { center: Point { x: 500.0, y: 500.0 }, radius: 150.0 } diameter = 300
circle1 = Circle { center: Point { x: 80.0, y: 115.0 }, radius: 30.0 } diameter = 60
circle = Circle { center: Point(500.0, 500.0), radius: 150.0 } area = 70685.83470577035
circle = Circle { center: Point(500.0, 500.0), radius: 150.0 } diameter = 300
circle1 = Circle { center: Point(80.0, 115.0), radius: 30.0 } diameter = 60
circle and circle1 intersect = false
distance between Point { x: 1.0, y: 1.0 } and Point { x: 0.0, y: 0.0 } is 1.4142135623730951
distance between Point(1.0, 1.0) and Point(0.0, 0.0) is 1.4142135623730951
$
```

Expand Down
17 changes: 9 additions & 8 deletions subjects/collect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

### Instructions

Implement the **function** `bubble_sort`, which receives a `Vec<i32>` and returns the same vector but in increasing order using the bubble sort algorithm.
Implement the **function** `bubble_sort`, which receives a list of integers and sorts it in increasing order using the bubble sort algorithm.

### Expected Function

```rust
pub fn bubble_sort(vec: &mut Vec<i32>) {
pub fn bubble_sort(arr: &mut [i32]) {
}
```

Expand All @@ -19,13 +19,14 @@ Here is a program to test your function.
use collect::*;

fn main() {
let ref mut v = vec![3, 2, 4, 5, 1, 7];
let mut b = v.clone();
bubble_sort(v);
println!("{:?}", v);
let mut v = [3, 2, 4, 5, 1, 7];
let mut v_clone = v;

b.sort();
println!("{:?}", b);
bubble_sort(&mut v);
println!("{:?}", v);

v_clone.sort_unstable();
println!("{:?}", v_clone);
}
```

Expand Down
Loading

0 comments on commit 942b3c9

Please sign in to comment.