Skip to content

Commit 83d9d7a

Browse files
authored
fix(rust-quest01): applied proposed changes
1 parent a54e647 commit 83d9d7a

File tree

24 files changed

+309
-648
lines changed

24 files changed

+309
-648
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
1-
/*Change the body of the function divide to return the value of the
2-
integer division of the x and y and the remainder of that division
3-
You're only allowed to change the body of the function
4-
5-
fn main() {
6-
let x = 9;
7-
let y = 4;
8-
let (division, remainder) = divide(x, y);
9-
println!(
10-
"{}/{}: division = {}, remainder = {}",
11-
x, y, division, remainder
12-
);
13-
}
14-
*/
15-
161
pub fn divide(x: i32, y: i32) -> (i32, i32) {
17-
// You're code here
182
let div = x / y;
193
let rem = x % y;
204
(div, rem)
215
}
22-
23-
#[cfg(test)]
24-
mod tests {
25-
use super::*;
26-
27-
#[test]
28-
fn it_works() {
29-
assert_eq!(divide(3, 4), (3 / 4, 3 % 4));
30-
assert_eq!(divide(73, 4), (73 / 4, 73 % 4));
31-
assert_eq!(divide(432, 32), (432 / 32, 432 % 32));
32-
assert_eq!(divide(897, 29), (897 / 29, 897 % 29));
33-
}
34-
}

solutions/fibonacci2/src/lib.rs

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
1-
// fn main() {
2-
// let val: u32 = 20;
3-
// println!("Fibonacci({}) = {}", val, fibonacci(val));
4-
//}
5-
61
pub fn fibonacci(n: u32) -> u32 {
7-
if n < 2 {
8-
return n;
9-
}
10-
fibonacci(n - 2) + fibonacci(n - 1)
11-
}
12-
13-
#[cfg(test)]
14-
mod tests {
15-
use super::*;
16-
17-
#[test]
18-
fn it_works() {
19-
assert_eq!(fibonacci(0), 0);
20-
assert_eq!(fibonacci(1), 1);
21-
assert_eq!(fibonacci(22), 17711);
22-
assert_eq!(fibonacci(20), 6765);
2+
if matches!(n, 0 | 1) {
3+
n
4+
} else {
5+
fibonacci(n - 2) + fibonacci(n - 1)
236
}
247
}

solutions/find_factorial/src/lib.rs

+4-31
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,7 @@
1-
// Complete this function to return the factorial of a given number
2-
/*
3-
fn main() {
4-
println!("The factorial of 0 = {}", factorial(0));
5-
println!("The factorial of 1 = {}", factorial(1));
6-
println!("The factorial of 5 = {}", factorial(5));
7-
println!("The factorial of 10 = {}", factorial(10));
8-
println!("The factorial of 19 = {}", factorial(19));
9-
}*/
10-
111
pub fn factorial(num: u64) -> u64 {
12-
match num {
13-
0 | 1 => 1,
14-
_ => factorial(num - 1) * num,
15-
}
16-
}
17-
18-
#[cfg(test)]
19-
mod tests {
20-
use super::*;
21-
22-
#[test]
23-
fn factorial_of_1() {
24-
assert_eq!(1, factorial(0));
25-
assert_eq!(1, factorial(1));
26-
assert_eq!(120, factorial(5));
27-
assert_eq!(40320, factorial(8));
28-
assert_eq!(3628800, factorial(10));
29-
assert_eq!(87178291200, factorial(14));
30-
assert_eq!(6402373705728000, factorial(18));
31-
assert_eq!(121645100408832000, factorial(19));
32-
assert_eq!(2432902008176640000, factorial(20));
2+
if num == 0 {
3+
1
4+
} else {
5+
num * factorial(num - 1)
336
}
347
}

solutions/groceries/src/lib.rs

+2-32
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,7 @@
1-
// # Instructions
2-
3-
// Create a function called `insert`
4-
// fn insert(vec: &mut Vec<String>, val: String) that inserts a new element at the end of the Vec
5-
61
pub fn insert(vec: &mut Vec<String>, val: String) {
72
vec.push(val);
83
}
94

10-
pub fn at_index(vec: &Vec<String>, index: usize) -> String {
11-
vec[index].clone()
12-
}
13-
14-
#[cfg(test)]
15-
mod test {
16-
use super::*;
17-
18-
#[test]
19-
fn test_insertions() {
20-
let mut groceries = Vec::new();
21-
insert(&mut groceries, "milk".to_string());
22-
assert_eq!(groceries, ["milk"]);
23-
insert(&mut groceries, "bread".to_string());
24-
assert_eq!(groceries, ["milk", "bread"]);
25-
}
26-
27-
#[test]
28-
fn test_index() {
29-
let groceries: Vec<String> = vec![
30-
"milk".to_string(),
31-
"bread".to_string(),
32-
"water".to_string(),
33-
"wine".to_string(),
34-
];
35-
assert_eq!(at_index(&groceries, 0), "milk");
36-
}
5+
pub fn at_index(slice: &[String], index: usize) -> &str {
6+
&slice[index]
377
}

solutions/looping/src/main.rs

+12-27
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,26 @@
1-
// Write a program that prints a riddle, receives input from the user
2-
// and checks that the answer is correct
3-
4-
// The program must allow indefinite number of trials and only quit after the
5-
// correct answer is given
6-
7-
// Every time the user introduces an incorrect answer the program must
8-
// print the riddle
9-
// Before quitting the program must print the number of trials
10-
11-
// Riddle: I am the beginning of the end, and the end of time and
12-
// space. I am essential to creation, and I surround every place. What
13-
// am I?
14-
15-
// Answer: The letter e
16-
171
use std::io;
182

3+
const RIDDLE: &str = "I am the beginning of the end, and the end of time and space. I am essential to creation, and I surround every place. What am I?";
4+
const ANSWER: &str = "The letter e\n";
5+
196
fn main() {
20-
let riddle = "I am the beginning of the end, and the end of time and space. I am essential to creation, and I surround every place. What am I?";
21-
let answer = "The letter e";
22-
let mut counter = 0;
7+
let mut trials = 0;
238

24-
let trials = loop {
25-
println!("{}", riddle);
9+
loop {
10+
println!("{}", RIDDLE);
2611

2712
let mut input = String::new();
2813

2914
io::stdin()
3015
.read_line(&mut input)
31-
.expect("Failed to read line");
16+
.expect("Couldn't read line");
3217

33-
counter += 1;
18+
trials += 1;
3419

35-
if answer.to_owned() + "\n" == input {
36-
break counter;
20+
if input == ANSWER {
21+
break;
3722
}
38-
};
23+
}
3924

40-
println!("Number of trials: {}", trials);
25+
println!("Number of trials: {trials}");
4126
}

solutions/looping/tests/cli.rs

-38
This file was deleted.

solutions/matrix_transposition/src/lib.rs

-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
// Define a function that calculate the transpose matrix of a 2x2 matrix
2-
// You don't need to understand everything about matrices
3-
4-
// Just convert lines into columns and vice versa
5-
// ( a b ) __ transposition __> ( a d )
6-
// ( c d ) ( b d )
7-
8-
// Only the body of the transpose function can be changed
9-
10-
// fn main() {
11-
// let matrix = Matrix((1, 3), (4, 5));
12-
// println!("Original matrix {:?}", matrix);
13-
// println!("Transpose matrix {:?}", transpose(matrix));
14-
// }
15-
161
#[derive(Debug, PartialEq, Eq)]
172
pub struct Matrix(pub (i32, i32), pub (i32, i32));
183

solutions/reverse_string/src/lib.rs

+1-44
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,3 @@
1-
/*
2-
Write a function `rev_str` that takes a `&str` as a parameter, and returns a string with its words reversed.
3-
4-
fn main() {
5-
println!("{}", rev_str("Hello, world!"));
6-
println!("{}", rev_str("Hello, my name is Roman"));
7-
println!("{}", rev_str("I have a nice car!"));
8-
println!("{}", rev_str("How old are You"));
9-
println!("{}", rev_str("ex: this is an example água"));
10-
}
11-
*/
12-
131
pub fn rev_str(input: &str) -> String {
14-
input.chars().rev().collect::<String>()
15-
}
16-
17-
#[cfg(test)]
18-
mod tests {
19-
use super::*;
20-
21-
fn test_reverse(input: &str, expected: &str) {
22-
assert_eq!(&rev_str(input), expected);
23-
}
24-
25-
#[test]
26-
// testing just one word
27-
fn test_simple_word() {
28-
test_reverse("robot", "tobor");
29-
test_reverse("Ramen", "nemaR");
30-
test_reverse("I'm hungry!", "!yrgnuh m'I");
31-
test_reverse("racecar", "racecar");
32-
test_reverse("drawer", "reward");
33-
test_reverse("子猫", "猫子");
34-
test_reverse("", "");
35-
}
36-
37-
#[test]
38-
// testing two or more words
39-
fn test_more_than_one() {
40-
test_reverse("Hello, world!", "!dlrow ,olleH");
41-
test_reverse("Hello, my name is Roman", "namoR si eman ym ,olleH");
42-
test_reverse("I have a nice car!", "!rac ecin a evah I");
43-
test_reverse("How old are You", "uoY era dlo woH");
44-
test_reverse("ex: this is an example água", "augá elpmaxe na si siht :xe");
45-
}
2+
input.chars().rev().collect()
463
}

solutions/scalar/src/lib.rs

-62
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,3 @@
1-
/*
2-
## Scaler
3-
### Instructions
4-
Create the following functions, that receives two parameters:
5-
- sum, that returns the sum between two values from 0 to 255
6-
- diff, that returns the difference between two values from -32768 to 32767
7-
- pro, that returns the product of the multiplication between two values from -128 to 127
8-
- quo, that returns the quotient of the division between two values
9-
- rem, that returns the remainder of the division between two values
10-
### Notions
11-
- https://doc.rust-lang.org/book/ch03-02-data-types.html
12-
*/
13-
14-
// fn main() {
15-
// // sum
16-
// println!("sum : {}", sum(234, 2));
17-
// println!("sum : {}", sum(1, 255)); // 'ERROR: attempt to add with overflow'
18-
// // diff
19-
// println!("diff : {}", diff(234, 2));
20-
// println!("diff : {}", diff(-32768, 32766)); // 'ERROR: attempt to subtract with overflow'
21-
// // product
22-
// println!("pro : {}", pro(23, 2));
23-
// println!("pro : {}", pro(-128, 2)); // 'ERROR: attempt to multiply with overflow'
24-
// // quotient
25-
// println!("quo : {}", quo(22.0, 2.0));
26-
// println!("quo : {}", quo(-128.23, 2.0));
27-
// // remainder
28-
// println!("rem : {}", rem(22.0, 2.0));
29-
// println!("rem : {}", rem(-128.23, 2.0));
30-
// }
31-
321
pub fn sum(a: u8, b: u8) -> u8 {
332
a + b
343
}
@@ -44,34 +13,3 @@ pub fn quo(a: f32, b: f32) -> f32 {
4413
pub fn rem(a: f32, b: f32) -> f32 {
4514
a % b
4615
}
47-
48-
// tests
49-
#[cfg(test)]
50-
mod tests {
51-
use super::*;
52-
53-
#[test]
54-
#[should_panic]
55-
fn test_panic_sum() {
56-
sum(25, 255);
57-
}
58-
#[test]
59-
#[should_panic]
60-
fn test_panic_diff() {
61-
diff(-32768, 32766);
62-
}
63-
#[test]
64-
#[should_panic]
65-
fn test_panic_pro() {
66-
pro(-128, 2);
67-
}
68-
69-
#[test]
70-
fn pass() {
71-
assert_eq!(sum(1, 2), 3);
72-
assert_eq!(diff(1, 2), -1);
73-
assert_eq!(pro(1, 2), 2);
74-
assert_eq!(quo(1.0, 2.0), 0.5);
75-
assert_eq!(rem(1.0, 2.0), 1.0);
76-
}
77-
}

0 commit comments

Comments
 (0)