From 3cb2a3001db6ba395f264657a6740693a11f79b5 Mon Sep 17 00:00:00 2001 From: MSilva95 Date: Mon, 25 Jan 2021 18:28:57 +0000 Subject: [PATCH] rust exam subjecs --- subjects/counting_words/README.md | 50 ++++++++++++ subjects/division_and_remainder/README.md | 14 ++-- subjects/easy_traits/README.md | 49 ++++++++++++ subjects/lastup/README.md | 36 +++++++++ subjects/queens/README.md | 92 +++++++++++++++++++++++ subjects/rot21/README.md | 44 +++++++++++ 6 files changed, 278 insertions(+), 7 deletions(-) create mode 100644 subjects/counting_words/README.md create mode 100644 subjects/easy_traits/README.md create mode 100644 subjects/lastup/README.md create mode 100644 subjects/queens/README.md create mode 100644 subjects/rot21/README.md diff --git a/subjects/counting_words/README.md b/subjects/counting_words/README.md new file mode 100644 index 000000000..dc3edb34a --- /dev/null +++ b/subjects/counting_words/README.md @@ -0,0 +1,50 @@ +## counting_words + +### Instructions + +In this program you will have to create a function `counting_words` that +receives a `&str` and returns each word and the number of times it appears on the string. + +The program will count as a word the following: + +- A number like ("0" or "1234") will count as 1. +- A simple word or letter like ("a" or "they") will count as 1. +- Two simple words joined by a single apostrophe ("it's" or "they're") will count as 1. + +The program must respect the following rules: + +- The count is case insensitive ("HELLO", "Hello", and "hello") are 3 uses of the same word. +- All forms of punctuation have to be ignored except for the apostrophe if used like the example above. +- The words can be separated by any form of whitespace (ie "\t", "\n", " "). + +### Expected Function + +```rust +fn counting_words(words: &str) -> HashMap {} +``` + +### Usage + +Here is a possible program to test your function : + +```rust +use counting_words::counting_words; +use std::collections::HashMap; + +fn main() { + println!("{:?}", counting_words("Hello, world!")); + println!("{:?}", counting_words("“Two things are infinite: the universe and human stupidity; and I'm not sure about the universe.” + ― Albert Einstein ")); + println!("{:?}", counting_words("Batman, BATMAN, batman, Stop stop")); +} +``` + +And its output: + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +{"hello": 1, "world": 1} +{"and": 2, "human": 1, "universe": 2, "the": 2, "i\'m": 1, "about": 1, "einstein": 1, "are": 1, "infinite": 1, "sure": 1, "albert": 1, "two": 1, "things": 1, "not": 1, "stupidity": 1} +{"batman": 3, "stop": 2} +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/division_and_remainder/README.md b/subjects/division_and_remainder/README.md index f6a758da8..71f58c977 100644 --- a/subjects/division_and_remainder/README.md +++ b/subjects/division_and_remainder/README.md @@ -20,13 +20,13 @@ Here is a program to test you're function use division_and_remainder::division_and_remainder; fn main() { - let x = 9; - let y = 4; - let (division, remainder) = divide(x, y); - println!( - "{}/{}: division = {}, remainder = {}", - x, y, division, remainder - ); + let x = 9; + let y = 4; + let (division, remainder) = divide(x, y); + println!( + "{}/{}: division = {}, remainder = {}", + x, y, division, remainder + ); } ``` diff --git a/subjects/easy_traits/README.md b/subjects/easy_traits/README.md new file mode 100644 index 000000000..8f7c50539 --- /dev/null +++ b/subjects/easy_traits/README.md @@ -0,0 +1,49 @@ +## easy_traits + +### Instructions + +Your task is to implement the trait `AppendStr` for the type String, and `AppendVec` for a vector of strings. + +The trait `AppendStr` has only one function, which is appending `"world"` to any object implementing this trait. +And `AppendVec` will append `"three"` to a vector of strings. + +### Expected Function + +```rust +trait AppendStr { + fn append_str(self) -> Self; +} + +trait AppendVec { + fn append_vec(&mut self) -> Self; +} + +impl AppendStr for String {} + +impl AppendVec for Vec {} +``` + +### Usage + +Here is a program to test your function. + +```rust +use easy_traits::easy_traits; + +fn main() { + let s = String::from("hello ").append_str(); + println!("{}", s); + + let l = vec![String::from("one"), String::from("two")].append_vec(); + println!("{:?}", l); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +hello world +["one", "two", "three"] +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/lastup/README.md b/subjects/lastup/README.md new file mode 100644 index 000000000..8c6eb910d --- /dev/null +++ b/subjects/lastup/README.md @@ -0,0 +1,36 @@ +## lastup + +### Instructions + +Complete the `lastup` function that takes a string and puts the last letter of each word in uppercase and the rest in lowercase. + +### Expected Functions + +```rust +pub fn lastup(input: &str) -> String { +} +``` + +### Usage + +Here is a program to test your function. + +```rust +use lastup::lastup; + +fn main() { + println!("{}", lastup("joe is missing")); + println!("{}", lastup("jill is leaving A")); + println!("{}",lastup("heLLo THere!")); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +joE iS missinG +jilL iS leavinG A +hellO therE! +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/queens/README.md b/subjects/queens/README.md new file mode 100644 index 000000000..493b71ff9 --- /dev/null +++ b/subjects/queens/README.md @@ -0,0 +1,92 @@ +## queens + +### Instructions + +In a chess game, a queen can attack pieces which are on the same row, +column, or diagonal. + +Your purpose in this exercise is to find out if two queens can attack +each other using the same rules. + +The chess board will be represented as an 8 by 8 array. + +So, given the position of the two queens on a chess board, you will have to +implement the function `can_attack` in the given struct `Queen` with +the purpose of finding out if the two queens can attack each other or not. + +For this to be possible, you will also have to implement the struct `ChessPosition` +with the function `new` that will allow you to verify if the position is valid or not. If the position is valid it will return that position and if it is invalid it will return `None`. + +So if you are told that the white queen is at (2, 3) and the black queen is at (5, 6), +then you would know you have got a set-up like so: + +``` +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ +_ _ _ W _ _ _ _ +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ B _ +_ _ _ _ _ _ _ _ +_ _ _ _ _ _ _ _ +``` + +In this case, the two queens can attack each other because both pieces share a diagonal. + +### Expected Function + +```rust +#[derive(Debug)] +pub struct ChessPosition { + rank: i32, + file: i32, +} + +#[derive(Debug)] +pub struct Queen { + position: ChessPosition, +} + +impl ChessPosition { + pub fn new(rank: i32, file: i32) -> Option {} +} + +impl Queen { + pub fn can_attack(&self, other: &Queen) -> bool {} +} +``` + +### Usage + +Here is a possible program to test your function : + +```rust +use queens::queens; + +fn main() { + let white_queen = Queen::new(ChessPosition::new(2, 2).unwrap()); + let black_queen = Queen::new(ChessPosition::new(0, 4).unwrap()); + + println!( + "Is it possible for the queens to attack each other? => {}", + white_queen.can_attack(&black_queen) + ); + + let white_queen = Queen::new(ChessPosition::new(1, 2).unwrap()); + let black_queen = Queen::new(ChessPosition::new(0, 4).unwrap()); + + println!( + "Is it possible for the queens to attack each other? => {}", + white_queen.can_attack(&black_queen) + ); +} +``` + +And its output: + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +Is it possible for the queens to attack each other? => true +Is it possible for the queens to attack each other? => false +student@ubuntu:~/[[ROOT]]/test$ +``` diff --git a/subjects/rot21/README.md b/subjects/rot21/README.md new file mode 100644 index 000000000..cef47cee2 --- /dev/null +++ b/subjects/rot21/README.md @@ -0,0 +1,44 @@ +## rot21 + +### Instructions + +Your purpose in this exercise is to create a `rot21` function that works like the ROT13 cipher. + +Your function will receive a string and it will rotate each letter of that string 21 times to the right. + +Your function should only change letters. If the string includes punctuation, symbols and numbers +they will remain the same. + +### Expected functions + +```rust +pub fn rot21(input: &str) -> String {} +``` + +### Usage + +Here is a program to test your function. + +```rust +use rot21::rot21; + +fn main() { + println!("The letter \"a\" becomes: {}", rot21("a")); + println!("The letter \"m\" becomes: {}", rot21("m")); + println!("The word \"MISS\" becomes: {}", rot21("MISS")); + println!("Your cypher wil be: {}", rot21("Testing numbers 1 2 3")); + println!("Your cypher wil be: {}", rot21("rot21 works!")); +} + +``` + +And its output + +```console +The letter "a" becomes: v +The letter "m" becomes: h +The word "MISS" becomes: HDNN +Your cypher wil be: Oznodib iphwzmn 1 2 3 +Your cypher wil be: mjo21 rjmfn! +student@ubuntu:~/[[ROOT]]/test$ +```