From 2f86fd1efe6bb5b60327907bb48e23d66dd123ff Mon Sep 17 00:00:00 2001 From: MSilva95 Date: Tue, 29 Dec 2020 10:40:42 +0000 Subject: [PATCH] exercises for rust piscine --- rust/tests/adding_test/Cargo.lock | 6 + rust/tests/adding_test/Cargo.toml | 9 + rust/tests/adding_test/src/main.rs | 72 ++++++++ rust/tests/adding_twice_test/Cargo.lock | 6 + rust/tests/adding_twice_test/Cargo.toml | 9 + rust/tests/adding_twice_test/src/main.rs | 89 ++++++++++ rust/tests/diamond_creation_test/Cargo.lock | 5 + rust/tests/diamond_creation_test/Cargo.toml | 9 + rust/tests/diamond_creation_test/src/main.rs | 149 ++++++++++++++++ rust/tests/get_products_test/Cargo.lock | 5 + rust/tests/get_products_test/Cargo.toml | 9 + rust/tests/get_products_test/src/main.rs | 49 ++++++ rust/tests/highest_test/Cargo.lock | 5 + rust/tests/highest_test/Cargo.toml | 9 + rust/tests/highest_test/src/main.rs | 76 ++++++++ rust/tests/iterators_test/Cargo.lock | 5 + rust/tests/iterators_test/Cargo.toml | 9 + rust/tests/iterators_test/src/main.rs | 67 +++++++ rust/tests/logic_number_test/Cargo.lock | 5 + rust/tests/logic_number_test/Cargo.toml | 9 + rust/tests/logic_number_test/src/main.rs | 65 +++++++ rust/tests/ordinal_test/Cargo.lock | 5 + rust/tests/ordinal_test/Cargo.toml | 9 + rust/tests/ordinal_test/src/main.rs | 26 +++ rust/tests/pangram_test/Cargo.lock | 5 + rust/tests/pangram_test/Cargo.toml | 9 + rust/tests/pangram_test/src/main.rs | 56 ++++++ rust/tests/pig_latin_test/Cargo.lock | 5 + rust/tests/pig_latin_test/Cargo.toml | 9 + rust/tests/pig_latin_test/src/main.rs | 59 +++++++ rust/tests/rgb_match_test/Cargo.lock | 5 + rust/tests/rgb_match_test/Cargo.toml | 9 + rust/tests/rgb_match_test/src/main.rs | 176 +++++++++++++++++++ rust/tests/rot_test/Cargo.lock | 5 + rust/tests/rot_test/Cargo.toml | 9 + rust/tests/rot_test/src/main.rs | 97 ++++++++++ rust/tests/scores_test/Cargo.lock | 6 + rust/tests/scores_test/Cargo.toml | 9 + rust/tests/scores_test/src/main.rs | 67 +++++++ rust/tests/searching_test/Cargo.lock | 5 + rust/tests/searching_test/Cargo.toml | 9 + rust/tests/searching_test/src/main.rs | 66 +++++++ rust/tests/spelling_test/Cargo.lock | 84 +++++++++ rust/tests/spelling_test/Cargo.toml | 10 ++ rust/tests/spelling_test/src/main.rs | 63 +++++++ rust/tests/stars_test/Cargo.lock | 5 + rust/tests/stars_test/Cargo.toml | 9 + rust/tests/stars_test/src/main.rs | 40 +++++ rust/tests/talking_test/Cargo.lock | 5 + rust/tests/talking_test/Cargo.toml | 9 + rust/tests/talking_test/src/main.rs | 83 +++++++++ subjects/adding/README.md | 32 ++++ subjects/adding_twice/README.md | 55 ++++++ subjects/diamond_creation/README.md | 46 +++++ subjects/get_products/README.md | 37 ++++ subjects/highest/README.md | 59 +++++++ subjects/iterators/README.md | 68 +++++++ subjects/logic_number/README.md | 58 ++++++ subjects/ordinal/README.md | 35 ++++ subjects/pangram/README.md | 44 +++++ subjects/pig_latin/README.md | 49 ++++++ subjects/rgb_match/README.md | 79 +++++++++ subjects/rot/README.md | 74 ++++++++ subjects/scores/README.md | 53 ++++++ subjects/searching/README.md | 40 +++++ subjects/spelling/README.md | 47 +++++ subjects/stars/README.md | 34 ++++ subjects/talking/README.md | 50 ++++++ 68 files changed, 2481 insertions(+) create mode 100644 rust/tests/adding_test/Cargo.lock create mode 100644 rust/tests/adding_test/Cargo.toml create mode 100644 rust/tests/adding_test/src/main.rs create mode 100644 rust/tests/adding_twice_test/Cargo.lock create mode 100644 rust/tests/adding_twice_test/Cargo.toml create mode 100644 rust/tests/adding_twice_test/src/main.rs create mode 100644 rust/tests/diamond_creation_test/Cargo.lock create mode 100644 rust/tests/diamond_creation_test/Cargo.toml create mode 100644 rust/tests/diamond_creation_test/src/main.rs create mode 100644 rust/tests/get_products_test/Cargo.lock create mode 100644 rust/tests/get_products_test/Cargo.toml create mode 100644 rust/tests/get_products_test/src/main.rs create mode 100644 rust/tests/highest_test/Cargo.lock create mode 100644 rust/tests/highest_test/Cargo.toml create mode 100644 rust/tests/highest_test/src/main.rs create mode 100644 rust/tests/iterators_test/Cargo.lock create mode 100644 rust/tests/iterators_test/Cargo.toml create mode 100644 rust/tests/iterators_test/src/main.rs create mode 100644 rust/tests/logic_number_test/Cargo.lock create mode 100644 rust/tests/logic_number_test/Cargo.toml create mode 100644 rust/tests/logic_number_test/src/main.rs create mode 100644 rust/tests/ordinal_test/Cargo.lock create mode 100644 rust/tests/ordinal_test/Cargo.toml create mode 100644 rust/tests/ordinal_test/src/main.rs create mode 100644 rust/tests/pangram_test/Cargo.lock create mode 100644 rust/tests/pangram_test/Cargo.toml create mode 100644 rust/tests/pangram_test/src/main.rs create mode 100644 rust/tests/pig_latin_test/Cargo.lock create mode 100644 rust/tests/pig_latin_test/Cargo.toml create mode 100644 rust/tests/pig_latin_test/src/main.rs create mode 100644 rust/tests/rgb_match_test/Cargo.lock create mode 100644 rust/tests/rgb_match_test/Cargo.toml create mode 100644 rust/tests/rgb_match_test/src/main.rs create mode 100644 rust/tests/rot_test/Cargo.lock create mode 100644 rust/tests/rot_test/Cargo.toml create mode 100644 rust/tests/rot_test/src/main.rs create mode 100644 rust/tests/scores_test/Cargo.lock create mode 100644 rust/tests/scores_test/Cargo.toml create mode 100644 rust/tests/scores_test/src/main.rs create mode 100644 rust/tests/searching_test/Cargo.lock create mode 100644 rust/tests/searching_test/Cargo.toml create mode 100644 rust/tests/searching_test/src/main.rs create mode 100644 rust/tests/spelling_test/Cargo.lock create mode 100644 rust/tests/spelling_test/Cargo.toml create mode 100644 rust/tests/spelling_test/src/main.rs create mode 100644 rust/tests/stars_test/Cargo.lock create mode 100644 rust/tests/stars_test/Cargo.toml create mode 100644 rust/tests/stars_test/src/main.rs create mode 100644 rust/tests/talking_test/Cargo.lock create mode 100644 rust/tests/talking_test/Cargo.toml create mode 100644 rust/tests/talking_test/src/main.rs create mode 100644 subjects/adding/README.md create mode 100644 subjects/adding_twice/README.md create mode 100644 subjects/diamond_creation/README.md create mode 100644 subjects/get_products/README.md create mode 100644 subjects/highest/README.md create mode 100644 subjects/iterators/README.md create mode 100644 subjects/logic_number/README.md create mode 100644 subjects/ordinal/README.md create mode 100644 subjects/pangram/README.md create mode 100644 subjects/pig_latin/README.md create mode 100644 subjects/rgb_match/README.md create mode 100644 subjects/rot/README.md create mode 100644 subjects/scores/README.md create mode 100644 subjects/searching/README.md create mode 100644 subjects/spelling/README.md create mode 100644 subjects/stars/README.md create mode 100644 subjects/talking/README.md diff --git a/rust/tests/adding_test/Cargo.lock b/rust/tests/adding_test/Cargo.lock new file mode 100644 index 00000000..2eaccef3 --- /dev/null +++ b/rust/tests/adding_test/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adding" +version = "0.1.0" + diff --git a/rust/tests/adding_test/Cargo.toml b/rust/tests/adding_test/Cargo.toml new file mode 100644 index 00000000..590a6cf0 --- /dev/null +++ b/rust/tests/adding_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "adding" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/adding_test/src/main.rs b/rust/tests/adding_test/src/main.rs new file mode 100644 index 00000000..48498258 --- /dev/null +++ b/rust/tests/adding_test/src/main.rs @@ -0,0 +1,72 @@ +/* +## adding + +### Instructions + +Create the function `add_curry` that returns a closure. +The purpose is to curry the add method to create more variations. + +*/ +fn main() { + let add10 = add_curry(-10); + let add20 = add_curry(2066); + let add30 = add_curry(300000); + + println!("{}", add10(5)); + println!("{}", add20(195)); + println!("{}", add30(5696)); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_zero() { + let z = add_curry(0); + assert_eq!(z(1999), 1999); + } + + #[test] + fn test_negatives() { + let neg = add_curry(-10); + assert_eq!(neg(6), -4); + assert_eq!(neg(10), 0); + assert_eq!(neg(600), 590); + assert_eq!(neg(1000), 990); + assert_eq!(neg(463), 453); + assert_eq!(neg(400000000), 399999990); + } + + #[test] + fn test_add10() { + let add10 = add_curry(10); + assert_eq!(add10(6), 16); + assert_eq!(add10(10), 20); + assert_eq!(add10(600), 610); + assert_eq!(add10(1000), 1010); + assert_eq!(add10(463), 473); + assert_eq!(add10(400000000), 400000010); + } + + #[test] + fn test_add250() { + let add250 = add_curry(250); + assert_eq!(add250(6), 256); + assert_eq!(add250(10), 260); + assert_eq!(add250(600), 850); + assert_eq!(add250(1000), 1250); + assert_eq!(add250(463), 713); + assert_eq!(add250(400000000), 400000250); + } + #[test] + fn test_add3960() { + let add3960 = add_curry(3960); + assert_eq!(add3960(6), 3966); + assert_eq!(add3960(10), 3970); + assert_eq!(add3960(600), 4560); + assert_eq!(add3960(1000), 4960); + assert_eq!(add3960(463), 4423); + assert_eq!(add3960(400000000), 400003960); + } +} diff --git a/rust/tests/adding_twice_test/Cargo.lock b/rust/tests/adding_twice_test/Cargo.lock new file mode 100644 index 00000000..b5975bc8 --- /dev/null +++ b/rust/tests/adding_twice_test/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "adding_twice" +version = "0.1.0" + diff --git a/rust/tests/adding_twice_test/Cargo.toml b/rust/tests/adding_twice_test/Cargo.toml new file mode 100644 index 00000000..b06266c3 --- /dev/null +++ b/rust/tests/adding_twice_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "adding_twice" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/adding_twice_test/src/main.rs b/rust/tests/adding_twice_test/src/main.rs new file mode 100644 index 00000000..42be52fb --- /dev/null +++ b/rust/tests/adding_twice_test/src/main.rs @@ -0,0 +1,89 @@ +/* +## adding_twice + +### Instructions + +In this exercise you will have to reuse your `add_curry` function +Then you have to complete the function `twice` using closures, this function will +take a function f(x) as parameter and return a function f(f(x)) +So, the purpose of this function is to add two times the value in `add_curry` +to the original value. + + ### Notions + +- https://doc.rust-lang.org/rust-by-example/fn/hof.html#higher-order-functions +*/ + +fn main() { + let add10 = add_curry(10); + let value = twice(add10); + println!("The value is {}", value(7)); + + let add20 = add_curry(20); + let value = twice(add20); + println!("The value is {}", value(7)); + + let add30 = add_curry(30); + let value = twice(add30); + println!("The value is {}", value(7)); + + let neg = add_curry(-32); + let value = twice(neg); + println!("{}", value(7)); +} +fn twice (fun: impl Fn() -> ) -> Fn() -> { +} + +#[cfg(test)] +mod tests { + use super::*; + + + #[test] + fn test_zero_twice() { + let z = twice(add_curry(0)); + assert_eq!(z(1902), 1902); + } + + #[test] + fn test_negative_twice() { + let neg = twice(add_curry(-32)); + assert_eq!(neg(6), -58); + assert_eq!(neg(10), -54); + assert_eq!(neg(660), 596); + assert_eq!(neg(1902), 1838); + assert_eq!(neg(463), 399); + assert_eq!(neg(400000000), 399999936); + } + + #[test] + fn test_add10_twice() { + let value = twice(add_curry(10)); + assert_eq!(value(6), 26); + assert_eq!(value(10), 30); + assert_eq!(value(600), 620); + assert_eq!(value(1000), 1020); + assert_eq!(value(463), 483); + assert_eq!(value(400000000), 400000020); + } + #[test] + fn test_add20_twice() { + let value = twice(add_curry(20)); + assert_eq!(value(6), 46); + assert_eq!(value(10), 50); + assert_eq!(value(600), 640); + assert_eq!(value(1000), 1040); + assert_eq!(value(463), 503); + assert_eq!(value(400000000), 400000040); + } + #[test] + fn test_add30_twice() { + let value = twice(add_curry(30)); + assert_eq!(value(6), 66); + assert_eq!(value(10), 70); + assert_eq!(value(600), 660); + assert_eq!(value(1000), 1060); + assert_eq!(value(463), 523); + assert_eq!(value(400000000), 400000060); + } +} diff --git a/rust/tests/diamond_creation_test/Cargo.lock b/rust/tests/diamond_creation_test/Cargo.lock new file mode 100644 index 00000000..ab2c3720 --- /dev/null +++ b/rust/tests/diamond_creation_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "diamond_creation" +version = "0.1.0" diff --git a/rust/tests/diamond_creation_test/Cargo.toml b/rust/tests/diamond_creation_test/Cargo.toml new file mode 100644 index 00000000..d0d34d04 --- /dev/null +++ b/rust/tests/diamond_creation_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "diamond_creation" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/diamond_creation_test/src/main.rs b/rust/tests/diamond_creation_test/src/main.rs new file mode 100644 index 00000000..357bb651 --- /dev/null +++ b/rust/tests/diamond_creation_test/src/main.rs @@ -0,0 +1,149 @@ +/* +## 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) + +### 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···· + +*/ + +fn main() { + println!("{:?}", make_diamond('A')); + println!("{:?}", make_diamond('C')); +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_a() { + assert_eq!(make_diamond('A'), vec!["A"]); + } + + #[test] + fn test_b() { + assert_eq!(make_diamond('B'), vec![" A ", "B B", " A "]); + } + + #[test] + fn test_c() { + assert_eq!( + make_diamond('C'), + vec![" A ", " B B ", "C C", " B B ", " A "] + ); + } + + #[test] + fn test_d() { + assert_eq!( + make_diamond('D'), + vec![" A ", " B B ", " C C ", "D D", " C C ", " B B ", " A ",] + ); + } + + #[test] + fn test_z() { + assert_eq!( + make_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/rust/tests/get_products_test/Cargo.lock b/rust/tests/get_products_test/Cargo.lock new file mode 100644 index 00000000..d8511c7a --- /dev/null +++ b/rust/tests/get_products_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "get_products" +version = "0.1.0" diff --git a/rust/tests/get_products_test/Cargo.toml b/rust/tests/get_products_test/Cargo.toml new file mode 100644 index 00000000..68f5d5c6 --- /dev/null +++ b/rust/tests/get_products_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "get_products" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/get_products_test/src/main.rs b/rust/tests/get_products_test/src/main.rs new file mode 100644 index 00000000..c7174e03 --- /dev/null +++ b/rust/tests/get_products_test/src/main.rs @@ -0,0 +1,49 @@ +/* +## get_products + +### Instructions + +Create a function `get_products` that takes a vector of integers, and returns a vector of the products +of each index. For this exercise to be correct you will have to return the product of every index +except the current one. + +### Example: + +Input: arr[] = {10, 3, 5, 6, 2} +Output: prod[] = {180, 600, 360, 300, 900} + +*/ + +fn main() { + let arr: Vec = vec![1, 7, 3, 4]; + let output = get_products(arr); + println!("{:?}", output); +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test_multiple() { + let arr: Vec = vec![1, 7, 3, 4]; + let output = get_products(arr); + let arr2: Vec = vec![10, 3, 5, 6, 2]; + let output2 = get_products(arr2); + assert_eq!(output, vec![84, 12, 28, 21]); + assert_eq!(output2, vec![180, 600, 360, 300, 900]); + } + + #[test] + fn test_empty_case() { + let arr: Vec = Vec::new(); + let output = get_products(arr); + assert_eq!(output, vec![]); + } + + #[test] + fn test_single_case() { + let arr: Vec = vec![2]; + let output = get_products(arr); + assert_eq!(output, vec![]); + } +} diff --git a/rust/tests/highest_test/Cargo.lock b/rust/tests/highest_test/Cargo.lock new file mode 100644 index 00000000..1be0e479 --- /dev/null +++ b/rust/tests/highest_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "highest" +version = "0.1.0" diff --git a/rust/tests/highest_test/Cargo.toml b/rust/tests/highest_test/Cargo.toml new file mode 100644 index 00000000..55a6c550 --- /dev/null +++ b/rust/tests/highest_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "highest" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/highest_test/src/main.rs b/rust/tests/highest_test/src/main.rs new file mode 100644 index 00000000..26747ddb --- /dev/null +++ b/rust/tests/highest_test/src/main.rs @@ -0,0 +1,76 @@ +/* +## Highest + +### Instructions + + In this exercise you will be given a `Numbers` struct. + Your task is to write these methods: + `List` that returns an `array` with every number in the struct + `Latest` that returns an `Option` with the last added number + `Highest` that return an `Option` with the highest number from the list, + `Highest_Three` that returns a `Vec` with the three highest numbers. + + ### Notions + +- https://doc.rust-lang.org/book/ch13-02-iterators.html + +*/ + +#[derive(Debug)] +struct Numbers<'a> { + numbers: &'a [u32], +} + +fn main() { + let expected = [30, 500, 20, 70]; + let n = Numbers::new(&expected); + println!("{:?}", n.List()); + + println!("{:?}", n.Highest()); + println!("{:?}", n.Latest()); + println!("{:?}", n.Highest_Three()); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_List() { + let expected = [30, 50, 20, 70]; + let n = Numbers::new(&expected); + assert_eq!(n.List(), &expected); + } + + #[test] + fn test_Latest() { + let n = Numbers::new(&[100, 0, 90, 30]); + let f = Numbers::new(&[]); + assert_eq!(n.Latest(), Some(30)); + assert!(f.Latest().is_none(), "It should have been None, {}", f.Latest()); + } + + #[test] + fn test_Highest() { + let n = Numbers::new(&[40, 100, 70]); + let f = Numbers::new(&[]); + assert_eq!(n.Highest(), Some(100)); + assert!(f.Highest().is_none(), "It should have been None, {}", f.Highest()); + } + + #[test] + fn test_Highest_Three() { + let e = Numbers::new(&[10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70]); + let f = Numbers::new(&[40, 20, 40, 30]); + let g = Numbers::new(&[30, 70]); + let h = Numbers::new(&[40]); + let i = Numbers::new(&[]); + let j = Numbers::new(&[20, 10, 30]); + assert_eq!(e.Highest_Three(), vec![100, 90, 70]); + assert_eq!(f.Highest_Three(), vec![40, 40, 30]); + assert_eq!(g.Highest_Three(), vec![70, 30]); + assert_eq!(h.Highest_Three(), vec![40]); + assert!(i.Highest_Three().is_empty()); + assert_eq!(j.Highest_Three(), vec![30, 20, 10]); + } +} diff --git a/rust/tests/iterators_test/Cargo.lock b/rust/tests/iterators_test/Cargo.lock new file mode 100644 index 00000000..827d950a --- /dev/null +++ b/rust/tests/iterators_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "even_iterator" +version = "0.1.0" diff --git a/rust/tests/iterators_test/Cargo.toml b/rust/tests/iterators_test/Cargo.toml new file mode 100644 index 00000000..1e66e912 --- /dev/null +++ b/rust/tests/iterators_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "even_iterator" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/iterators_test/src/main.rs b/rust/tests/iterators_test/src/main.rs new file mode 100644 index 00000000..ea871ee3 --- /dev/null +++ b/rust/tests/iterators_test/src/main.rs @@ -0,0 +1,67 @@ +/* +## even_iterator + +### Instructions + +Create a method `new` that takes one number `usize` and initializes the `Number` struct. +This method will have to determinate if the given number is even or odd, +if it is even you will have to increment one to the odd number and +if it is odd you have to increment one to the even number. + +After that you will implement an iterator for the struct `Number` that returns a tuple (usize,usize,usize). +containing each field of the struct Number. +The first position of the tuple will be the even numbers, +the second will be the odd numbers, +and the third will be the factorial numbers. + +### Notions + +- https://doc.rust-lang.org/std/iter/trait.Iterator.html + +*/ + +fn main() { + let mut a = Number::new(5); + println!("{:?}", a.next()); // Some((6, 5, 120)) + println!("{:?}", a.next()); // Some((8, 7, 720)) + println!("{:?}", a.next()); // Some((10, 9, 5040)) + println!() + let mut a = Number::new(18); + println!("{:?}", a.next()); // Some((18, 19, 6402373705728000)) + println!("{:?}", a.next()); // Some((20, 21, 121645100408832000)) + println!("{:?}", a.next()); // Some((22, 23, 2432902008176640000)) +} + +struct Number { + even: usize, + odd: usize, + fact: usize, +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_first_seven() { + let test_even = vec![0, 2, 4, 6, 8, 10, 12]; + let test_odd = vec![1, 3, 5, 7, 9, 11, 13]; + let test_fact = vec![1, 1, 2, 6, 24, 120, 720]; + + for (i, x) in Number::new(0).take(7).enumerate() { + assert_eq!(x.0, test_even[i]); + assert_eq!(x.1, test_odd[i]); + assert_eq!(x.2, test_fact[i]); + } + } + + #[test] + fn test_next() { + let mut a = Number::new(6); + assert_eq!(a.next().unwrap(), (6, 7, 720)); + assert_eq!(a.next().unwrap(), (8, 9, 5040)); + assert_eq!(a.next().unwrap(), (10, 11, 40320)); + assert_eq!(a.next().unwrap(), (12, 13, 362880)); + assert_eq!(a.next().unwrap(), (14, 15, 3628800)); + } +} diff --git a/rust/tests/logic_number_test/Cargo.lock b/rust/tests/logic_number_test/Cargo.lock new file mode 100644 index 00000000..df352370 --- /dev/null +++ b/rust/tests/logic_number_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "logic-number" +version = "0.1.0" diff --git a/rust/tests/logic_number_test/Cargo.toml b/rust/tests/logic_number_test/Cargo.toml new file mode 100644 index 00000000..a28f0b50 --- /dev/null +++ b/rust/tests/logic_number_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "logic-number" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/logic_number_test/src/main.rs b/rust/tests/logic_number_test/src/main.rs new file mode 100644 index 00000000..2d2db5c5 --- /dev/null +++ b/rust/tests/logic_number_test/src/main.rs @@ -0,0 +1,65 @@ +/* +## logic_number + +### Instructions + +In this exercise it will be given an example of a sequence of numbers, your purpose is to +determinate if the sequence returns true or false. +For this you have to create a function `number_logic` that will take a number `u32` and return true +if the number is the sum of its own digits, each raised to the power of the number of digits, +and false otherwise. + +### Example: + + 9 returns true, because 9 = 9^1 = 9 + 10 returns false, because 10 != 1^2 + 0^2 = 1 + 153 returns true, because: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 + 154 returns false, because: 154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190 +*/ + +fn main() { + let array = [9, 10, 153, 154]; + for pat in &array { + if number_logic(*pat) == true { + println!( + "this number returns {} because the number {} obey the rules of the sequence", + number_logic(*pat), + pat + ) + } + if number_logic(*pat) == false { + println!("this number returns {} because the number {} does not obey the rules of the sequence", number_logic(*pat),pat ) + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_zero() { + assert!(number_logic(0)) + } + + #[test] + fn test_single_digit_numbers() { + assert!(number_logic(5)); + assert!(number_logic(9)) + } + + #[test] + fn test_two_digit_numbers() { + assert!(!number_logic(10)) + } + + #[test] + fn test_three_or_more_digit_number() { + assert!(number_logic(153)); + assert!(!number_logic(100)); + assert!(number_logic(9474)); + assert!(!number_logic(9475)); + assert!(number_logic(9_926_315)); + assert!(!number_logic(9_926_316)) + } +} diff --git a/rust/tests/ordinal_test/Cargo.lock b/rust/tests/ordinal_test/Cargo.lock new file mode 100644 index 00000000..1785fddb --- /dev/null +++ b/rust/tests/ordinal_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ordinal" +version = "0.1.0" diff --git a/rust/tests/ordinal_test/Cargo.toml b/rust/tests/ordinal_test/Cargo.toml new file mode 100644 index 00000000..01da4c4c --- /dev/null +++ b/rust/tests/ordinal_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "ordinal" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/ordinal_test/src/main.rs b/rust/tests/ordinal_test/src/main.rs new file mode 100644 index 00000000..1d85d10b --- /dev/null +++ b/rust/tests/ordinal_test/src/main.rs @@ -0,0 +1,26 @@ +/* +## ordinal + +### Instructions + +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)); +} + +#[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/rust/tests/pangram_test/Cargo.lock b/rust/tests/pangram_test/Cargo.lock new file mode 100644 index 00000000..c8022d42 --- /dev/null +++ b/rust/tests/pangram_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "panaram" +version = "0.1.0" diff --git a/rust/tests/pangram_test/Cargo.toml b/rust/tests/pangram_test/Cargo.toml new file mode 100644 index 00000000..d750b421 --- /dev/null +++ b/rust/tests/pangram_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "panaram" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/pangram_test/src/main.rs b/rust/tests/pangram_test/src/main.rs new file mode 100644 index 00000000..3fdc4ba9 --- /dev/null +++ b/rust/tests/pangram_test/src/main.rs @@ -0,0 +1,56 @@ +/* +## 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 std::collections::HashSet; + +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/rust/tests/pig_latin_test/Cargo.lock b/rust/tests/pig_latin_test/Cargo.lock new file mode 100644 index 00000000..593ef227 --- /dev/null +++ b/rust/tests/pig_latin_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "pig_latin" +version = "0.1.0" diff --git a/rust/tests/pig_latin_test/Cargo.toml b/rust/tests/pig_latin_test/Cargo.toml new file mode 100644 index 00000000..4fc3d58b --- /dev/null +++ b/rust/tests/pig_latin_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "pig_latin" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/pig_latin_test/src/main.rs b/rust/tests/pig_latin_test/src/main.rs new file mode 100644 index 00000000..e4bde7cc --- /dev/null +++ b/rust/tests/pig_latin_test/src/main.rs @@ -0,0 +1,59 @@ + +/* +## 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 + +*/ + +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"); + } + + #[test] + + fn test_word_beginning_with_consonant() { + assert_eq!(pig_latin(&String::from("queen")), "eenquay"); + 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"); + } +} \ No newline at end of file diff --git a/rust/tests/rgb_match_test/Cargo.lock b/rust/tests/rgb_match_test/Cargo.lock new file mode 100644 index 00000000..e16e879b --- /dev/null +++ b/rust/tests/rgb_match_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rgb_match" +version = "0.1.0" diff --git a/rust/tests/rgb_match_test/Cargo.toml b/rust/tests/rgb_match_test/Cargo.toml new file mode 100644 index 00000000..e0bd43d6 --- /dev/null +++ b/rust/tests/rgb_match_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rgb_match" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/rgb_match_test/src/main.rs b/rust/tests/rgb_match_test/src/main.rs new file mode 100644 index 00000000..5cddadb9 --- /dev/null +++ b/rust/tests/rgb_match_test/src/main.rs @@ -0,0 +1,176 @@ +/* +## 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 } +*/ + +#[derive(Debug, Clone, Copy, PartialEq)] +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)); +} + +#[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/rust/tests/rot_test/Cargo.lock b/rust/tests/rot_test/Cargo.lock new file mode 100644 index 00000000..5e2bb150 --- /dev/null +++ b/rust/tests/rot_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rot" +version = "0.1.0" diff --git a/rust/tests/rot_test/Cargo.toml b/rust/tests/rot_test/Cargo.toml new file mode 100644 index 00000000..675a71a6 --- /dev/null +++ b/rust/tests/rot_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rot" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/rot_test/src/main.rs b/rust/tests/rot_test/src/main.rs new file mode 100644 index 00000000..46224759 --- /dev/null +++ b/rust/tests/rot_test/src/main.rs @@ -0,0 +1,97 @@ +/* + ## rot + +### 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 `rot` function that is a better version of the ROT13 cipher. +Your function will receive a string and a number and it will rot 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. +*/ + +fn main() { + + println!("The letter \"a\" becomes: {}", rot("a", 26)); + println!("The letter \"m\" becomes: {}", rot("m", 0)); + println!("The letter \"m\" becomes: {}", rot("m", 13)); + println!("The letter \"a\" becomes: {}", rot("a", 15)); + println!("The word \"MISS\" becomes: {}", rot("MISS", 5)); + println!( + "The decoded message is: {}", + rot("Gur svir obkvat jvmneqf whzc dhvpxyl.", 13) + ); + println!( + "The decoded message is: {}", + rot("Mtb vznhpqd ifky ozrunsl ejgwfx ajc", 5) + ); + println!( + "Your cypher wil be: {}", + rot("Testing with numbers 1 2 3", 4) + ); + println!("Your cypher wil be: {}", rot("Testing", -14)); + println!("The letter \"a\" becomes: {}", rot("a", -1)); +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_simple() { + assert_eq!("z", rot("m", 13)); + assert_eq!("n", rot("m", 1)); + assert_eq!("a", rot("a", 26)); + assert_eq!("z", rot("a", 25)); + assert_eq!("b", rot("l", 16)); + assert_eq!("j", rot("j", 0)); + assert_eq!("RNXX", rot("MISS", 5)); + assert_eq!("M J Q Q T", rot("H E L L O", 5)); + } + + #[test] + fn test_all_letters() { + assert_eq!( + "Gur svir obkvat jvmneqf whzc dhvpxyl.", + rot("The five boxing wizards jump quickly.", 13) + ); + } + + #[test] + fn test_numbers_punctuation() { + assert_eq!( + "Xiwxmrk amxl ryqfivw 1 2 3", + rot("Testing with numbers 1 2 3", 4) + ); + assert_eq!("Gzo\'n zvo, Bmviyhv!", rot("Let\'s eat, Grandma!", 21)); + } + + #[test] + fn test_negative() { + assert_eq!("z", rot("a", -1)); + assert_eq!("W", rot("A", -4)); + assert_eq!("Fqefuzs", rot("Testing", -14)); + } +} diff --git a/rust/tests/scores_test/Cargo.lock b/rust/tests/scores_test/Cargo.lock new file mode 100644 index 00000000..ac25b396 --- /dev/null +++ b/rust/tests/scores_test/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "scrabble" +version = "0.1.0" + diff --git a/rust/tests/scores_test/Cargo.toml b/rust/tests/scores_test/Cargo.toml new file mode 100644 index 00000000..6b7e6b9c --- /dev/null +++ b/rust/tests/scores_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "scrabble" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/scores_test/src/main.rs b/rust/tests/scores_test/src/main.rs new file mode 100644 index 00000000..4fb2259f --- /dev/null +++ b/rust/tests/scores_test/src/main.rs @@ -0,0 +1,67 @@ +/* +## 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. +*/ + +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/rust/tests/searching_test/Cargo.lock b/rust/tests/searching_test/Cargo.lock new file mode 100644 index 00000000..0c7799f9 --- /dev/null +++ b/rust/tests/searching_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "searching" +version = "0.1.0" diff --git a/rust/tests/searching_test/Cargo.toml b/rust/tests/searching_test/Cargo.toml new file mode 100644 index 00000000..659bd082 --- /dev/null +++ b/rust/tests/searching_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "searching" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/searching_test/src/main.rs b/rust/tests/searching_test/src/main.rs new file mode 100644 index 00000000..a6c3e526 --- /dev/null +++ b/rust/tests/searching_test/src/main.rs @@ -0,0 +1,66 @@ +/* +## 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. + +``` +*/ + +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/rust/tests/spelling_test/Cargo.lock b/rust/tests/spelling_test/Cargo.lock new file mode 100644 index 00000000..de64d9df --- /dev/null +++ b/rust/tests/spelling_test/Cargo.lock @@ -0,0 +1,84 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + +[[package]] +name = "getrandom" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc587bc0ec293155d5bfa6b9891ec18a1e330c234f896ea47fbada4cadbe47e6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.80" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom", + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core", +] + +[[package]] +name = "spelling" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" diff --git a/rust/tests/spelling_test/Cargo.toml b/rust/tests/spelling_test/Cargo.toml new file mode 100644 index 00000000..1a2286bd --- /dev/null +++ b/rust/tests/spelling_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "spelling" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.7" \ No newline at end of file diff --git a/rust/tests/spelling_test/src/main.rs b/rust/tests/spelling_test/src/main.rs new file mode 100644 index 00000000..c3e8eca6 --- /dev/null +++ b/rust/tests/spelling_test/src/main.rs @@ -0,0 +1,63 @@ +/* +## 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" +*/ + +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/rust/tests/stars_test/Cargo.lock b/rust/tests/stars_test/Cargo.lock new file mode 100644 index 00000000..7abb721d --- /dev/null +++ b/rust/tests/stars_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "stars" +version = "0.1.0" diff --git a/rust/tests/stars_test/Cargo.toml b/rust/tests/stars_test/Cargo.toml new file mode 100644 index 00000000..5881ed6f --- /dev/null +++ b/rust/tests/stars_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "stars" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/stars_test/src/main.rs b/rust/tests/stars_test/src/main.rs new file mode 100644 index 00000000..0499f6e6 --- /dev/null +++ b/rust/tests/stars_test/src/main.rs @@ -0,0 +1,40 @@ +/* +## 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 + +*/ + +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/rust/tests/talking_test/Cargo.lock b/rust/tests/talking_test/Cargo.lock new file mode 100644 index 00000000..08a6c5cc --- /dev/null +++ b/rust/tests/talking_test/Cargo.lock @@ -0,0 +1,5 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "talking" +version = "0.1.0" diff --git a/rust/tests/talking_test/Cargo.toml b/rust/tests/talking_test/Cargo.toml new file mode 100644 index 00000000..07c8e169 --- /dev/null +++ b/rust/tests/talking_test/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "talking" +version = "0.1.0" +authors = ["MSilva95 "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/rust/tests/talking_test/src/main.rs b/rust/tests/talking_test/src/main.rs new file mode 100644 index 00000000..8ef723ef --- /dev/null +++ b/rust/tests/talking_test/src/main.rs @@ -0,0 +1,83 @@ +/* +## 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. + +*/ + +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!"); + } +} diff --git a/subjects/adding/README.md b/subjects/adding/README.md new file mode 100644 index 00000000..e6cb61f3 --- /dev/null +++ b/subjects/adding/README.md @@ -0,0 +1,32 @@ +## adding + +### Instructions + +Create the function `add_curry` that returns a closure. +The purpose is to curry the add method to create more variations. + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let add10 = add_curry(-10); + let add20 = add_curry(2066); + let add30 = add_curry(300000); + + println!("{}", add10(5)); + println!("{}", add20(195)); + println!("{}", add30(5696)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +-5 +2261 +305696 +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/adding_twice/README.md b/subjects/adding_twice/README.md new file mode 100644 index 00000000..04dc18cf --- /dev/null +++ b/subjects/adding_twice/README.md @@ -0,0 +1,55 @@ +## adding_twice + +### Instructions + +In this exercise you will have to reuse your `add_curry` function +Then you have to complete the function `twice` using closures, this function will +take a function f(x) as parameter and return a function f(f(x)) +So, the purpose of this function is to add two times the value in `add_curry` to the original value. + +### Notions + +- https://doc.rust-lang.org/rust-by-example/fn/hof.html#higher-order-functions + +### Expected functions + +The type of the arguments are missing use the example `main` function to determine the correct type. + +```rust +fn twice(F: _) -> _{} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let add10 = add_curry(10); + let value = twice(add10); + println!("The value is {}", value(7)); + + let add20 = add_curry(20); + let value = twice(add20); + println!("The value is {}", value(7)); + + let add30 = add_curry(30); + let value = twice(add30); + println!("The value is {}", value(7)); + + let neg = add_curry(-32); + let value = twice(neg); + println!("The value is {}", value(7)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +The value is 27 +The value is 47 +The value is 67 +The value is -57 +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/diamond_creation/README.md b/subjects/diamond_creation/README.md new file mode 100644 index 00000000..5440524a --- /dev/null +++ b/subjects/diamond_creation/README.md @@ -0,0 +1,46 @@ +## 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$ +``` diff --git a/subjects/get_products/README.md b/subjects/get_products/README.md new file mode 100644 index 00000000..b1eee2e6 --- /dev/null +++ b/subjects/get_products/README.md @@ -0,0 +1,37 @@ +## get_products + +### Instructions + +Create a function `get_products` that takes a vector of integers, and returns a vector of the products +of each index. For this exercise to be correct you will have to return the product of every index +except the current one. + +### Notions + +- https://doc.rust-lang.org/std/iter/trait.Iterator.html + +### Expected functions + +```rust +fn get_products(arr: Vec) -> Vec {} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let arr: Vec = vec![1, 7, 3, 4]; + let output = get_products(arr); + println!("{:?}", output); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +[84, 12, 28, 21] +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/highest/README.md b/subjects/highest/README.md new file mode 100644 index 00000000..2f1e2773 --- /dev/null +++ b/subjects/highest/README.md @@ -0,0 +1,59 @@ +## Highest + +### Instructions + +In this exercise you will be given a `Numbers` struct. + +Your task is to write these methods: + +- `List` that returns an `array` with every number in the struct +- `Latest` that returns an `Option` with the last added number +- `Highest` that return an `Option` with the highest number from the list, +- `Highest_Three` that returns a `Vec` with the three highest numbers. + +### Notions + +- https://doc.rust-lang.org/std/iter/trait.Iterator.html + +### Expected functions + +```rust +fn List(&self) -> &[u32] {} + +fn Latest(&self) -> Option {} + +fn Highest(&self) -> Option {} + +fn Highest_Three(&self) -> Vec {} +``` + +### Usage + +Here is a program to test your function. + +```rust +#[derive(Debug)] +struct Numbers<'a> { + numbers: &'a [u32], +} + +fn main() { + let expected = [30, 500, 20, 70]; + let n = Numbers::new(&expected); + println!("{:?}", n.List()); + println!("{:?}", n.Highest()); + println!("{:?}", n.Latest()); + println!("{:?}", n.Highest_Three()); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +[30, 500, 20, 70] +Some(500) +Some(70) +[500, 70, 30] +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/iterators/README.md b/subjects/iterators/README.md new file mode 100644 index 00000000..070dcc83 --- /dev/null +++ b/subjects/iterators/README.md @@ -0,0 +1,68 @@ +## iterators + +### Instructions + +Create a method `new` that takes one number `usize` and initializes the `Number` struct. + +This method will have to determinate if the given number is even or odd, if it is even you will have to increment one to the odd number and +if it is odd you have to increment one to the even number. + +After that you will implement an iterator for the struct `Number` that returns a tuple (usize,usize,usize) containing each field of the struct Number. + +The first position of the tuple will be the even number, the second will be the odd number, and the third will be the factorial number. + +So the purpose is to return the given number in the right position, if it is even it will be at the first position, and if it is odd it will be in the second position. Apart from that you have to return the factorial of the given number in the third position. + +### Notions + +- https://doc.rust-lang.org/std/iter/trait.Iterator.html + +### Expected functions + +```rust +impl Number { +fn new(nbr: usize) -> Number {} +} + +impl Iterator for Number { + fn next(&mut self) -> Option {} +} +``` + +### Usage + +Here is a program to test your function. + +```rust +struct Number { + even: usize, + odd: usize, + fact: usize, +} + +fn main() { + let mut a = Number::new(5); + println!("{:?}", a.next()); + println!("{:?}", a.next()); + println!("{:?}", a.next()); + println!(); + let mut b = Number::new(18); + println!("{:?}", b.next()); + println!("{:?}", b.next()); + println!("{:?}", b.next()); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +Some((6, 5, 120)) +Some((8, 7, 720)) +Some((10, 9, 5040)) + +Some((18, 19, 6402373705728000)) +Some((20, 21, 121645100408832000)) +Some((22, 23, 2432902008176640000)) +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/logic_number/README.md b/subjects/logic_number/README.md new file mode 100644 index 00000000..d3f2b79f --- /dev/null +++ b/subjects/logic_number/README.md @@ -0,0 +1,58 @@ +## logic_number + +### Instructions + +In this exercise it will be given an example of a sequence of numbers. + +Your purpose is to determinate if the sequence returns true or false. +For this you have to create a function `number_logic` that will take a number `u32` and return true if the number is the sum of its own digits, each raised to the power of the number of digits, and false otherwise. + +Example: + +- 9 returns true, because 9 = 9^1 = 9 +- 10 returns false, because 10 != 1^2 + 0^2 = 1 +- 153 returns true, because: 153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 +- 154 returns false, because: 154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190 + +### Notions + +- https://doc.rust-lang.org/book/ch18-00-patterns.html + +### Expected functions + +```rust +pub fn number_logic(num: u32) -> bool {} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let array = [9, 10, 153, 154]; + for pat in &array { + if number_logic(*pat) == true { + println!( + "this number returns {} because the number {} obey the rules of the sequence", + number_logic(*pat), + pat + ) + } + if number_logic(*pat) == false { + println!("this number returns {} because the number {} does not obey the rules of the sequence", number_logic(*pat),pat ) + } + } +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +this number returns true because the number 9 obey the rules of the sequence +this number returns false because the number 10 does not obey the rules of the sequence +this number returns true because the number 153 obey the rules of the sequence +this number returns false because the number 154 does not obey the rules of the sequence +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/ordinal/README.md b/subjects/ordinal/README.md new file mode 100644 index 00000000..1e0cdbf7 --- /dev/null +++ b/subjects/ordinal/README.md @@ -0,0 +1,35 @@ +## ordinal + +### Instructions + +Complete the function "num_to_ordinal" that receives a cardinal number and returns its ordinal number. + +### Expected functions + +```rust +fn num_to_ordinal(x: u32) -> String {} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + println!("{}", num_to_ordinal(1)); + println!("{}", num_to_ordinal(22)); + println!("{}", num_to_ordinal(43)); + println!("{}", num_to_ordinal(47)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +1st +22nd +43rd +47th +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/pangram/README.md b/subjects/pangram/README.md new file mode 100644 index 00000000..01c4cf02 --- /dev/null +++ b/subjects/pangram/README.md @@ -0,0 +1,44 @@ +## 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." + +### Notions + +- https://doc.rust-lang.org/book/ch18-00-patterns.html + +### Expected functions + +```rust +fn is_pangram(s: &str) -> bool {} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + println!( + "{}", + is_pangram("the quick brown fox jumps over the lazy dog!") + ); + println!("{}", is_pangram("this is not a pangram!")); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +true +false +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/pig_latin/README.md b/subjects/pig_latin/README.md new file mode 100644 index 00000000..18ee5d23 --- /dev/null +++ b/subjects/pig_latin/README.md @@ -0,0 +1,49 @@ +## 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" at the end. + +### Notions + +- https://doc.rust-lang.org/book/ch18-00-patterns.html + +### Expected functions + +```rust +pub fn pig_latin(text: &str) -> String {} +``` + +### Usage + +Here is a program to test your function. + +```rust +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"))); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +iglooay +appleay +ellohay +aresquay +enonxay +airchay +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/rgb_match/README.md b/subjects/rgb_match/README.md new file mode 100644 index 00000000..a4e21c81 --- /dev/null +++ b/subjects/rgb_match/README.md @@ -0,0 +1,79 @@ +## 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$ +``` diff --git a/subjects/rot/README.md b/subjects/rot/README.md new file mode 100644 index 00000000..5db1a0a3 --- /dev/null +++ b/subjects/rot/README.md @@ -0,0 +1,74 @@ +## rot + +### 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 `rot` 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. + +```rust +fn main() { + + println!("The letter \"a\" becomes: {}", rot("a", 26)); + println!("The letter \"m\" becomes: {}", rot("m", 0)); + println!("The letter \"m\" becomes: {}", rot("m", 13)); + println!("The letter \"a\" becomes: {}", rot("a", 15)); + println!("The word \"MISS\" becomes: {}", rot("MISS", 5)); + println!( + "The decoded message is: {}", + rot("Gur svir obkvat jvmneqf whzc dhvpxyl.", 13) + ); + println!( + "The decoded message is: {}", + rot("Mtb vznhpqd ifky ozrunsl ejgwfx ajc", 5) + ); + println!( + "Your cypher wil be: {}", + rot("Testing with numbers 1 2 3", 4) + ); + println!("Your cypher wil be: {}", rot("Testing", -14)); + println!("The letter \"a\" becomes: {}", rot("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$ +``` diff --git a/subjects/scores/README.md b/subjects/scores/README.md new file mode 100644 index 00000000..e39cbca0 --- /dev/null +++ b/subjects/scores/README.md @@ -0,0 +1,53 @@ +## 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 | + +### 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$ +``` diff --git a/subjects/searching/README.md b/subjects/searching/README.md new file mode 100644 index 00000000..0e315720 --- /dev/null +++ b/subjects/searching/README.md @@ -0,0 +1,40 @@ +## searching + +### Instructions + +In this exercise you will have to complete the function `search`. +this function receives an array and a key of `i32`, then it will return the position +of the given key in the array. + +### Notions + +- https://doc.rust-lang.org/book/ch18-00-patterns.html + +### Expected functions + +```rust +fn search(array: &[i32], key: i32) -> Option {} +``` + +### Usage + +Here is a program to test your function. + +```rust +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 + ); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +the element 6 is in the position Some(3) in the array [1, 3, 4, 6, 8, 9, 11] +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/spelling/README.md b/subjects/spelling/README.md new file mode 100644 index 00000000..34d25ca8 --- /dev/null +++ b/subjects/spelling/README.md @@ -0,0 +1,47 @@ +## 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)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +three hundred forty-eight +nine thousand nine hundred ninety-six +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/stars/README.md b/subjects/stars/README.md new file mode 100644 index 00000000..bedda616 --- /dev/null +++ b/subjects/stars/README.md @@ -0,0 +1,34 @@ +## 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$ +``` diff --git a/subjects/talking/README.md b/subjects/talking/README.md new file mode 100644 index 00000000..330fcd6f --- /dev/null +++ b/subjects/talking/README.md @@ -0,0 +1,50 @@ +## 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$ +```