Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CON-3260 Fix rust piscine's quest-01 #144

Merged
merged 14 commits into from
Dec 10, 2024
29 changes: 0 additions & 29 deletions solutions/division_and_remainder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,5 @@
/*Change the body of the function divide to return the value of the
integer division of the x and y and the remainder of that division
You're only allowed to change the body of the function

fn main() {
let x = 9;
let y = 4;
let (division, remainder) = divide(x, y);
println!(
"{}/{}: division = {}, remainder = {}",
x, y, division, remainder
);
}
*/

pub fn divide(x: i32, y: i32) -> (i32, i32) {
// You're code here
let div = x / y;
let rem = x % y;
(div, rem)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
assert_eq!(divide(3, 4), (3 / 4, 3 % 4));
assert_eq!(divide(73, 4), (73 / 4, 73 % 4));
assert_eq!(divide(432, 32), (432 / 32, 432 % 32));
assert_eq!(divide(897, 29), (897 / 29, 897 % 29));
}
}
25 changes: 4 additions & 21 deletions solutions/fibonacci2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
// fn main() {
// let val: u32 = 20;
// println!("Fibonacci({}) = {}", val, fibonacci(val));
//}

pub fn fibonacci(n: u32) -> u32 {
if n < 2 {
return n;
}
fibonacci(n - 2) + fibonacci(n - 1)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
assert_eq!(fibonacci(0), 0);
assert_eq!(fibonacci(1), 1);
assert_eq!(fibonacci(22), 17711);
assert_eq!(fibonacci(20), 6765);
if matches!(n, 0 | 1) {
n
} else {
fibonacci(n - 2) + fibonacci(n - 1)
}
}
35 changes: 4 additions & 31 deletions solutions/find_factorial/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
// Complete this function to return the factorial of a given number
/*
fn main() {
println!("The factorial of 0 = {}", factorial(0));
println!("The factorial of 1 = {}", factorial(1));
println!("The factorial of 5 = {}", factorial(5));
println!("The factorial of 10 = {}", factorial(10));
println!("The factorial of 19 = {}", factorial(19));
}*/

pub fn factorial(num: u64) -> u64 {
match num {
0 | 1 => 1,
_ => factorial(num - 1) * num,
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn factorial_of_1() {
assert_eq!(1, factorial(0));
assert_eq!(1, factorial(1));
assert_eq!(120, factorial(5));
assert_eq!(40320, factorial(8));
assert_eq!(3628800, factorial(10));
assert_eq!(87178291200, factorial(14));
assert_eq!(6402373705728000, factorial(18));
assert_eq!(121645100408832000, factorial(19));
assert_eq!(2432902008176640000, factorial(20));
if num == 0 {
1
} else {
num * factorial(num - 1)
}
}
34 changes: 2 additions & 32 deletions solutions/groceries/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,7 @@
// # Instructions

// Create a function called `insert`
// fn insert(vec: &mut Vec<String>, val: String) that inserts a new element at the end of the Vec

pub fn insert(vec: &mut Vec<String>, val: String) {
vec.push(val);
}

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

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_insertions() {
let mut groceries = Vec::new();
insert(&mut groceries, "milk".to_string());
assert_eq!(groceries, ["milk"]);
insert(&mut groceries, "bread".to_string());
assert_eq!(groceries, ["milk", "bread"]);
}

#[test]
fn test_index() {
let groceries: Vec<String> = vec![
"milk".to_string(),
"bread".to_string(),
"water".to_string(),
"wine".to_string(),
];
assert_eq!(at_index(&groceries, 0), "milk");
}
pub fn at_index(slice: &[String], index: usize) -> &str {
&slice[index]
}
39 changes: 12 additions & 27 deletions solutions/looping/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,26 @@
// Write a program that prints a riddle, receives input from the user
// and checks that the answer is correct

// The program must allow indefinite number of trials and only quit after the
// correct answer is given

// Every time the user introduces an incorrect answer the program must
// print the riddle
// Before quitting the program must print the number of trials

// 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?

// Answer: The letter e

use std::io;

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?";
const ANSWER: &str = "The letter e\n";

fn main() {
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?";
let answer = "The letter e";
let mut counter = 0;
let mut trials = 0;

let trials = loop {
println!("{}", riddle);
loop {
println!("{}", RIDDLE);

let mut input = String::new();

io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
.expect("Couldn't read line");

counter += 1;
trials += 1;

if answer.to_owned() + "\n" == input {
break counter;
if input == ANSWER {
break;
}
};
}

println!("Number of trials: {}", trials);
println!("Number of trials: {trials}");
}
38 changes: 0 additions & 38 deletions solutions/looping/tests/cli.rs

This file was deleted.

15 changes: 0 additions & 15 deletions solutions/matrix_transposition/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
// Define a function that calculate the transpose matrix of a 2x2 matrix
// You don't need to understand everything about matrices

// Just convert lines into columns and vice versa
// ( a b ) __ transposition __> ( a d )
// ( c d ) ( b d )

// Only the body of the transpose function can be changed

// fn main() {
// let matrix = Matrix((1, 3), (4, 5));
// println!("Original matrix {:?}", matrix);
// println!("Transpose matrix {:?}", transpose(matrix));
// }

#[derive(Debug, PartialEq, Eq)]
pub struct Matrix(pub (i32, i32), pub (i32, i32));

Expand Down
45 changes: 1 addition & 44 deletions solutions/reverse_string/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,3 @@
/*
Write a function `rev_str` that takes a `&str` as a parameter, and returns a string with its words reversed.

fn main() {
println!("{}", rev_str("Hello, world!"));
println!("{}", rev_str("Hello, my name is Roman"));
println!("{}", rev_str("I have a nice car!"));
println!("{}", rev_str("How old are You"));
println!("{}", rev_str("ex: this is an example água"));
}
*/

pub fn rev_str(input: &str) -> String {
input.chars().rev().collect::<String>()
}

#[cfg(test)]
mod tests {
use super::*;

fn test_reverse(input: &str, expected: &str) {
assert_eq!(&rev_str(input), expected);
}

#[test]
// testing just one word
fn test_simple_word() {
test_reverse("robot", "tobor");
test_reverse("Ramen", "nemaR");
test_reverse("I'm hungry!", "!yrgnuh m'I");
test_reverse("racecar", "racecar");
test_reverse("drawer", "reward");
test_reverse("子猫", "猫子");
test_reverse("", "");
}

#[test]
// testing two or more words
fn test_more_than_one() {
test_reverse("Hello, world!", "!dlrow ,olleH");
test_reverse("Hello, my name is Roman", "namoR si eman ym ,olleH");
test_reverse("I have a nice car!", "!rac ecin a evah I");
test_reverse("How old are You", "uoY era dlo woH");
test_reverse("ex: this is an example água", "augá elpmaxe na si siht :xe");
}
input.chars().rev().collect()
}
62 changes: 0 additions & 62 deletions solutions/scalar/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,3 @@
/*
## Scaler
### Instructions
Create the following functions, that receives two parameters:
- sum, that returns the sum between two values from 0 to 255
- diff, that returns the difference between two values from -32768 to 32767
- pro, that returns the product of the multiplication between two values from -128 to 127
- quo, that returns the quotient of the division between two values
- rem, that returns the remainder of the division between two values
### Notions
- https://doc.rust-lang.org/book/ch03-02-data-types.html
*/

// fn main() {
// // sum
// println!("sum : {}", sum(234, 2));
// println!("sum : {}", sum(1, 255)); // 'ERROR: attempt to add with overflow'
// // diff
// println!("diff : {}", diff(234, 2));
// println!("diff : {}", diff(-32768, 32766)); // 'ERROR: attempt to subtract with overflow'
// // product
// println!("pro : {}", pro(23, 2));
// println!("pro : {}", pro(-128, 2)); // 'ERROR: attempt to multiply with overflow'
// // quotient
// println!("quo : {}", quo(22.0, 2.0));
// println!("quo : {}", quo(-128.23, 2.0));
// // remainder
// println!("rem : {}", rem(22.0, 2.0));
// println!("rem : {}", rem(-128.23, 2.0));
// }

pub fn sum(a: u8, b: u8) -> u8 {
a + b
}
Expand All @@ -44,34 +13,3 @@ pub fn quo(a: f32, b: f32) -> f32 {
pub fn rem(a: f32, b: f32) -> f32 {
a % b
}

// tests
#[cfg(test)]
mod tests {
use super::*;

#[test]
#[should_panic]
fn test_panic_sum() {
sum(25, 255);
}
#[test]
#[should_panic]
fn test_panic_diff() {
diff(-32768, 32766);
}
#[test]
#[should_panic]
fn test_panic_pro() {
pro(-128, 2);
}

#[test]
fn pass() {
assert_eq!(sum(1, 2), 3);
assert_eq!(diff(1, 2), -1);
assert_eq!(pro(1, 2), 2);
assert_eq!(quo(1.0, 2.0), 0.5);
assert_eq!(rem(1.0, 2.0), 1.0);
}
}
Loading
Loading