diff --git a/solutions/diamond_creation/src/lib.rs b/solutions/diamond_creation/src/lib.rs index ca166e54..a61aadef 100644 --- a/solutions/diamond_creation/src/lib.rs +++ b/solutions/diamond_creation/src/lib.rs @@ -1,158 +1,15 @@ -/* ## diamond_creation - -### Instructions - -Complete the function "make_diamond" that takes a letter as input, and outputs it in a diamond shape. - -Rules: - -- The first and last row contain one 'A'. -- The given letter has to be at the widest point. -- All rows, except the first and last, have exactly two identical letters. -- All rows have as many trailing spaces as leading spaces. (This might be 0). -- The diamond is vertically and horizontally symmetric. -- The diamond width equals the height. -- The top half has the letters in ascending order. (abcd) -- The bottom half has the letters in descending order. (dcba) - -### Notions - -- https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html - -### Expected functions - -```rust -fn get_diamond(c: char) -> Vec {} -``` - -### Usage - -Here is a program to test your function. - -```rust -fn main() { - println!("{:?}", make_diamond('A')); - println!("{:?}", make_diamond('C')); -} -``` - -And its output - -```console -student@ubuntu:~/[[ROOT]]/test$ cargo run -["A"] -[" A ", " B B ", "C C", " B B ", " A "] -student@ubuntu:~/[[ROOT]]/test$ -``` -*/ -use std::iter; - pub fn get_diamond(c: char) -> Vec { - let size = ((c as u8) - b'A') as usize * 2 + 1; - - let half: Vec<_> = iter::once(format!("{0:^1$}", 'A', size)) - .chain((1..=size / 2).map(|i| { - format!( - "{0:^1$}", - format!("{0}{1}{0}", (b'A' + i as u8) as char, " ".repeat(i * 2 - 1)), - size - ) - })) - .collect(); - - half.iter() - .chain(half.iter().rev().skip(1)) - .cloned() - .collect() -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_a() { - assert_eq!(get_diamond('A'), vec!["A"]); - } - - #[test] - fn test_b() { - assert_eq!(get_diamond('B'), vec![" A ", "B B", " A "]); - } + let n = ((c as u8 - b'A') * 2 + 1) as _; + let mid = (n + 1) / 2; - #[test] - fn test_c() { - assert_eq!( - get_diamond('C'), - vec![" A ", " B B ", "C C", " B B ", " A "] - ); - } + let ladder = (0..mid).map(|i| { + let mut s = vec![' '; n]; + let c = (b'A' + i as u8) as _; + s[mid - 1 - i] = c; + s[mid - 1 + i] = c; - #[test] - fn test_d() { - assert_eq!( - get_diamond('D'), - vec![" A ", " B B ", " C C ", "D D", " C C ", " B B ", " A ",] - ); - } + String::from_iter(s) + }); - #[test] - fn test_z() { - assert_eq!( - get_diamond('Z'), - vec![ - " A ", - " B B ", - " C C ", - " D D ", - " E E ", - " F F ", - " G G ", - " H H ", - " I I ", - " J J ", - " K K ", - " L L ", - " M M ", - " N N ", - " O O ", - " P P ", - " Q Q ", - " R R ", - " S S ", - " T T ", - " U U ", - " V V ", - " W W ", - " X X ", - " Y Y ", - "Z Z", - " Y Y ", - " X X ", - " W W ", - " V V ", - " U U ", - " T T ", - " S S ", - " R R ", - " Q Q ", - " P P ", - " O O ", - " N N ", - " M M ", - " L L ", - " K K ", - " J J ", - " I I ", - " H H ", - " G G ", - " F F ", - " E E ", - " D D ", - " C C ", - " B B ", - " A ", - ] - ); - } + ladder.clone().chain(ladder.rev().skip(1)).collect() } diff --git a/solutions/luhn_algorithm/src/lib.rs b/solutions/luhn_algorithm/src/lib.rs index 5ee46e91..4fa50fcc 100644 --- a/solutions/luhn_algorithm/src/lib.rs +++ b/solutions/luhn_algorithm/src/lib.rs @@ -1,24 +1,23 @@ pub fn is_luhn_formula(code: &str) -> bool { - if code.trim().len() <= 1 { - return false; + let it = code.chars().filter(|c| !c.is_ascii_whitespace()); + if it.clone().count() <= 1 { + false + } else { + it.rev() + .enumerate() + .try_fold(0, |acc, (i, c)| { + c.to_digit(10).map(|c| { + acc + if i % 2 == 1 { + if c > 4 { + c * 2 - 9 + } else { + c * 2 + } + } else { + c + } + }) + }) + .map_or(false, |n| n % 10 == 0) } - code.as_bytes() - .iter() - .rev() - .filter(|&c| *c != b' ') - .enumerate() - .try_fold(0, |acc, (i, &c)| { - if c.is_ascii_digit() { - Some( - acc + match (i % 2 == 1, c - b'0') { - (true, nb) if nb > 4 => nb * 2 - 9, - (true, nb) => nb * 2, - (false, nb) => nb, - }, - ) - } else { - None - } - }) - .map_or(false, |checksum| checksum % 10 == 0) } diff --git a/solutions/ordinal/src/lib.rs b/solutions/ordinal/src/lib.rs index 831c3c2a..a2a48724 100644 --- a/solutions/ordinal/src/lib.rs +++ b/solutions/ordinal/src/lib.rs @@ -1,34 +1,16 @@ -/* - Complete the function "num_to_ordinal" that receives a cardinal number and returns its ordinal number. - -fn main() { - println!("{}", num_to_ordinal(1)); - println!("{}", num_to_ordinal(22)); - println!("{}", num_to_ordinal(43)); - println!("{}", num_to_ordinal(47)); -} -*/ - pub fn num_to_ordinal(x: u32) -> String { format!( "{}{}", x, - match (x % 10, x % 100) { - (1, 1) | (1, 21..=91) => "st", - (2, 2) | (2, 22..=92) => "nd", - (3, 3) | (3, 23..=93) => "rd", - _ => "th", + if matches!(x % 100, 11..=13) { + "th" + } else { + match x % 10 { + 1 => "st", + 2 => "nd", + 3 => "rd", + _ => "th", + } } ) } - -#[test] -fn test_num_to_ordinal() { - assert_eq!(num_to_ordinal(0), "0th"); - assert_eq!(num_to_ordinal(1), "1st"); - assert_eq!(num_to_ordinal(12), "12th"); - assert_eq!(num_to_ordinal(22), "22nd"); - assert_eq!(num_to_ordinal(43), "43rd"); - assert_eq!(num_to_ordinal(67), "67th"); - assert_eq!(num_to_ordinal(1901), "1901st"); -} diff --git a/solutions/pangram/src/lib.rs b/solutions/pangram/src/lib.rs index 659b2344..7c802871 100644 --- a/solutions/pangram/src/lib.rs +++ b/solutions/pangram/src/lib.rs @@ -1,62 +1,3 @@ -// Determine if the string is a pangram. -// A pangram is a sentence using every letter of the alphabet at least once. -// The best known English pangram is: "The quick brown fox jumps over the lazy dog." - -use std::collections::HashSet; - pub fn is_pangram(s: &str) -> bool { - let mut letters = HashSet::new(); - - for c in s.chars().map(|c| c.to_ascii_lowercase()) { - match c { - 'a'..='z' => letters.insert(c), - _ => false, - }; - if letters.len() == 26 { - return true; - } - } - - false -} - -// fn main() { -// println!( -// "{}", -// is_pangram("the quick brown fox jumps over the lazy dog!") -// ); -// println!("{}", is_pangram("this is not a panagram!")); -// } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_empty_strings() { - assert!(!is_pangram("")); - assert!(!is_pangram(" ")); - } - - #[test] - fn test_is_panagram() { - assert!(is_pangram("the quick brown fox jumps over the lazy dog")); - assert!(is_pangram("the_quick_brown_fox_jumps_over_the_lazy_dog")); - assert!(is_pangram( - "the 1 quick brown fox jumps over the 2 lazy dogs" - )); - } - #[test] - fn test_not_pangrams() { - assert!(!is_pangram( - "a quick movement of the enemy will jeopardize five gunboats" - )); - assert!(!is_pangram("the quick brown fish jumps over the lazy dog")); - assert!(!is_pangram("the quick brown fox jumps over the lay dog")); - assert!(!is_pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")); - assert!(is_pangram("\"Five quacking Zephyrs jolt my wax bed.\"")); - assert!(is_pangram( - "Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich." - )); - } + ('a'..='z').all(|c| s.to_lowercase().contains(c)) } diff --git a/solutions/pig_latin/src/lib.rs b/solutions/pig_latin/src/lib.rs index b0baecda..e48d220b 100644 --- a/solutions/pig_latin/src/lib.rs +++ b/solutions/pig_latin/src/lib.rs @@ -1,63 +1,17 @@ -pub fn pig_latin(text: &str) -> String { - text.split_whitespace() - .map(|s| { - let mut nb_chars_to_move = 0; - for c in s.chars() { - if !is_vowel(c) { - nb_chars_to_move += 1; - } else { - break; - } - } - if nb_chars_to_move >= 2 - && nb_chars_to_move < s.len() - && s.chars().nth(nb_chars_to_move - 1) == Some('q') - && s.chars().nth(nb_chars_to_move) == Some('u') - { - nb_chars_to_move += 1; - } - format!("{}{}ay", &s[nb_chars_to_move..], &s[0..nb_chars_to_move]) - }) - .collect::>() - .join(" ") -} - -fn is_vowel(mut c: char) -> bool { - c = lowercase(c); - c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' -} +const VOWELS: &str = "aeiou"; -fn lowercase(c: char) -> char { - c.to_lowercase().to_string().chars().next().unwrap() -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_word_beginning_with_vowel() { - assert_eq!(pig_latin(&String::from("apple")), "appleay"); - assert_eq!(pig_latin(&String::from("ear")), "earay"); - assert_eq!(pig_latin(&String::from("igloo")), "iglooay"); - assert_eq!(pig_latin(&String::from("object")), "objectay"); - assert_eq!(pig_latin(&String::from("under")), "underay"); - } +pub fn pig_latin(text: &str) -> String { + let first_vowel = text + .find(|c: char| VOWELS.contains(c.to_ascii_lowercase())) + .unwrap_or(text.len()); - #[test] + let (start, rem) = text.split_at( + if first_vowel != 0 && text.len() >= 3 && &text[1..3] == "qu" { + 3 + } else { + first_vowel + }, + ); - fn test_word_beginning_with_consonant() { - assert_eq!(pig_latin(&String::from("queen")), "ueenqay"); - assert_eq!(pig_latin(&String::from("square")), "aresquay"); - assert_eq!(pig_latin(&String::from("equal")), "equalay"); - assert_eq!(pig_latin(&String::from("pig")), "igpay"); - assert_eq!(pig_latin(&String::from("koala")), "oalakay"); - assert_eq!(pig_latin(&String::from("yellow")), "ellowyay"); - assert_eq!(pig_latin(&String::from("xenon")), "enonxay"); - assert_eq!(pig_latin(&String::from("qat")), "atqay"); - assert_eq!(pig_latin(&String::from("chair")), "airchay"); - assert_eq!(pig_latin(&String::from("therapy")), "erapythay"); - assert_eq!(pig_latin(&String::from("thrush")), "ushthray"); - assert_eq!(pig_latin(&String::from("school")), "oolschay"); - } + format!("{rem}{start}ay") } diff --git a/solutions/rgb_match/src/lib.rs b/solutions/rgb_match/src/lib.rs index 170e6f28..34a8b54e 100644 --- a/solutions/rgb_match/src/lib.rs +++ b/solutions/rgb_match/src/lib.rs @@ -1,87 +1,3 @@ -/* -## rgb_match - -### Instructions - -Implement the struct `Color` with the function `swap`. -This function must allow you to swap the values of the struct. - -### Notions - -- https://doc.rust-lang.org/book/ch18-00-patterns.html - -### Expected functions - -```rust -impl Color { - fn swap(mut self, first: u8, second: u8) -> Color {} - } -``` - -### Usage - -Here is a program to test your function. - -```rust -struct Color { - r: u8, - g: u8, - b: u8, - a: u8, -} - -fn main() { - let c = Color { - r: 255, - g: 200, - b: 10, - a: 30, - }; - - println!("{:?}", c.swap(c.r, c.b)); - println!("{:?}", c.swap(c.r, c.g)); - println!("{:?}", c.swap(c.r, c.a)); - println!(); - println!("{:?}", c.swap(c.g, c.r)); - println!("{:?}", c.swap(c.g, c.b)); - println!("{:?}", c.swap(c.g, c.a)); - println!(); - println!("{:?}", c.swap(c.b, c.r)); - println!("{:?}", c.swap(c.b, c.g)); - println!("{:?}", c.swap(c.b, c.a)); - println!(); - println!("{:?}", c.swap(c.a, c.r)); - println!("{:?}", c.swap(c.a, c.b)); - println!("{:?}", c.swap(c.a, c.g)); -} -``` - -And its output - -```console -student@ubuntu:~/[[ROOT]]/test$ cargo run -Color { r: 10, g: 200, b: 255, a: 30 } -Color { r: 200, g: 255, b: 10, a: 30 } -Color { r: 30, g: 200, b: 10, a: 255 } - -Color { r: 200, g: 255, b: 10, a: 30 } -Color { r: 255, g: 10, b: 200, a: 30 } -Color { r: 255, g: 30, b: 10, a: 200 } - -Color { r: 10, g: 200, b: 255, a: 30 } -Color { r: 255, g: 10, b: 200, a: 30 } -Color { r: 255, g: 200, b: 30, a: 10 } - -Color { r: 30, g: 200, b: 10, a: 255 } -Color { r: 255, g: 200, b: 30, a: 10 } -Color { r: 255, g: 30, b: 10, a: 200 } - -student@ubuntu:~/[[ROOT]]/test$ -``` - - -*/ - #[derive(Debug, Clone, Copy, PartialEq)] pub struct Color { pub r: u8, @@ -92,208 +8,16 @@ pub struct Color { impl Color { pub fn swap(mut self, first: u8, second: u8) -> Color { - match first { - v if v == self.r => match second { - v if v == self.b => { - self.r = second; - self.b = first; - } - v if v == self.g => { - self.r = second; - self.g = first; - } - v if v == self.a => { - self.r = second; - self.a = first; - } - _ => {} - }, - - v if v == self.g => match second { - v if v == self.r => { - self.g = second; - self.r = first; - } - v if v == self.b => { - self.g = second; - self.b = first; - } - v if v == self.a => { - self.g = second; - self.a = first; - } - _ => {} - }, - - v if v == self.a => match second { - v if v == self.r => { - self.a = second; - self.r = first; - } - v if v == self.b => { - self.a = second; - self.b = first; - } - v if v == self.g => { - self.a = second; - self.g = first; + [&mut self.r, &mut self.g, &mut self.b, &mut self.a] + .into_iter() + .for_each(|v| { + if *v == first { + *v = second + } else if *v == second { + *v = first; } - _ => {} - }, - - v if v == self.b => match second { - v if v == self.r => { - self.b = second; - self.r = first; - } - v if v == self.g => { - self.b = second; - self.g = first; - } - v if v == self.a => { - self.b = second; - self.a = first; - } - _ => {} - }, - _ => {} - } - - Color { - r: self.r, - g: self.g, - b: self.b, - a: self.a, - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_one() { - let c = Color { - r: 255, - g: 200, - b: 10, - a: 30, - }; - // swap r - assert_eq!( - c.swap(c.r, c.b), - Color { - r: 10, - g: 200, - b: 255, - a: 30 - } - ); - assert_eq!( - c.swap(c.r, c.g), - Color { - r: 200, - g: 255, - b: 10, - a: 30 - } - ); - assert_eq!( - c.swap(c.r, c.a), - Color { - r: 30, - g: 200, - b: 10, - a: 255 - } - ); - - // swap g - assert_eq!( - c.swap(c.g, c.r), - Color { - r: 200, - g: 255, - b: 10, - a: 30 - } - ); - assert_eq!( - c.swap(c.g, c.b), - Color { - r: 255, - g: 10, - b: 200, - a: 30 - } - ); - assert_eq!( - c.swap(c.g, c.a), - Color { - r: 255, - g: 30, - b: 10, - a: 200 - } - ); - - // swap b - assert_eq!( - c.swap(c.b, c.r), - Color { - r: 10, - g: 200, - b: 255, - a: 30 - } - ); - assert_eq!( - c.swap(c.b, c.g), - Color { - r: 255, - g: 10, - b: 200, - a: 30 - } - ); - assert_eq!( - c.swap(c.b, c.a), - Color { - r: 255, - g: 200, - b: 30, - a: 10 - } - ); + }); - // swap a - assert_eq!( - c.swap(c.a, c.r), - Color { - r: 30, - g: 200, - b: 10, - a: 255 - } - ); - assert_eq!( - c.swap(c.a, c.b), - Color { - r: 255, - g: 200, - b: 30, - a: 10 - } - ); - assert_eq!( - c.swap(c.a, c.g), - Color { - r: 255, - g: 30, - b: 10, - a: 200 - } - ); + self } } diff --git a/solutions/rot/src/lib.rs b/solutions/rot/src/lib.rs index 79a0048a..58850f48 100644 --- a/solutions/rot/src/lib.rs +++ b/solutions/rot/src/lib.rs @@ -1,133 +1,17 @@ -/* -## rotate - -### Instructions - -By now you will have the knowledge of the so called rotational cipher "ROT13". - -A ROT13 on the Latin alphabet would be as follows: - -- Plain: abcdefghijklmnopqrstuvwxyz - -- Cipher: nopqrstuvwxyzabcdefghijklm - -Your purpose in this exercise is to create a similar `rotate` function that is a better version of the ROT13 cipher. -Your function will receive a string and a number and it will rotate each letter of that string, the number of times, settled by the second argument, to the right or to the left if the number are negative. - -Your function should only change letters. If the string includes punctuation and numbers -they will remain the same. - -### Notions - -- https://doc.rust-lang.org/book/ch18-00-patterns.html - -### Expected functions - -```rust -fn rotate(input: &str, key: i8) -> String {} -``` - -### Usage - -Here is a program to test your function. - -```rust -fn main() { - - println!("The letter \"a\" becomes: {}", rotate("a", 26)); - println!("The letter \"m\" becomes: {}", rotate("m", 0)); - println!("The letter \"m\" becomes: {}", rotate("m", 13)); - println!("The letter \"a\" becomes: {}", rotate("a", 15)); - println!("The word \"MISS\" becomes: {}", rotate("MISS", 5)); - println!( - "The decoded message is: {}", - rotate("Gur svir obkvat jvmneqf whzc dhvpxyl.", 13) - ); - println!( - "The decoded message is: {}", - rotate("Mtb vznhpqd ifky ozrunsl ejgwfx ajc", 5) - ); - println!( - "Your cypher wil be: {}", - rotate("Testing with numbers 1 2 3", 4) - ); - println!("Your cypher wil be: {}", rotate("Testing", -14)); - println!("The letter \"a\" becomes: {}", rotate("a", -1)); -} -``` - -And its output - -```console -student@ubuntu:~/[[ROOT]]/test$ cargo run -The letter "a" becomes: a -The letter "m" becomes: m -The letter "m" becomes: z -The letter "a" becomes: p -The word "MISS" becomes: RNXX -The decoded message is: The five boxing wizards jump quickly. -The decoded message is: Ryg aesmuvi nkpd tewzsxq jolbkc foh -Your cypher wil be: Xiwxmrk amxl ryqfivw 1 2 3 -Your cypher wil be: Fqefuzs -The letter "a" becomes: z - -student@ubuntu:~/[[ROOT]]/test$ -``` - -*/ +const ALPHABET_LEN: i8 = (b'Z' - b'A' + 1) as _; pub fn rotate(input: &str, key: i8) -> String { input .chars() - .map(|character| match character { - 'a'..='z' => { - ((((character as u8) - b'a') as i8 + key).rem_euclid(26) as u8 + b'a') as char - } - 'A'..='Z' => { - ((((character as u8) - b'A') as i8 + key).rem_euclid(26) as u8 + b'A') as char + .map(|c| { + if !c.is_ascii_alphabetic() { + c + } else { + let lower_bound = if c.is_ascii_lowercase() { b'a' } else { b'A' }; + + (((c as u8 - lower_bound) as i8 + key).rem_euclid(ALPHABET_LEN) as u8 + lower_bound) + as _ } - _ => character, }) - .collect::() -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_simple() { - assert_eq!("z", rotate("m", 13)); - assert_eq!("n", rotate("m", 1)); - assert_eq!("a", rotate("a", 26)); - assert_eq!("z", rotate("a", 25)); - assert_eq!("b", rotate("l", 16)); - assert_eq!("j", rotate("j", 0)); - assert_eq!("RNXX", rotate("MISS", 5)); - assert_eq!("M J Q Q T", rotate("H E L L O", 5)); - } - - #[test] - fn test_all_letters() { - assert_eq!( - "Gur svir obkvat jvmneqf whzc dhvpxyl.", - rotate("The five boxing wizards jump quickly.", 13) - ); - } - - #[test] - fn test_numbers_punctuation() { - assert_eq!( - "Xiwxmrk amxl ryqfivw 1 2 3", - rotate("Testing with numbers 1 2 3", 4) - ); - assert_eq!("Gzo\'n zvo, Bmviyhv!", rotate("Let\'s eat, Grandma!", 21)); - } - - #[test] - fn test_negative() { - assert_eq!("z", rotate("a", -1)); - assert_eq!("W", rotate("A", -4)); - assert_eq!("Fqefuzs", rotate("Testing", -14)); - } + .collect() } diff --git a/solutions/scores/src/lib.rs b/solutions/scores/src/lib.rs index e3ee7fea..acf12309 100644 --- a/solutions/scores/src/lib.rs +++ b/solutions/scores/src/lib.rs @@ -1,59 +1,3 @@ -/* -## scores - -### Instructions - -Lets play a little! -Create a function `score` that given a string, computes the score for that given string. - -Each letter has their value, you just have to sum the values of the letters in the -given string. - -You'll need these: - -| Letter | Value | -| ---------------------------- | :---: | -| A, E, I, O, U, L, N, R, S, T | 1 | -| D, G | 2 | -| B, C, M, P | 3 | -| F, H, V, W, Y | 4 | -| K | 5 | -| J, X | 8 | -| Q, Z | 10 | - -### Notions - -- https://doc.rust-lang.org/book/ch18-00-patterns.html - -### Expected functions - -```rust -fn score(word: &str) -> u64 {} -``` - -### Usage - -Here is a program to test your function. - -```rust -fn main() { - println!("{}", score("a")); - println!("{}", score("ã ê Á?")); - println!("{}", score("ThiS is A Test")); -} -``` - -And its output - -```console -student@ubuntu:~/[[ROOT]]/test$ cargo run -1 -0 -14 -student@ubuntu:~/[[ROOT]]/test$ -``` -*/ - pub fn score(word: &str) -> u64 { word.chars() .map(|x| match x.to_ascii_lowercase() { @@ -68,37 +12,3 @@ pub fn score(word: &str) -> u64 { }) .sum() } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_simple() { - assert_eq!(score("a"), 1); - assert_eq!(score("A"), 1); - assert_eq!(score("h"), 4); - assert_eq!(score("at"), 2); - assert_eq!(score("Yes"), 6); - assert_eq!(score("cellphones"), 17); - } - - #[test] - fn test_empty() { - assert_eq!(score(""), 0); - assert_eq!(score(" "), 0); - } - - #[test] - fn test_special() { - assert_eq!(score("in Portugal NÃO stands for: no"), 30); - assert_eq!(score("This is a test, comparação, têm Água?"), 36); - } - - #[test] - fn test_long() { - assert_eq!(score("ThiS is A Test"), 14); - assert_eq!(score("long sentences are working"), 34); - assert_eq!(score("abcdefghijklmnopqrstuvwxyz"), 87); - } -} diff --git a/solutions/scytale_cipher/src/lib.rs b/solutions/scytale_cipher/src/lib.rs index 5260591a..a006c60d 100644 --- a/solutions/scytale_cipher/src/lib.rs +++ b/solutions/scytale_cipher/src/lib.rs @@ -1,21 +1,23 @@ -pub fn scytale_cipher(s: String, i: u32) -> String { - if i as usize >= s.chars().count() || i == 1 { - return s.to_string(); - } - - let width = (s.chars().count() as f64 / i as f64).ceil() as usize; - let mut table = vec![vec![' '; width]; i as usize]; +use std::iter; - for (pos, element) in s.chars().enumerate() { - let col = pos % i as usize; - let row = pos / i as usize; +pub fn scytale_cipher(message: &str, i: usize) -> String { + let i = i.min(message.len()); + if message.is_empty() || i == 0 { + String::new() + } else { + let cols = message.len().div_ceil(i); - table[col][row] = element; + (0..i) + .map(|n| { + message + .chars() + .skip(n) + .step_by(i) + .chain(iter::repeat_n(' ', cols - (message.len() - n).div_ceil(i))) + .collect::() + }) + .collect::() + .trim_end() + .to_owned() } - table - .iter() - .flatten() - .collect::() - .trim_end() - .to_string() } diff --git a/solutions/searching/src/lib.rs b/solutions/searching/src/lib.rs index 696b5077..bc9664b3 100644 --- a/solutions/searching/src/lib.rs +++ b/solutions/searching/src/lib.rs @@ -1,84 +1,3 @@ -// In this exercise you will have to create your own `search` function. -// `search` receives an array of i32 and a key, then it will return the position -// of the given key in the array. - pub fn search(array: &[i32], key: i32) -> Option { - let array = array.as_ref(); - - if array.is_empty() { - return None; - } - - let mut left: usize = 0; - let mut right: usize = array.len(); - - while left <= right { - let middle: usize = (left + right) / 2; - - let element = array.get(middle)?; // ? is used to fast error handling - if key < *element { - right = middle.checked_sub(1)?; // sub two num, checks underflow. If underflow, (None) is returned. - } else if key > *element { - left = middle + 1; - } else { - return Some(middle); - } - } - - return None; -} - -// fn main() { -// let ar = [1, 3, 4, 6, 8, 9, 11]; -// let f = search(&ar, 6); -// println!( -// "the element 6 is in the position {:?} in the array {:?}", -// f, ar -// ); -// } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_search_a_value_in_an_array() { - assert_eq!(search(&[6], 6), Some(0)); - assert_eq!(search(&[1, 2], 1), Some(0)); - assert_eq!(search(&[1, 2], 2), Some(1)); - } - #[test] - fn test_middle_of_an_array() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 6), Some(3)); - } - - #[test] - fn test_beginning_of_an_array() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 1), Some(0)); - } - - #[test] - fn test_end_of_an_array() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 11), Some(6)); - } - - #[test] - fn test_long_array() { - assert_eq!( - search(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), - Some(9) - ); - assert_eq!( - search(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21), - Some(5) - ); - } - - #[test] - fn test_value_is_not_included() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 7), None); - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 0), None); - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 13), None); - assert_eq!(search(&[], 1), None); - } + array.iter().copied().rposition(|e| e == key) } diff --git a/solutions/spelling/src/lib.rs b/solutions/spelling/src/lib.rs index e7f1f8a0..5fbd0b09 100644 --- a/solutions/spelling/src/lib.rs +++ b/solutions/spelling/src/lib.rs @@ -1,170 +1,88 @@ -/* -## spelling - -### Instructions - -In this exercise a number between 0 and 1000000 will be generated. -Your purpose is to create the function `spell` that will spell the numbers generated. - -So, if the program generates the number: - -- 1 your function will return the string "one" -- 14 your function will return the string "fourteen". -- 96 your function will return the string "ninety-six" -- 100 your function will return the string "one hundred". -- 101 your function will return the string "one hundred one" -- 348 your function will return the string "one hundred twenty-three" -- 1002 your function will return the string "one thousand two". -- 1000000 your function will return the string "one million" - -### Notions - -- https://doc.rust-lang.org/book/ch18-00-patterns.html - -### Expected functions - -```rust -pub fn spell(n: u64) -> String {} -``` - -### Usage - -Here is a program to test your function. - -```rust -fn main() { - println!("{}", spell(348)); - println!("{}", spell(9996)); +const SMALL_CASES: &[(u64, &str)] = &[ + (1, "one"), + (2, "two"), + (3, "three"), + (4, "four"), + (5, "five"), + (6, "six"), + (7, "seven"), + (8, "eight"), + (9, "nine"), + (10, "ten"), + (11, "eleven"), + (12, "twelve"), + (13, "thirteen"), + (14, "fourteen"), + (15, "fifteen"), + (16, "fifteen"), + (17, "seventeen"), + (18, "eighteen"), + (19, "nineteen"), + (20, "twenty"), + (30, "thirty"), + (40, "forty"), + (50, "fifty"), + (60, "sixty"), + (70, "seventy"), + (80, "eighty"), + (90, "ninety"), +]; + +fn dozens(n: u64) -> Option { + if n == 0 { + None + } else { + Some( + SMALL_CASES + .iter() + .copied() + .find(|&(k, _)| k == n) + .map(|(_, v)| v.to_owned()) + .unwrap_or_else(|| { + let rem = n % 10; + format!("{}-{}", dozens(n - rem).unwrap(), dozens(rem).unwrap()) + }), + ) + } } -``` -And its output +fn greatness( + n: u64, + greatness: (u64, &str), + fn_before: fn(u64) -> Option, +) -> Option { + if n < greatness.0 { + return fn_before(n); + } -```console -student@ubuntu:~/[[ROOT]]/test$ cargo run -three hundred forty-eight -nine thousand nine hundred ninety-six -student@ubuntu:~/[[ROOT]]/test$ -``` - */ -//use rand::Rng; + let hundred = n / greatness.0; -pub fn spell(n: u64) -> String { - match n { - 0..=99 => spells_below_100(n), - 100..=999 => spells_hundreds(n), - _ => spells_bignum(n), - } + Some(if let Some(rest) = fn_before(n % greatness.0) { + format!("{} {} {}", fn_before(hundred).unwrap(), greatness.1, rest) + } else { + format!("{} {}", fn_before(hundred).unwrap(), greatness.1) + }) } -pub fn spells_below_100(n: u64) -> String { - match n { - 0 => "zero".to_string(), - 1 => "one".to_string(), - 2 => "two".to_string(), - 3 => "three".to_string(), - 4 => "four".to_string(), - 5 => "five".to_string(), - 6 => "six".to_string(), - 7 => "seven".to_string(), - 8 => "eight".to_string(), - 9 => "nine".to_string(), - 10 => "ten".to_string(), - 11 => "eleven".to_string(), - 12 => "twelve".to_string(), - 13 => "thirteen".to_string(), - 14 => "fourteen".to_string(), - 15 => "fifteen".to_string(), - 16 => "fifteen".to_string(), - 17 => "seventeen".to_string(), - 18 => "eighteen".to_string(), - 19 => "nineeen".to_string(), - 20 => "twenty".to_string(), - 30 => "thirty".to_string(), - 40 => "forty".to_string(), - 50 => "fifty".to_string(), - 60 => "sixty".to_string(), - 70 => "seventy".to_string(), - 80 => "eighty".to_string(), - 90 => "ninety".to_string(), - _ => { - let rem = n % 10; - format!("{}-{}", spells_below_100(n - rem), spells_below_100(rem)) - } - } +#[inline] +fn hundreds(n: u64) -> Option { + greatness(n, (100, "hundred"), dozens) } -pub fn spells_hundreds(n: u64) -> String { - let div = n / 100; - let rem = n % 100; - let mut enc_str = format!("{} hundred", spells_below_100(div)); - if rem != 0 { - enc_str = format!("{} {}", enc_str, spells_below_100(rem)); - } - enc_str +#[inline] +fn thousands(n: u64) -> Option { + greatness(n, (1000, "thousand"), hundreds) } -pub fn spells_bignum(n: u64) -> String { - let mut enc_chunks: Vec = vec![]; - let mut chunks: Vec = vec![0; 7]; - let mut m = n; - for e in chunks.iter_mut() { - let rem = m % 1_000; - m = m / 1_000; - *e += rem; - } - for (idx, chunk) in chunks.into_iter().enumerate() { - let substr = match idx { - 0 => "", - 1 => "thousand", - 2 => "million", - 3 => "billion", - 4 => "trillion", - 5 => "quadrillion", - _ => "quintillion", - }; - if chunk != 0 { - enc_chunks.push(format!("{} {}", spell(chunk), substr).trim().to_string()); - } - } - enc_chunks.reverse(); - enc_chunks.join(" ") +#[inline] +fn millions(n: u64) -> Option { + greatness(n, (1_000_000, "million"), thousands) } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_one() { - assert_eq!(spell(0), String::from("zero")); - assert_eq!(spell(1), String::from("one")); - assert_eq!(spell(14), String::from("fourteen")); - assert_eq!(spell(20), String::from("twenty")); - assert_eq!(spell(22), String::from("twenty-two")); - assert_eq!(spell(101), String::from("one hundred one")); - assert_eq!(spell(120), String::from("one hundred twenty")); - assert_eq!(spell(123), String::from("one hundred twenty-three")); - assert_eq!(spell(1000), String::from("one thousand")); - assert_eq!(spell(1055), String::from("one thousand fifty-five")); - assert_eq!( - spell(1234), - String::from("one thousand two hundred thirty-four") - ); - assert_eq!( - spell(10123), - String::from("ten thousand one hundred twenty-three") - ); - assert_eq!( - spell(910112), - String::from("nine hundred ten thousand one hundred twelve") - ); - assert_eq!( - spell(651123), - String::from("six hundred fifty-one thousand one hundred twenty-three") - ); - - assert_eq!(spell(810000), String::from("eight hundred ten thousand")); - assert_eq!(spell(1000000), String::from("one million")); +pub fn spell(n: u64) -> String { + if n == 0 { + "zero".to_owned() + } else { + millions(n).unwrap() } } diff --git a/solutions/stars/src/lib.rs b/solutions/stars/src/lib.rs index 51432f3f..41f3f11b 100644 --- a/solutions/stars/src/lib.rs +++ b/solutions/stars/src/lib.rs @@ -1,65 +1,3 @@ -/* -## stars - -### Instructions - -Write a function named `stars` that takes a number as -parameter and returns a string of stars (asterisks) 2^n long (2 to the nth power). - -### Expected functions - -```rust -fn stars(n: u32) -> String {} -``` - -### Usage - -Here is a program to test your function. - -```rust -fn main() { - println!("{}", stars(1)); - println!("{}", stars(4)); - println!("{}", stars(5)); -} -``` - -And its output - -```console -student@ubuntu:~/[[ROOT]]/test$ cargo run -** -**************** -******************************** -student@ubuntu:~/[[ROOT]]/test$ -``` - -*/ pub fn stars(n: u32) -> String { - let base: u32 = 2; - let mut f = 0; - let mut l = "".to_string(); - while f != base.pow(n) { - l += "*"; - f += 1 - } - return l; -} - -#[cfg(test)] -mod test { - use super::*; - #[test] - fn test_stars() { - assert_eq!(stars(0), "*"); - assert_eq!(stars(1), "**"); - assert_eq!(stars(2), "****"); - assert_eq!(stars(3), "********"); - assert_eq!(stars(4), "****************"); - assert_eq!(stars(5), "********************************"); - assert_eq!( - stars(10), - "****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************" - ); - } + "*".repeat(2usize.pow(n)) } diff --git a/solutions/talking/src/lib.rs b/solutions/talking/src/lib.rs index 4679f817..f5cccd0a 100644 --- a/solutions/talking/src/lib.rs +++ b/solutions/talking/src/lib.rs @@ -1,137 +1,16 @@ -/* -## talking - -### Instructions - -Build the function `talking` that will allow you to talk with your computer. - -His answers will be created by you following the rules below. - -- He answers "There is no need to yell, calm down!" if you yell at him, for example "LEAVE ME ALONE!" -(it is consider yelling when the sentence is all written in capital letters). -- He answers "Sure" if you ask him something without yelling, for example "Is everything ok with you?" -- He answers "Quiet, I am thinking!" if you yell a question at him. "HOW ARE YOU?" -- He says "Just say something!" if you address him without actually saying anything. -- He answers "Interesting" to anything else. - -### Notions - -- https://doc.rust-lang.org/book/ch18-00-patterns.html - -### Expected functions - -```rust -fn talking(text: &str) -> &str {} -``` - -### Usage - -Here is a program to test your function. - -```rust -fn main() { - println!("{:?}", talking("JUST DO IT!")); - println!("{:?}", talking("Hello how are you?")); - println!("{:?}", talking("WHAT'S GOING ON?")); - println!("{:?}", talking("something")); - println!("{:?}", talking("")); -} -``` - -And its output - -```console -student@ubuntu:~/[[ROOT]]/test$ cargo run -"There is no need to yell, calm down!" -"Sure." -"Quiet, I am thinking!" -"Interesting" -"Just say something!" -student@ubuntu:~/[[ROOT]]/test$ -``` -*/ - pub fn talking(text: &str) -> &str { - let trimmed = text.trim(); - - if trimmed.is_empty() { - return "Just say something!"; - } - - let is_yelling = trimmed[..trimmed.len() - 1].chars().all(is_uppercase) - && trimmed.chars().any(char::is_alphabetic); - let is_question = match trimmed.chars().last() { - Some('?') => true, - _ => false, - }; - - if is_question && is_yelling { - "Quiet, I am thinking!" - } else if is_question { + if text.chars().all(|c| !c.is_ascii_lowercase()) && text.chars().any(|c| c.is_ascii_uppercase()) + { + if text.ends_with('?') { + "Quiet, I am thinking!" + } else { + "There is no need to yell, calm down!" + } + } else if text.ends_with('?') { "Sure." - } else if is_yelling { - "There is no need to yell, calm down!" + } else if text.trim().is_empty() { + "Just say something!" } else { "Interesting" } } - -pub fn is_uppercase(c: char) -> bool { - !c.is_alphabetic() || c.is_uppercase() -} - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn test_yell() { - assert_eq!( - talking("JUST DO IT!"), - "There is no need to yell, calm down!" - ); - assert_eq!( - talking("1, 2, 3 GO!"), - "There is no need to yell, calm down!" - ); - assert_eq!( - talking("I LOVE YELLING"), - "There is no need to yell, calm down!" - ); - assert_eq!( - talking("WJDAGSAF ASVF EVA VA"), - "There is no need to yell, calm down!" - ); - } - - #[test] - fn test_question() { - assert_eq!(talking("Hello how are you?"), "Sure."); - assert_eq!(talking("Are you going to be OK?"), "Sure."); - assert_eq!(talking("7?"), "Sure."); - assert_eq!(talking("Like 15?"), "Sure."); - } - - #[test] - fn test_question_yelling() { - assert_eq!(talking("WHAT'S GOING ON?"), "Quiet, I am thinking!"); - assert_eq!(talking("ARE YOU FINISHED?"), "Quiet, I am thinking!"); - assert_eq!(talking("WHAT DID I DO?"), "Quiet, I am thinking!"); - assert_eq!(talking("ARE YOU COMING?"), "Quiet, I am thinking!"); - } - - #[test] - fn test_interesting() { - assert_eq!(talking("something"), "Interesting"); - assert_eq!(talking("Wow that's good!"), "Interesting"); - assert_eq!(talking("Run far"), "Interesting"); - assert_eq!(talking("1 2 3 go!"), "Interesting"); - assert_eq!(talking("This is not ? a question."), "Interesting"); - } - - #[test] - fn test_empty() { - assert_eq!(talking(""), "Just say something!"); - assert_eq!(talking(" "), "Just say something!"); - assert_eq!(talking(" "), "Just say something!"); - } -} diff --git a/tests/diamond_creation_test/src/lib.rs b/tests/diamond_creation_test/src/lib.rs new file mode 100644 index 00000000..2748fb2f --- /dev/null +++ b/tests/diamond_creation_test/src/lib.rs @@ -0,0 +1,87 @@ +use diamond_creation::*; + +#[test] +fn test_a() { + assert_eq!(get_diamond('A'), vec!["A"]); +} + +#[test] +fn test_b() { + assert_eq!(get_diamond('B'), vec![" A ", "B B", " A "]); +} + +#[test] +fn test_c() { + assert_eq!( + get_diamond('C'), + vec![" A ", " B B ", "C C", " B B ", " A "] + ); +} + +#[test] +fn test_d() { + assert_eq!( + get_diamond('D'), + vec![" A ", " B B ", " C C ", "D D", " C C ", " B B ", " A ",] + ); +} + +#[test] +fn test_z() { + assert_eq!( + get_diamond('Z'), + vec![ + " A ", + " B B ", + " C C ", + " D D ", + " E E ", + " F F ", + " G G ", + " H H ", + " I I ", + " J J ", + " K K ", + " L L ", + " M M ", + " N N ", + " O O ", + " P P ", + " Q Q ", + " R R ", + " S S ", + " T T ", + " U U ", + " V V ", + " W W ", + " X X ", + " Y Y ", + "Z Z", + " Y Y ", + " X X ", + " W W ", + " V V ", + " U U ", + " T T ", + " S S ", + " R R ", + " Q Q ", + " P P ", + " O O ", + " N N ", + " M M ", + " L L ", + " K K ", + " J J ", + " I I ", + " H H ", + " G G ", + " F F ", + " E E ", + " D D ", + " C C ", + " B B ", + " A ", + ] + ); +} diff --git a/tests/diamond_creation_test/src/main.rs b/tests/diamond_creation_test/src/main.rs deleted file mode 100644 index 35b293d6..00000000 --- a/tests/diamond_creation_test/src/main.rs +++ /dev/null @@ -1,149 +0,0 @@ -/* -## diamond_creation - -### Instructions - -Complete the function "get_diamond" that takes a letter as input, and outputs it in a diamond shape. - -Rules: - - The first and last row contain one 'A'. - The given letter has to be at the widest point. - All rows, except the first and last, have exactly two identical letters. - All rows have as many trailing spaces as leading spaces. (This might be 0). - The diamond is vertically and horizontally symmetric. - The diamond width equals the height. - The top half has the letters in ascending order. (abcd) - The bottom half has the letters in descending order. (dcba) - -### Example: - -In the following examples, spaces are indicated by "·" - -EX: letter 'A': - -A - -EX: letter 'B': - -.A. -B.B -.A. - -EX: letter 'C': - -··A·· -·B·B· -C···C -·B·B· -··A·· - -EX: letter 'E': - -····A···· -···B·B··· -··C···C·· -·D·····D· -E·······E -·D·····D· -··C···C·· -···B·B··· -····A···· - -*/ -use diamond_creation::*; -fn main() { - println!("{:?}", get_diamond('A')); - println!("{:?}", get_diamond('C')); -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_a() { - assert_eq!(get_diamond('A'), vec!["A"]); - } - - #[test] - fn test_b() { - assert_eq!(get_diamond('B'), vec![" A ", "B B", " A "]); - } - - #[test] - fn test_c() { - assert_eq!( - get_diamond('C'), - vec![" A ", " B B ", "C C", " B B ", " A "] - ); - } - - #[test] - fn test_d() { - assert_eq!( - get_diamond('D'), - vec![" A ", " B B ", " C C ", "D D", " C C ", " B B ", " A ",] - ); - } - - #[test] - fn test_z() { - assert_eq!( - get_diamond('Z'), - vec![ - " A ", - " B B ", - " C C ", - " D D ", - " E E ", - " F F ", - " G G ", - " H H ", - " I I ", - " J J ", - " K K ", - " L L ", - " M M ", - " N N ", - " O O ", - " P P ", - " Q Q ", - " R R ", - " S S ", - " T T ", - " U U ", - " V V ", - " W W ", - " X X ", - " Y Y ", - "Z Z", - " Y Y ", - " X X ", - " W W ", - " V V ", - " U U ", - " T T ", - " S S ", - " R R ", - " Q Q ", - " P P ", - " O O ", - " N N ", - " M M ", - " L L ", - " K K ", - " J J ", - " I I ", - " H H ", - " G G ", - " F F ", - " E E ", - " D D ", - " C C ", - " B B ", - " A ", - ] - ); - } -} diff --git a/tests/luhn_algorithm_test/src/lib.rs b/tests/luhn_algorithm_test/src/lib.rs new file mode 100644 index 00000000..ca2a3682 --- /dev/null +++ b/tests/luhn_algorithm_test/src/lib.rs @@ -0,0 +1,62 @@ +use std::ops::Not; + +use luhn_algorithm::*; + +#[test] +fn test_subject_examples() { + assert!(is_luhn_formula("").not(), "An empty string is not valid"); + assert!(is_luhn_formula("1").not(), "1 is not valid"); + assert!(is_luhn_formula("79927398713"), "79927398713 is valid"); +} + +#[test] +fn test_valid_numbers() { + assert!( + is_luhn_formula("4532015112830366"), + "4532015112830366 is valid" + ); + assert!( + is_luhn_formula("6011 1111 1111 1117"), + "6011 1111 1111 1117 is valid" + ); + assert!( + is_luhn_formula("371449635398431"), + "371449635398431 is valid" + ); + assert!( + is_luhn_formula(" 5555555555554444"), + " 5555555555554444 is valid" + ); + assert!( + is_luhn_formula("79927398713 8336263 "), + "79927398713 8336263 is valid" + ); +} + +#[test] +fn test_invalid_numbers() { + assert!( + is_luhn_formula("1234567890123456").not(), + "1234567890123456 is not valid" + ); + assert!( + is_luhn_formula("a6011 1111 1111 1117x").not(), + "a6011 1111 1111 1117x is not valid" + ); + assert!( + is_luhn_formula("4444333322221110").not(), + "4444333322221110 is not valid" + ); + assert!( + is_luhn_formula("9876543210987654").not(), + "9876543210987654 is not valid" + ); + assert!( + is_luhn_formula("8765432198765432").not(), + "8765432198765432 is not valid" + ); + assert!( + is_luhn_formula("1111222233334445").not(), + "1111222233334445 is not valid" + ); +} diff --git a/tests/luhn_algorithm_test/src/main.rs b/tests/luhn_algorithm_test/src/main.rs deleted file mode 100644 index 80809b43..00000000 --- a/tests/luhn_algorithm_test/src/main.rs +++ /dev/null @@ -1,40 +0,0 @@ -use luhn_algorithm::*; - -fn main() { - println!("{}", is_luhn_formula("")); - println!("{}", is_luhn_formula("1")); - println!("{}", is_luhn_formula("79927398713")); - println!("{}", is_luhn_formula("7992 7398 713")); - println!("{}", is_luhn_formula("1234567890123456")); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_subject_examples() { - assert!(is_luhn_formula("") == false, "An empty string is not valid"); - assert!(is_luhn_formula("1") == false, "1 is not valid"); - assert!(is_luhn_formula("79927398713") == true, "79927398713 is valid"); - } - - #[test] - fn test_valid_numbers() { - assert!(is_luhn_formula("4532015112830366") == true, "4532015112830366 is valid"); - assert!(is_luhn_formula("6011 1111 1111 1117") == true, "6011 1111 1111 1117 is valid"); - assert!(is_luhn_formula("371449635398431") == true, "371449635398431 is valid"); - assert!(is_luhn_formula(" 5555555555554444") == true, " 5555555555554444 is valid"); - assert!(is_luhn_formula("79927398713 8336263 ") == true, "79927398713 8336263 is valid"); - } - - #[test] - fn test_invalid_numbers() { - assert!(is_luhn_formula("1234567890123456") == false, "1234567890123456 is not valid"); - assert!(is_luhn_formula("a6011 1111 1111 1117x") == false, "a6011 1111 1111 1117x is not valid"); - assert!(is_luhn_formula("4444333322221110") == false, "4444333322221110 is not valid"); - assert!(is_luhn_formula("9876543210987654") == false, "9876543210987654 is not valid"); - assert!(is_luhn_formula("8765432198765432") == false, "8765432198765432 is not valid"); - assert!(is_luhn_formula("1111222233334445") == false, "1111222233334445 is not valid"); - } -} diff --git a/tests/ordinal_test/src/main.rs b/tests/ordinal_test/src/lib.rs similarity index 52% rename from tests/ordinal_test/src/main.rs rename to tests/ordinal_test/src/lib.rs index 37defdc2..818d0c5a 100644 --- a/tests/ordinal_test/src/main.rs +++ b/tests/ordinal_test/src/lib.rs @@ -1,27 +1,19 @@ -/* -## ordinal - -### Instructions - -Complete the function "num_to_ordinal" that receives a cardinal number and returns its ordinal number. - -*/ use ordinal::*; -fn main() { - println!("{}", num_to_ordinal(1)); - println!("{}", num_to_ordinal(22)); - println!("{}", num_to_ordinal(43)); - println!("{}", num_to_ordinal(47)); -} - #[test] fn test_num_to_ordinal() { assert_eq!(num_to_ordinal(0), "0th"); assert_eq!(num_to_ordinal(1), "1st"); + assert_eq!(num_to_ordinal(11), "11th"); assert_eq!(num_to_ordinal(12), "12th"); + assert_eq!(num_to_ordinal(13), "13th"); + assert_eq!(num_to_ordinal(14), "14th"); assert_eq!(num_to_ordinal(22), "22nd"); assert_eq!(num_to_ordinal(43), "43rd"); assert_eq!(num_to_ordinal(67), "67th"); + assert_eq!(num_to_ordinal(113), "113th"); + assert_eq!(num_to_ordinal(114), "114th"); assert_eq!(num_to_ordinal(1901), "1901st"); + assert_eq!(num_to_ordinal(1113), "1113th"); + assert_eq!(num_to_ordinal(11111), "11111th"); } diff --git a/tests/pangram_test/src/lib.rs b/tests/pangram_test/src/lib.rs new file mode 100644 index 00000000..89ead8e0 --- /dev/null +++ b/tests/pangram_test/src/lib.rs @@ -0,0 +1,29 @@ +use pangram::*; + +#[test] +fn test_empty_strings() { + assert!(!is_pangram("")); + assert!(!is_pangram(" ")); +} + +#[test] +fn test_is_pangram() { + assert!(is_pangram("the quick brown fox jumps over the lazy dog")); + assert!(is_pangram("the_quick_brown_fox_jumps_over_the_lazy_dog")); + assert!(is_pangram( + "the 1 quick brown fox jumps over the 2 lazy dogs" + )); +} +#[test] +fn test_not_pangram() { + assert!(!is_pangram( + "a quick movement of the enemy will jeopardize five gunboats" + )); + assert!(!is_pangram("the quick brown fish jumps over the lazy dog")); + assert!(!is_pangram("the quick brown fox jumps over the lay dog")); + assert!(!is_pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")); + assert!(is_pangram("\"Five quacking Zephyrs jolt my wax bed.\"")); + assert!(is_pangram( + "Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich." + )); +} diff --git a/tests/pangram_test/src/main.rs b/tests/pangram_test/src/main.rs deleted file mode 100644 index 9c21e6d5..00000000 --- a/tests/pangram_test/src/main.rs +++ /dev/null @@ -1,55 +0,0 @@ -/* -## pangram - -### Instructions - -Determine if the string is a pangram. -A pangram is a sentence using every letter of the alphabet at least once. - -Example: - -"The quick brown fox jumps over the lazy dog." - -*/ -use pangram::*; - -fn main() { - println!( - "{}", - is_pangram("the quick brown fox jumps over the lazy dog!") - ); - println!("{}", is_pangram("this is not a pangram!")); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_empty_strings() { - assert!(!is_pangram("")); - assert!(!is_pangram(" ")); - } - - #[test] - fn test_is_pangram() { - assert!(is_pangram("the quick brown fox jumps over the lazy dog")); - assert!(is_pangram("the_quick_brown_fox_jumps_over_the_lazy_dog")); - assert!(is_pangram( - "the 1 quick brown fox jumps over the 2 lazy dogs" - )); - } - #[test] - fn test_not_pangram() { - assert!(!is_pangram( - "a quick movement of the enemy will jeopardize five gunboats" - )); - assert!(!is_pangram("the quick brown fish jumps over the lazy dog")); - assert!(!is_pangram("the quick brown fox jumps over the lay dog")); - assert!(!is_pangram("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")); - assert!(is_pangram("\"Five quacking Zephyrs jolt my wax bed.\"")); - assert!(is_pangram( - "Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich." - )); - } -} diff --git a/tests/pig_latin_test/src/lib.rs b/tests/pig_latin_test/src/lib.rs new file mode 100644 index 00000000..be2f6e56 --- /dev/null +++ b/tests/pig_latin_test/src/lib.rs @@ -0,0 +1,37 @@ +use pig_latin::*; + +#[test] +fn test_word_beginning_with_vowel() { + assert_eq!(pig_latin("apple"), "appleay"); + assert_eq!(pig_latin("ear"), "earay"); + assert_eq!(pig_latin("igloo"), "iglooay"); + assert_eq!(pig_latin("object"), "objectay"); + assert_eq!(pig_latin("under"), "underay"); + assert_eq!(pig_latin("equal"), "equalay"); +} + +#[test] +fn test_word_beginning_with_consonant() { + assert_eq!(pig_latin("queen"), "ueenqay"); + assert_eq!(pig_latin("square"), "aresquay"); + assert_eq!(pig_latin("pig"), "igpay"); + assert_eq!(pig_latin("koala"), "oalakay"); + assert_eq!(pig_latin("yellow"), "ellowyay"); + assert_eq!(pig_latin("xenon"), "enonxay"); + assert_eq!(pig_latin("qat"), "atqay"); + assert_eq!(pig_latin("chair"), "airchay"); + assert_eq!(pig_latin("therapy"), "erapythay"); + assert_eq!(pig_latin("thrush"), "ushthray"); + assert_eq!(pig_latin("school"), "oolschay"); + assert_eq!(pig_latin("british"), "itishbray"); +} + +#[test] +fn test_multiple_words() { + assert_eq!(pig_latin("apple"), "appleay"); + assert_eq!(pig_latin("ear"), "earay"); + assert_eq!(pig_latin("igloo"), "iglooay"); + assert_eq!(pig_latin("qat"), "atqay"); + assert_eq!(pig_latin("school"), "oolschay"); + assert_eq!(pig_latin("therapy"), "erapythay"); +} diff --git a/tests/pig_latin_test/src/main.rs b/tests/pig_latin_test/src/main.rs deleted file mode 100644 index 6d41fc74..00000000 --- a/tests/pig_latin_test/src/main.rs +++ /dev/null @@ -1,58 +0,0 @@ -/* -## pig_latin - -### Instructions - -Write a function that transforms a string passed as argument in its `Pig Latin` version. - -The rules used by Pig Latin are the following: - -- If a word begins with a vowel, just add "ay" to the end. -- If it begins with a consonant, then we take all consonants before the first vowel and we put them on the end of the word and add "ay" at the end. -- If a word starts with a consonant followed by "qu", move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay"). - -### Notions - -- https://doc.rust-lang.org/book/ch18-00-patterns.html - -*/ -use pig_latin::*; -fn main() { - println!("{}", pig_latin(&String::from("igloo"))); - println!("{}", pig_latin(&String::from("apple"))); - println!("{}", pig_latin(&String::from("hello"))); - println!("{}", pig_latin(&String::from("square"))); - println!("{}", pig_latin(&String::from("xenon"))); - println!("{}", pig_latin(&String::from("chair"))); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_word_beginning_with_vowel() { - assert_eq!(pig_latin(&String::from("apple")), "appleay"); - assert_eq!(pig_latin(&String::from("ear")), "earay"); - assert_eq!(pig_latin(&String::from("igloo")), "iglooay"); - assert_eq!(pig_latin(&String::from("object")), "objectay"); - assert_eq!(pig_latin(&String::from("under")), "underay"); - assert_eq!(pig_latin(&String::from("equal")), "equalay"); - } - - #[test] - fn test_word_beginning_with_consonant() { - assert_eq!(pig_latin(&String::from("queen")), "ueenqay"); - assert_eq!(pig_latin(&String::from("square")), "aresquay"); - assert_eq!(pig_latin(&String::from("pig")), "igpay"); - assert_eq!(pig_latin(&String::from("koala")), "oalakay"); - assert_eq!(pig_latin(&String::from("yellow")), "ellowyay"); - assert_eq!(pig_latin(&String::from("xenon")), "enonxay"); - assert_eq!(pig_latin(&String::from("qat")), "atqay"); - assert_eq!(pig_latin(&String::from("chair")), "airchay"); - assert_eq!(pig_latin(&String::from("therapy")), "erapythay"); - assert_eq!(pig_latin(&String::from("thrush")), "ushthray"); - assert_eq!(pig_latin(&String::from("school")), "oolschay"); - assert_eq!(pig_latin(&String::from("british")), "itishbray"); - } -} diff --git a/tests/rgb_match_test/src/lib.rs b/tests/rgb_match_test/src/lib.rs new file mode 100644 index 00000000..932ebca2 --- /dev/null +++ b/tests/rgb_match_test/src/lib.rs @@ -0,0 +1,126 @@ +use rgb_match::*; + +#[test] +fn test_one() { + let c = Color { + r: 255, + g: 200, + b: 10, + a: 30, + }; + // swap r + assert_eq!( + c.swap(c.r, c.b), + Color { + r: 10, + g: 200, + b: 255, + a: 30 + } + ); + assert_eq!( + c.swap(c.r, c.g), + Color { + r: 200, + g: 255, + b: 10, + a: 30 + } + ); + assert_eq!( + c.swap(c.r, c.a), + Color { + r: 30, + g: 200, + b: 10, + a: 255 + } + ); + + // swap g + assert_eq!( + c.swap(c.g, c.r), + Color { + r: 200, + g: 255, + b: 10, + a: 30 + } + ); + assert_eq!( + c.swap(c.g, c.b), + Color { + r: 255, + g: 10, + b: 200, + a: 30 + } + ); + assert_eq!( + c.swap(c.g, c.a), + Color { + r: 255, + g: 30, + b: 10, + a: 200 + } + ); + + // swap b + assert_eq!( + c.swap(c.b, c.r), + Color { + r: 10, + g: 200, + b: 255, + a: 30 + } + ); + assert_eq!( + c.swap(c.b, c.g), + Color { + r: 255, + g: 10, + b: 200, + a: 30 + } + ); + assert_eq!( + c.swap(c.b, c.a), + Color { + r: 255, + g: 200, + b: 30, + a: 10 + } + ); + + // swap a + assert_eq!( + c.swap(c.a, c.r), + Color { + r: 30, + g: 200, + b: 10, + a: 255 + } + ); + assert_eq!( + c.swap(c.a, c.b), + Color { + r: 255, + g: 200, + b: 30, + a: 10 + } + ); + assert_eq!( + c.swap(c.a, c.g), + Color { + r: 255, + g: 30, + b: 10, + a: 200 + } + ); +} diff --git a/tests/rgb_match_test/src/main.rs b/tests/rgb_match_test/src/main.rs deleted file mode 100644 index 5d9e58f3..00000000 --- a/tests/rgb_match_test/src/main.rs +++ /dev/null @@ -1,169 +0,0 @@ -/* -## rgb_match - -### Instructions - -Implement the struct `Color` with the function `swap`. -This function must allow you to swap the values of the struct. - -### Example: - -If the struct has this values -> Color { r: 255,g: 200,b: 10,a: 30,} and -you want to `swap(c.a, c.g)` you will get -> Color { r: 255, g: 30, b: 10, a: 200 } -*/ -use rgb_match::*; - -fn main() { - let c = Color { - r: 255, - g: 200, - b: 10, - a: 30, - }; - - println!("{:?}", c.swap(c.r, c.b)); - println!("{:?}", c.swap(c.r, c.g)); - println!("{:?}", c.swap(c.r, c.a)); - println!(); - println!("{:?}", c.swap(c.g, c.r)); - println!("{:?}", c.swap(c.g, c.b)); - println!("{:?}", c.swap(c.g, c.a)); - println!(); - println!("{:?}", c.swap(c.b, c.r)); - println!("{:?}", c.swap(c.b, c.g)); - println!("{:?}", c.swap(c.b, c.a)); - println!(); - println!("{:?}", c.swap(c.a, c.r)); - println!("{:?}", c.swap(c.a, c.b)); - println!("{:?}", c.swap(c.a, c.g)); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_one() { - let c = Color { - r: 255, - g: 200, - b: 10, - a: 30, - }; - // swap r - assert_eq!( - c.swap(c.r, c.b), - Color { - r: 10, - g: 200, - b: 255, - a: 30 - } - ); - assert_eq!( - c.swap(c.r, c.g), - Color { - r: 200, - g: 255, - b: 10, - a: 30 - } - ); - assert_eq!( - c.swap(c.r, c.a), - Color { - r: 30, - g: 200, - b: 10, - a: 255 - } - ); - - // swap g - assert_eq!( - c.swap(c.g, c.r), - Color { - r: 200, - g: 255, - b: 10, - a: 30 - } - ); - assert_eq!( - c.swap(c.g, c.b), - Color { - r: 255, - g: 10, - b: 200, - a: 30 - } - ); - assert_eq!( - c.swap(c.g, c.a), - Color { - r: 255, - g: 30, - b: 10, - a: 200 - } - ); - - // swap b - assert_eq!( - c.swap(c.b, c.r), - Color { - r: 10, - g: 200, - b: 255, - a: 30 - } - ); - assert_eq!( - c.swap(c.b, c.g), - Color { - r: 255, - g: 10, - b: 200, - a: 30 - } - ); - assert_eq!( - c.swap(c.b, c.a), - Color { - r: 255, - g: 200, - b: 30, - a: 10 - } - ); - - // swap a - assert_eq!( - c.swap(c.a, c.r), - Color { - r: 30, - g: 200, - b: 10, - a: 255 - } - ); - assert_eq!( - c.swap(c.a, c.b), - Color { - r: 255, - g: 200, - b: 30, - a: 10 - } - ); - assert_eq!( - c.swap(c.a, c.g), - Color { - r: 255, - g: 30, - b: 10, - a: 200 - } - ); - } -} diff --git a/tests/rot_test/src/lib.rs b/tests/rot_test/src/lib.rs new file mode 100644 index 00000000..cef69799 --- /dev/null +++ b/tests/rot_test/src/lib.rs @@ -0,0 +1,37 @@ +use rot::*; + +#[test] +fn test_simple() { + assert_eq!("z", rotate("m", 13)); + assert_eq!("n", rotate("m", 1)); + assert_eq!("a", rotate("a", 26)); + assert_eq!("z", rotate("a", 25)); + assert_eq!("b", rotate("l", 16)); + assert_eq!("j", rotate("j", 0)); + assert_eq!("RNXX", rotate("MISS", 5)); + assert_eq!("M J Q Q T", rotate("H E L L O", 5)); +} + +#[test] +fn test_all_letters() { + assert_eq!( + "Gur svir obkvat jvmneqf whzc dhvpxyl.", + rotate("The five boxing wizards jump quickly.", 13) + ); +} + +#[test] +fn test_numbers_punctuation() { + assert_eq!( + "Xiwxmrk amxl ryqfivw 1 2 3", + rotate("Testing with numbers 1 2 3", 4) + ); + assert_eq!("Gzo\'n zvo, Bmviyhv!", rotate("Let\'s eat, Grandma!", 21)); +} + +#[test] +fn test_negative() { + assert_eq!("z", rotate("a", -1)); + assert_eq!("W", rotate("A", -4)); + assert_eq!("Fqefuzs", rotate("Testing", -14)); +} diff --git a/tests/rot_test/src/main.rs b/tests/rot_test/src/main.rs deleted file mode 100644 index 8e89a8bf..00000000 --- a/tests/rot_test/src/main.rs +++ /dev/null @@ -1,97 +0,0 @@ -/* - ## rotate - -### Instructions - -By now you will have the knowledge of the so called rotational cipher "ROT13". - -A ROT13 on the Latin alphabet would be as follows: - -- Plain: abcdefghijklmnopqrstuvwxyz - -- Cipher: nopqrstuvwxyzabcdefghijklm - -Your purpose in this exercise is to create a similar `rotate` function that is a better version of the ROT13 cipher. -Your function will receive a string and a number and it will rotate each letter of that string, the number of times, settled by the second argument, to the right or to the left if the number are negative. - -Your function should only change letters. If the string includes punctuation and numbers -they will remain the same. - -### Notions - -- https://doc.rust-lang.org/book/ch18-00-patterns.html - -### Expected functions - -```rust - -``` - -### Usage - -Here is a program to test your function. -*/ -use rot::*; - -fn main() { - println!("The letter \"a\" becomes: {}", rotate("a", 26)); - println!("The letter \"m\" becomes: {}", rotate("m", 0)); - println!("The letter \"m\" becomes: {}", rotate("m", 13)); - println!("The letter \"a\" becomes: {}", rotate("a", 15)); - println!("The word \"MISS\" becomes: {}", rotate("MISS", 5)); - println!( - "The decoded message is: {}", - rotate("Gur svir obkvat jvmneqf whzc dhvpxyl.", 13) - ); - println!( - "The decoded message is: {}", - rotate("Mtb vznhpqd ifky ozrunsl ejgwfx ajc", 5) - ); - println!( - "Your cypher wil be: {}", - rotate("Testing with numbers 1 2 3", 4) - ); - println!("Your cypher wil be: {}", rotate("Testing", -14)); - println!("The letter \"a\" becomes: {}", rotate("a", -1)); -} - -#[cfg(test)] -mod test { - use super::*; - - #[test] - fn test_simple() { - assert_eq!("z", rotate("m", 13)); - assert_eq!("n", rotate("m", 1)); - assert_eq!("a", rotate("a", 26)); - assert_eq!("z", rotate("a", 25)); - assert_eq!("b", rotate("l", 16)); - assert_eq!("j", rotate("j", 0)); - assert_eq!("RNXX", rotate("MISS", 5)); - assert_eq!("M J Q Q T", rotate("H E L L O", 5)); - } - - #[test] - fn test_all_letters() { - assert_eq!( - "Gur svir obkvat jvmneqf whzc dhvpxyl.", - rotate("The five boxing wizards jump quickly.", 13) - ); - } - - #[test] - fn test_numbers_punctuation() { - assert_eq!( - "Xiwxmrk amxl ryqfivw 1 2 3", - rotate("Testing with numbers 1 2 3", 4) - ); - assert_eq!("Gzo\'n zvo, Bmviyhv!", rotate("Let\'s eat, Grandma!", 21)); - } - - #[test] - fn test_negative() { - assert_eq!("z", rotate("a", -1)); - assert_eq!("W", rotate("A", -4)); - assert_eq!("Fqefuzs", rotate("Testing", -14)); - } -} diff --git a/tests/scores_test/src/lib.rs b/tests/scores_test/src/lib.rs new file mode 100644 index 00000000..13a8bb44 --- /dev/null +++ b/tests/scores_test/src/lib.rs @@ -0,0 +1,30 @@ +use scores::*; + +#[test] +fn test_simple() { + assert_eq!(score("a"), 1); + assert_eq!(score("A"), 1); + assert_eq!(score("h"), 4); + assert_eq!(score("at"), 2); + assert_eq!(score("Yes"), 6); + assert_eq!(score("cellphones"), 17); +} + +#[test] +fn test_empty() { + assert_eq!(score(""), 0); + assert_eq!(score(" "), 0); +} + +#[test] +fn test_special() { + assert_eq!(score("in Portugal NÃO stands for: no"), 30); + assert_eq!(score("This is a test, comparação, têm Água?"), 36); +} + +#[test] +fn test_long() { + assert_eq!(score("ThiS is A Test"), 14); + assert_eq!(score("long sentences are working"), 34); + assert_eq!(score("abcdefghijklmnopqrstuvwxyz"), 87); +} diff --git a/tests/scores_test/src/main.rs b/tests/scores_test/src/main.rs deleted file mode 100644 index 2cc07297..00000000 --- a/tests/scores_test/src/main.rs +++ /dev/null @@ -1,68 +0,0 @@ -/* -## score - -### Instructions - -Lets play a little! -Create a function `score` that given a string, computes the score for that given string. -Each letter has their value, you just have to sum the values of the letters in the -given string. - -You'll need these: - -Letter Value -A, E, I, O, U, L, N, R, S, T 1 -D, G 2 -B, C, M, P 3 -F, H, V, W, Y 4 -K 5 -J, X 8 -Q, Z 10 - -### Examples - -So if yo pass the word "Hello" you will have the score 8 returned. -Any type of special character or punctuation marks has a value of 0 so if you have -"Á~,!" it will return 0. -*/ -use scores::*; - -fn main() { - println!("{}", score("a")); - println!("{}", score("ã ê Á?")); - println!("{}", score("ThiS is A Test")); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_simple() { - assert_eq!(score("a"), 1); - assert_eq!(score("A"), 1); - assert_eq!(score("h"), 4); - assert_eq!(score("at"), 2); - assert_eq!(score("Yes"), 6); - assert_eq!(score("cellphones"), 17); - } - - #[test] - fn test_empty() { - assert_eq!(score(""), 0); - assert_eq!(score(" "), 0); - } - - #[test] - fn test_special() { - assert_eq!(score("in Portugal NÃO stands for: no"), 30); - assert_eq!(score("This is a test, comparação, têm Água?"), 36); - } - - #[test] - fn test_long() { - assert_eq!(score("ThiS is A Test"), 14); - assert_eq!(score("long sentences are working"), 34); - assert_eq!(score("abcdefghijklmnopqrstuvwxyz"), 87); - } -} diff --git a/tests/scytale_cipher_test/src/lib.rs b/tests/scytale_cipher_test/src/lib.rs new file mode 100644 index 00000000..9a9878b4 --- /dev/null +++ b/tests/scytale_cipher_test/src/lib.rs @@ -0,0 +1,33 @@ +use scytale_cipher::*; + +#[test] +fn test_scytale_code() { + assert_eq!(scytale_cipher("scytale Code", 6), "sec yCtoadle"); + assert_eq!(scytale_cipher("scytale Code", 8), "sCcoydtea l e"); +} + +#[test] +fn test_nothing() { + assert_eq!(scytale_cipher("", 4), ""); + assert_eq!(scytale_cipher("", 0), ""); +} + +#[test] +fn test_same_length() { + assert_eq!(scytale_cipher("qwerty qwerty", 13), "qwerty qwerty"); +} + +#[test] +fn test_thicker_cylinder() { + assert_eq!(scytale_cipher("attack morning", 6), "a ntmgto ar cn ki"); +} + +#[test] +fn test_overflowing() { + assert_eq!(scytale_cipher("abc", 6), "abc"); +} + +#[test] +fn test_others() { + assert_eq!(scytale_cipher("a b c", 2), "abc"); +} diff --git a/tests/scytale_cipher_test/src/main.rs b/tests/scytale_cipher_test/src/main.rs deleted file mode 100644 index f98f005b..00000000 --- a/tests/scytale_cipher_test/src/main.rs +++ /dev/null @@ -1,31 +0,0 @@ -use scytale_cipher::*; - -fn main() { - println!("{:?}", scytale_cipher(String::from("attack morning"), 6)); - // output : a ntmgto ar cn ki -} - -#[test] -fn test_scytale_cipher() { - scytale_cipher(String::from("attack morning"), 6); - assert_eq!( - &scytale_cipher(String::from("scytale Code"), 6), - "sec yCtoadle" - ); - assert_eq!( - &scytale_cipher(String::from("scytale Code"), 8), - "sCcoydtea l e" - ); - // nothing - assert_eq!(&scytale_cipher(String::from(""), 4), ""); - // same len - assert_eq!( - &scytale_cipher(String::from("qwerty qwerty"), 13), - "qwerty qwerty" - ); - // different cylinder - assert_eq!( - &scytale_cipher(String::from("attack morning"), 6), - "a ntmgto ar cn ki" - ); -} diff --git a/tests/searching_test/src/lib.rs b/tests/searching_test/src/lib.rs new file mode 100644 index 00000000..a527bdce --- /dev/null +++ b/tests/searching_test/src/lib.rs @@ -0,0 +1,48 @@ +use searching::*; + +#[test] +fn test_search_a_value_in_an_array() { + assert_eq!(search(&[6], 6), Some(0)); + assert_eq!(search(&[1, 2], 1), Some(0)); + assert_eq!(search(&[1, 2], 2), Some(1)); +} +#[test] +fn test_middle_of_an_array() { + assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 6), Some(3)); +} + +#[test] +fn test_beginning_of_an_array() { + assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 1), Some(0)); +} + +#[test] +fn test_end_of_an_array() { + assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 11), Some(6)); +} + +#[test] +fn test_long_array() { + assert_eq!( + search(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), + Some(9) + ); + assert_eq!( + search(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21), + Some(5) + ); +} + +#[test] +fn test_with_duplicates() { + assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11, 1], 1), Some(7)); + assert_eq!(search(&[1, 3, 9, 6, 8, 9, 11], 9), Some(5)); +} + +#[test] +fn test_value_is_not_included() { + assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 7), None); + assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 0), None); + assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 13), None); + assert_eq!(search(&[], 1), None); +} diff --git a/tests/searching_test/src/main.rs b/tests/searching_test/src/main.rs deleted file mode 100644 index 73d7d412..00000000 --- a/tests/searching_test/src/main.rs +++ /dev/null @@ -1,67 +0,0 @@ -/* -## searching - -### Instructions - -In this exercise you will have to complete the function `search`. -`search` receives an array and a key of `i32`, then it will return the position -of the given key in the array. - -``` -*/ -use searching::*; - -fn main() { - let array = [1, 3, 4, 6, 8, 9, 11]; - let key = search(&array, 6); - println!( - "the element 6 is in the position {:?} in the array {:?}", - key, array - ); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_search_a_value_in_an_array() { - assert_eq!(search(&[6], 6), Some(0)); - assert_eq!(search(&[1, 2], 1), Some(0)); - assert_eq!(search(&[1, 2], 2), Some(1)); - } - #[test] - fn test_middle_of_an_array() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 6), Some(3)); - } - - #[test] - fn test_beginning_of_an_array() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 1), Some(0)); - } - - #[test] - fn test_end_of_an_array() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 11), Some(6)); - } - - #[test] - fn test_long_array() { - assert_eq!( - search(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), - Some(9) - ); - assert_eq!( - search(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21), - Some(5) - ); - } - - #[test] - fn test_value_is_not_included() { - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 7), None); - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 0), None); - assert_eq!(search(&[1, 3, 4, 6, 8, 9, 11], 13), None); - assert_eq!(search(&[], 1), None); - } -} diff --git a/tests/spelling_test/Cargo.toml b/tests/spelling_test/Cargo.toml index e4d8594e..ea7632de 100644 --- a/tests/spelling_test/Cargo.toml +++ b/tests/spelling_test/Cargo.toml @@ -7,5 +7,4 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -rand = "0.7" spelling = { path = "../../solutions/spelling"} diff --git a/tests/spelling_test/src/lib.rs b/tests/spelling_test/src/lib.rs new file mode 100644 index 00000000..024a7c6e --- /dev/null +++ b/tests/spelling_test/src/lib.rs @@ -0,0 +1,34 @@ +use spelling::*; + +#[test] +fn test_predefined() { + assert_eq!(spell(0), String::from("zero")); + assert_eq!(spell(1), String::from("one")); + assert_eq!(spell(14), String::from("fourteen")); + assert_eq!(spell(20), String::from("twenty")); + assert_eq!(spell(22), String::from("twenty-two")); + assert_eq!(spell(101), String::from("one hundred one")); + assert_eq!(spell(120), String::from("one hundred twenty")); + assert_eq!(spell(123), String::from("one hundred twenty-three")); + assert_eq!(spell(1000), String::from("one thousand")); + assert_eq!(spell(1055), String::from("one thousand fifty-five")); + assert_eq!( + spell(1234), + String::from("one thousand two hundred thirty-four") + ); + assert_eq!( + spell(10123), + String::from("ten thousand one hundred twenty-three") + ); + assert_eq!( + spell(910112), + String::from("nine hundred ten thousand one hundred twelve") + ); + assert_eq!( + spell(651123), + String::from("six hundred fifty-one thousand one hundred twenty-three") + ); + + assert_eq!(spell(810000), String::from("eight hundred ten thousand")); + assert_eq!(spell(1000000), String::from("one million")); +} diff --git a/tests/spelling_test/src/main.rs b/tests/spelling_test/src/main.rs deleted file mode 100644 index 22bec3d3..00000000 --- a/tests/spelling_test/src/main.rs +++ /dev/null @@ -1,65 +0,0 @@ -/* -## spelling - - ### Instructions - -In this exercise a number between 0 and 1000000 will be generated. -Your purpose is to create the function `spell` that will spell the numbers generated. - -So, if the program generates the number: - -- 1 your function will return the string "one" -- 14 your function will return the string "fourteen". -- 96 your function will return the string "ninety-six" -- 100 your function will return the string "one hundred". -- 101 your function will return the string "one hundred one" -- 348 your function will return the string "one hundred twenty-three" -- 1002 your function will return the string "one thousand two". -- 1000000 your function will return the string "one million" -*/ -use rand::Rng; -use spelling::*; - -fn main() { - let mut rng = rand::thread_rng(); - println!("{}", spell(rng.gen_range(0, 1000000))); - println!("{}", spell(rng.gen_range(0, 1000000))); -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_one() { - assert_eq!(spell(0), String::from("zero")); - assert_eq!(spell(1), String::from("one")); - assert_eq!(spell(14), String::from("fourteen")); - assert_eq!(spell(20), String::from("twenty")); - assert_eq!(spell(22), String::from("twenty-two")); - assert_eq!(spell(101), String::from("one hundred one")); - assert_eq!(spell(120), String::from("one hundred twenty")); - assert_eq!(spell(123), String::from("one hundred twenty-three")); - assert_eq!(spell(1000), String::from("one thousand")); - assert_eq!(spell(1055), String::from("one thousand fifty-five")); - assert_eq!( - spell(1234), - String::from("one thousand two hundred thirty-four") - ); - assert_eq!( - spell(10123), - String::from("ten thousand one hundred twenty-three") - ); - assert_eq!( - spell(910112), - String::from("nine hundred ten thousand one hundred twelve") - ); - assert_eq!( - spell(651123), - String::from("six hundred fifty-one thousand one hundred twenty-three") - ); - - assert_eq!(spell(810000), String::from("eight hundred ten thousand")); - assert_eq!(spell(1000000), String::from("one million")); - } -} diff --git a/tests/stars_test/src/lib.rs b/tests/stars_test/src/lib.rs new file mode 100644 index 00000000..0d316c97 --- /dev/null +++ b/tests/stars_test/src/lib.rs @@ -0,0 +1,15 @@ +use stars::*; + +#[test] +fn test_stars() { + assert_eq!(stars(0), "*"); + assert_eq!(stars(1), "**"); + assert_eq!(stars(2), "****"); + assert_eq!(stars(3), "********"); + assert_eq!(stars(4), "****************"); + assert_eq!(stars(5), "********************************"); + assert_eq!( + stars(10), + "****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************" + ); +} diff --git a/tests/stars_test/src/main.rs b/tests/stars_test/src/main.rs deleted file mode 100644 index e621e999..00000000 --- a/tests/stars_test/src/main.rs +++ /dev/null @@ -1,41 +0,0 @@ -/* -## stars - -### Instructions - -Write a recursive function named `stars` that takes an i32 as -parameter and returns a String of stars (asterisks) 2^n long (2 to the nth power). - -### Example: - -stars(0) "*" 2^0 = 1 -stars(1) "**" 2^1 = 2 -stars(2) "****" 2^2 = 4 -stars(3) "********" 2^3 = 8 -stars(4) "****************" 2^4 = 16 - -*/ -use stars::*; - -fn main() { - println!("{}", stars(4)); - println!("{}", stars(5)); -} - -#[cfg(test)] -mod test { - use super::*; - #[test] - fn test_stars() { - assert_eq!(stars(0), "*"); - assert_eq!(stars(1), "**"); - assert_eq!(stars(2), "****"); - assert_eq!(stars(3), "********"); - assert_eq!(stars(4), "****************"); - assert_eq!(stars(5), "********************************"); - assert_eq!( - stars(10), - "****************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************" - ); - } -} diff --git a/tests/talking_test/src/lib.rs b/tests/talking_test/src/lib.rs new file mode 100644 index 00000000..02b851f9 --- /dev/null +++ b/tests/talking_test/src/lib.rs @@ -0,0 +1,53 @@ +use talking::*; + +#[test] +fn test_yell() { + assert_eq!( + talking("JUST DO IT!"), + "There is no need to yell, calm down!" + ); + assert_eq!( + talking("1, 2, 3 GO!"), + "There is no need to yell, calm down!" + ); + assert_eq!( + talking("I LOVE YELLING"), + "There is no need to yell, calm down!" + ); + assert_eq!( + talking("WJDAGSAF ASVF EVA VA"), + "There is no need to yell, calm down!" + ); +} + +#[test] +fn test_question() { + assert_eq!(talking("Hello how are you?"), "Sure."); + assert_eq!(talking("Are you going to be OK?"), "Sure."); + assert_eq!(talking("7?"), "Sure."); + assert_eq!(talking("Like 15?"), "Sure."); +} + +#[test] +fn test_question_yelling() { + assert_eq!(talking("WHAT'S GOING ON?"), "Quiet, I am thinking!"); + assert_eq!(talking("ARE YOU FINISHED?"), "Quiet, I am thinking!"); + assert_eq!(talking("WHAT DID I DO?"), "Quiet, I am thinking!"); + assert_eq!(talking("ARE YOU COMING?"), "Quiet, I am thinking!"); +} + +#[test] +fn test_interesting() { + assert_eq!(talking("something"), "Interesting"); + assert_eq!(talking("Wow that's good!"), "Interesting"); + assert_eq!(talking("Run far"), "Interesting"); + assert_eq!(talking("1 2 3 go!"), "Interesting"); + assert_eq!(talking("This is not ? a question."), "Interesting"); +} + +#[test] +fn test_empty() { + assert_eq!(talking(""), "Just say something!"); + assert_eq!(talking(" "), "Just say something!"); + assert_eq!(talking(" "), "Just say something!"); +} diff --git a/tests/talking_test/src/main.rs b/tests/talking_test/src/main.rs deleted file mode 100644 index b89a33c3..00000000 --- a/tests/talking_test/src/main.rs +++ /dev/null @@ -1,80 +0,0 @@ -/* -## talking - -### Instructions - -Build the function `talking` that will allow you to talk with your computer. -His answers will be created by you following the rules below. - -He answers "There is no need to yell, calm down!" if you yell at him, for example "LEAVE ME ALONE!" -(it is consider yelling when the sentence is all written in capital letters). -He answers "Sure" if you ask him something without yelling, for example "Is everything ok with you?" -He answers "Quiet, I am thinking!" if you yell a question at him. "HOW ARE YOU?" -He says "Just say something!" if you address him without actually saying anything. -He answers "Interesting" to anything else. - -*/ -use talking::*; -fn main() { - println!("{:?}", talking("JUST DO IT!")); - println!("{:?}", talking("Hello how are you?")); - println!("{:?}", talking("WHAT'S GOING ON?")); - println!("{:?}", talking("something")); - println!("{:?}", talking("")); -} - -#[cfg(test)] -mod tests { - use super::*; - #[test] - fn test_yell() { - assert_eq!( - talking("JUST DO IT!"), - "There is no need to yell, calm down!" - ); - assert_eq!( - talking("1, 2, 3 GO!"), - "There is no need to yell, calm down!" - ); - assert_eq!( - talking("I LOVE YELLING"), - "There is no need to yell, calm down!" - ); - assert_eq!( - talking("WJDAGSAF ASVF EVA VA"), - "There is no need to yell, calm down!" - ); - } - - #[test] - fn test_question() { - assert_eq!(talking("Hello how are you?"), "Sure."); - assert_eq!(talking("Are you going to be OK?"), "Sure."); - assert_eq!(talking("7?"), "Sure."); - assert_eq!(talking("Like 15?"), "Sure."); - } - - #[test] - fn test_question_yelling() { - assert_eq!(talking("WHAT'S GOING ON?"), "Quiet, I am thinking!"); - assert_eq!(talking("ARE YOU FINISHED?"), "Quiet, I am thinking!"); - assert_eq!(talking("WHAT DID I DO?"), "Quiet, I am thinking!"); - assert_eq!(talking("ARE YOU COMING?"), "Quiet, I am thinking!"); - } - - #[test] - fn test_interesting() { - assert_eq!(talking("something"), "Interesting"); - assert_eq!(talking("Wow that's good!"), "Interesting"); - assert_eq!(talking("Run far"), "Interesting"); - assert_eq!(talking("1 2 3 go!"), "Interesting"); - assert_eq!(talking("This is not ? a question."), "Interesting"); - } - - #[test] - fn test_empty() { - assert_eq!(talking(""), "Just say something!"); - assert_eq!(talking(" "), "Just say something!"); - assert_eq!(talking(" "), "Just say something!"); - } -}