Skip to content

Commit

Permalink
fix(rust-quest01): applied proposed changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrodesu authored Dec 9, 2024
1 parent 6eeb184 commit bc94fc9
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 64 deletions.
2 changes: 1 addition & 1 deletion subjects/division_and_remainder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn divide(x: i32, y: i32) -> (i32, i32) {
Here is a program to test your function

```rust
use division_and_remainder::divide;
use division_and_remainder::*;

fn main() {
let x = 9;
Expand Down
30 changes: 23 additions & 7 deletions subjects/fibonacci2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Complete the body of the **function** `fibonacci`.

```rust
pub fn fibonacci(n: u32) -> u32 {
}
pub fn fibonacci(n: u32) -> u32 {
}
```

This function receives a number `n` and returns the `n`th number in the fibonacci series.
Expand All @@ -18,13 +18,29 @@ The Fibonacci Series starts like this: 0, 1, 1, 2, 3, 5, 8, 13 etc...
Here is a possible test for your function:

```rust
use fibonacci2::fibonacci;
use fibonacci2::*;

fn main() {
println!("The element in the position {} in fibonacci series is {}",2, fibonacci(2));
println!("The element in the position {} in fibonacci series is {}",4, fibonacci(4));
println!("The element in the position {} in fibonacci series is {}",22, fibonacci(22));
println!("The element in the position {} in fibonacci series is {}", 20, fibonacci(20));
println!(
"The element in the position {} in fibonacci series is {}",
2,
fibonacci(2)
);
println!(
"The element in the position {} in fibonacci series is {}",
4,
fibonacci(4)
);
println!(
"The element in the position {} in fibonacci series is {}",
22,
fibonacci(22)
);
println!(
"The element in the position {} in fibonacci series is {}",
20,
fibonacci(20)
);
}
```

Expand Down
4 changes: 2 additions & 2 deletions subjects/find_factorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Example: the factorial of 6 (written 6!) is 1 \* 2 \* 3 \* 4 \* 5 \* 6 = 720.
### Usage

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

```rust
use find_factorial::factorial;
use find_factorial::*;

fn main() {
println!("The factorial of 0 = {}", factorial(0));
Expand Down
32 changes: 14 additions & 18 deletions subjects/groceries/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ Create a **function** named `insert`, that inserts a new element at the end of t

Create another **function** named `at_index` that returns the value found at the index passed as an argument.


```rust
pub fn insert(vec: &mut Vec<String>, val: String) {
}

pub fn at_index(vec: &Vec<String>, index: usize) -> String {
pub fn at_index(slice: &[String], index: usize) -> &str {
}
```

Expand All @@ -20,34 +19,31 @@ pub fn at_index(vec: &Vec<String>, index: usize) -> String {
Here is a possible program to test your function:

```rust
use groceries::{insert, at_index};
use groceries::*;

fn main() {
let mut groceries = vec![
"yogurt".to_string(),
"panettone".to_string(),
"bread".to_string(),
"cheese".to_string(),
];
insert(&mut groceries, String::from("nuts"));
println!("The groceries list contains {:?}", &groceries);
println!(
"The second element of the grocery list is {:?}",
at_index(&groceries, 1)
);
let mut groceries = vec![
"yogurt".to_string(),
"panettone".to_string(),
"bread".to_string(),
"cheese".to_string(),
];
insert(&mut groceries, String::from("nuts"));
println!("groceries = {:?}", &groceries);
println!("groceries[1] = {:?}", at_index(&groceries, 1));
}
```

And its output:

```console
$ cargo run
The groceries list contains ["yogurt", "panettone", "bread", "cheese", "nuts"]
The second element of the grocery list is "panettone"
groceries = ["yogurt", "panettone", "bread", "cheese", "nuts"]
groceries[1] = "panettone"
$
```

### Notions

- [Common Collections](https://doc.rust-lang.org/stable/book/ch08-00-common-collections.html)
- [Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html)
- [Vectors](https://doc.rust-lang.org/stable/book/ch08-01-vectors.html)
2 changes: 1 addition & 1 deletion subjects/looping/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Answer: The letter e
- [Module std::io](https://doc.rust-lang.org/std/io/index.html)
- [loop](https://doc.rust-lang.org/std/keyword.loop.html)

### Usage 
### Usage

```console
$ cargo run
Expand Down
3 changes: 1 addition & 2 deletions subjects/matrix_transposition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ The transposition of a matrix, switches the columns to rows, and the rows to col
Here is a possible program to test your function

```rust
use matrix_transposition::transpose;
use matrix_transposition::Matrix;
use matrix_transposition::*;

fn main() {
let matrix = Matrix((1, 3), (4, 5));
Expand Down
5 changes: 2 additions & 3 deletions subjects/reverse_string/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

Create a **function** named `rev_str` that takes a `&str` as a parameter, and returns a `String` with its letters reversed.


```rust
pub fn rev_str(input: &str) -> String {
}
Expand All @@ -15,7 +14,7 @@ pub fn rev_str(input: &str) -> String {
Here is a possible program to test your function :

```rust
use reverse_string::rev_str;
use reverse_string::*;

fn main() {
println!("{}", rev_str("Hello, world!"));
Expand Down Expand Up @@ -43,4 +42,4 @@ $
- [Strings](https://doc.rust-lang.org/rust-by-example/std/str.html)
- [Primitive Type str](https://doc.rust-lang.org/std/primitive.str.html)
- [Primtive Type char](https://doc.rust-lang.org/std/primitive.char.html)
- [Module std::string](https://doc.rust-lang.org/std/string/index.html)
- [Module std::string](https://doc.rust-lang.org/std/string/index.html)
29 changes: 16 additions & 13 deletions subjects/scalar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ Create the following **functions**, which each accept two parameters:
- `rem`, which returns the remainder of the division between two 32bit values

> You will need to figure out the exact data types for the parameters **and** the return values.
There are some hints in the example.

> There are some hints in the example.
```rust
pub fn sum(a: X, b: X) -> X {
Expand Down Expand Up @@ -43,23 +42,27 @@ use scalar::*;

fn main() {
// sum
println!("sum : {}", sum(234, 2)); // 'sum : 236'
println!("sum : {}", sum(1, 255)); // 'ERROR: attempt to add with overflow'
println!("sum: {}", sum(234, 2)); // 'sum: 236'
println!("sum: {}", sum(1, 255)); // 'ERROR: attempt to add with overflow'

// diff
println!("diff : {}", diff(234, 2)); // 'diff : 232'
println!("diff : {}", diff(-32768, 32766)); // 'ERROR: attempt to subtract with overflow'
println!("diff: {}", diff(234, 2)); // 'diff: 232'
println!("diff: {}", diff(-32768, 32766)); // 'ERROR: attempt to subtract with overflow'

// product
println!("pro : {}", pro(23, 2)); // 'pro : 46'
println!("pro : {}", pro(-128, 2)); // 'ERROR: attempt to multiply with overflow'
println!("pro: {}", pro(23, 2)); // 'pro: 46'
println!("pro: {}", pro(-128, 2)); // 'ERROR: attempt to multiply with overflow'

// quotient
println!("quo : {}", quo(22.0, 2.0));// 'quo : 11'
println!("quo : {}", quo(-128.23, 2.0));// 'quo : -64.115'
println!("quo: {}", quo(22.0, 2.0)); // 'quo: 11'
println!("quo: {}", quo(-128.23, 2.0)); // 'quo: -64.115'

// remainder
println!("rem : {}", rem(22.0, 2.0));// 'rem : 0'
println!("rem : {}", rem(-128.23, 2.0));// 'rem : -0.22999573'
println!("rem: {}", rem(22.0, 2.0)); // 'rem: 0'
println!("rem: {}", rem(-128.23, 2.0)); // 'rem: -0.22999573'
}
```

### Notions

- [Data Types](https://doc.rust-lang.org/book/ch03-02-data-types.html)
- [Data Types](https://doc.rust-lang.org/book/ch03-02-data-types.html)
8 changes: 4 additions & 4 deletions subjects/speed_transformation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ pub fn km_per_hour_to_meters_per_second(km_h: f64) -> f64 {
### Usage

```rust
use speed_transformation::km_per_hour_to_meters_per_second;
use speed_transformation::*;

fn main() {
let km_h = 100.0;
let m_s = km_per_hour_to_meters_per_second(km_h);
println!("{} km/h is equivalent to {} m/s", km_h, m_s);
let km_h = 100.0;
let m_s = km_per_hour_to_meters_per_second(km_h);
println!("{} km/h is equivalent to {} m/s", km_h, m_s);
}
```

Expand Down
4 changes: 2 additions & 2 deletions subjects/temperature_conv/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub fn celsius_to_fahrenheit(c: f64) -> f64 {
use temperature_conv::*;

fn main() {
println!("{} F = {} C", -459.67, fahrenheit_to_celsius(-459.67));
println!("{} C = {} F", 0.0, celsius_to_fahrenheit(0.0));
println!("{} F = {} C", -459.67, fahrenheit_to_celsius(-459.67));
println!("{} C = {} F", 0.0, celsius_to_fahrenheit(0.0));
}
```

Expand Down
20 changes: 9 additions & 11 deletions subjects/tuples_refs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
pub fn id(student: &Student) -> u32 {
}

pub fn first_name(student: &Student) -> String {
pub fn first_name(student: &Student) -> &str {
}

pub fn last_name(student: &Student) -> String {
pub fn last_name(student: &Student) -> &str {
}
```

Expand All @@ -25,22 +25,20 @@ Here is a program to test your functions
use tuples_refs::*;

fn main() {
let student = Student(20, "Pedro".to_string(), "Domingos".to_string());
println!("Student: {:?}", student);
println!("Student first name: {}", first_name(&student));
println!("Student last name: {}", last_name(&student));
println!("Student Id: {}", id(&student));
let student = Student(20, "Pedro".to_string(), "Domingos".to_string());
println!("Student's first name: {}", first_name(&student));
println!("Student's last name: {}", last_name(&student));
println!("Student's id: {}", id(&student));
}
```

And its output:

```console
$ cargo run
Student: Student(20, "Pedro", "Domingos")
Student first name: Pedro
Student last name: Domingos
Student Id: 20
Student's first name: Pedro
Student's last name: Domingos
Student's id: 20
$
```

Expand Down

0 comments on commit bc94fc9

Please sign in to comment.