Browse Source

rust exam subjecs

pull/699/head
MSilva95 4 years ago
parent
commit
3cb2a3001d
  1. 50
      subjects/counting_words/README.md
  2. 49
      subjects/easy_traits/README.md
  3. 36
      subjects/lastup/README.md
  4. 92
      subjects/queens/README.md
  5. 44
      subjects/rot21/README.md

50
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<String, u32> {}
```
### 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$
```

49
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<String> {}
```
### 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$
```

36
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$
```

92
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<Self> {}
}
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$
```

44
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$
```
Loading…
Cancel
Save