fix(piscine-rust): add crates to exercises

This commit is contained in:
miguel 2023-10-25 13:01:35 +01:00 committed by MSilva95
parent 1aa3484757
commit 166a10990f
28 changed files with 144 additions and 77 deletions

View File

@ -1,6 +1,6 @@
## display_table
### Instructions
### Instructions
Define the `Table` struct below, and implement the associated functions `new` and `add_row`. You can see how they should work from the usage.
@ -38,35 +38,38 @@ impl Table {
Here is a possible program to test your function:
```rust
use display_table::*;
fn main() {
let mut table = Table::new();
println!("{}", table);
table.headers = vec![
String::from("Model"),
String::from("Piece N°"),
String::from("In Stock"),
String::from("Description"),
];
table.add_row(&[
String::from("model 1"),
String::from("43-EWQE304"),
String::from("30"),
String::from("Piece for x"),
]);
table.add_row(&[
String::from("model 2"),
String::from("98-QCVX5433"),
String::from("100000000"),
String::from("-"),
]);
table.add_row(&[
String::from("model y"),
String::from("78-NMNH"),
String::from("60"),
String::from("nothing"),
]);
println!("{}", table);
let mut table = Table::new();
println!("{}", table);
table.headers = vec![
String::from("Model"),
String::from("Piece N°"),
String::from("In Stock"),
String::from("Description"),
];
table.add_row(&[
String::from("model 1"),
String::from("43-EWQE304"),
String::from("30"),
String::from("Piece for x"),
]);
table.add_row(&[
String::from("model 2"),
String::from("98-QCVX5433"),
String::from("100000000"),
String::from("-"),
]);
table.add_row(&[
String::from("model y"),
String::from("78-NMNH"),
String::from("60"),
String::from("nothing"),
]);
println!("{}", table);
}
```
And its output:

View File

@ -1,3 +1,5 @@
use display_table::*;
fn main() {
let mut table = Table::new();
println!("{}", table);

View File

@ -1,6 +1,8 @@
use negative_spelling::*;
use dress_code::*;
fn main() {
println!("{}", negative_spell(-1234));
println!("{}", negative_spell(100));
println!(
"My outfit will be: {:?}",
choose_outfit(Some(0), Ok("Dear friend, ..."))
);
}

View File

@ -41,33 +41,35 @@ impl Table {
Here is a possible program to test your function:
```rust
fn main() {
let mut table = Table::new();
table.headers = vec![
"Name".to_string(),
"Last Name".to_string(),
"ID Number".to_string(),
];
table.add_row(&[
"Adam".to_string(),
"Philips".to_string(),
"123456789".to_string(),
]);
table.add_row(&[
"Adamaris".to_string(),
"Shelby".to_string(),
"1111123456789".to_string(),
]);
table.add_row(&[
"Ackerley".to_string(),
"Philips".to_string(),
"123456789".to_string(),
]);
let filter_names = |col: &str| col == "Name";
println!("{:?}", table.filter_col(filter_names));
use filter_table::*;
let filter_philips = |lastname: &str| lastname == "Philips";
println!("{:?}", table.filter_row("Last Name", filter_philips));
fn main() {
let mut table = Table::new();
table.headers = vec![
"Name".to_string(),
"Last Name".to_string(),
"ID Number".to_string(),
];
table.add_row(&[
"Adam".to_string(),
"Philips".to_string(),
"123456789".to_string(),
]);
table.add_row(&[
"Adamaris".to_string(),
"Shelby".to_string(),
"1111123456789".to_string(),
]);
table.add_row(&[
"Ackerley".to_string(),
"Philips".to_string(),
"123456789".to_string(),
]);
let filter_names = |col: &str| col == "Name";
println!("{:?}", table.filter_col(filter_names));
let filter_philips = |lastname: &str| lastname == "Philips";
println!("{:?}", table.filter_row("Last Name", filter_philips));
}
```

View File

@ -1,3 +1,5 @@
use filter_table::*;
fn main() {
let mut table = Table::new();
table.headers = vec![

View File

@ -17,21 +17,25 @@ pub fn flatten_tree<T: ToOwned<Owned = T>>(tree: &BTreeSet<T>) -> Vec<T> {
Here is a possible program to test your function:
```rust
fn main() {
let mut tree = BTreeSet::new();
tree.insert(34);
tree.insert(0);
tree.insert(9);
tree.insert(30);
println!("{:?}", flatten_tree(&tree));
use flat_tree::*;
use std::collections::BTreeSet;
let mut tree = BTreeSet::new();
tree.insert("Slow");
tree.insert("kill");
tree.insert("will");
tree.insert("Horses");
println!("{:?}", flatten_tree(&tree));
fn main() {
let mut tree = BTreeSet::new();
tree.insert(34);
tree.insert(0);
tree.insert(9);
tree.insert(30);
println!("{:?}", flatten_tree(&tree));
let mut tree = BTreeSet::new();
tree.insert("Slow");
tree.insert("kill");
tree.insert("will");
tree.insert("Horses");
println!("{:?}", flatten_tree(&tree));
}
```
And its output:

View File

@ -1,3 +1,6 @@
use flat_tree::*;
use std::collections::BTreeSet;
fn main() {
let mut tree = BTreeSet::new();
tree.insert(34);

View File

@ -34,6 +34,8 @@ fn inv_pyramid(v: String, i: u32) -> Vec<String> {}
Here is a program to test your function
```rust
use inv_pyramid::*;
fn main() {
let a = inv_pyramid(String::from("#"), 1);
let b = inv_pyramid(String::from("a"), 2);

View File

@ -1,3 +1,5 @@
use inv_pyramid::*;
fn main() {
let a = inv_pyramid(String::from("#"), 1);
let b = inv_pyramid(String::from("a"), 2);

View File

@ -2,7 +2,7 @@
### Instructions
You will need to create an *API*, so that a program can organize a queue of people.
You will need to create an _API_, so that a program can organize a queue of people.
The program requires the following functions. Add them as associated functions to the `Queue` structure:
@ -52,6 +52,8 @@ impl Queue {
Here is a program to test your function:
```rust
use lunch_queue::*;
fn main() {
let mut list = Queue::new();
list.add(String::from("Marie"), 20);

View File

@ -1,3 +1,5 @@
use lunch_queue::*;
fn main() {
let mut list = Queue::new();
list.add(String::from("Marie"), 20);

View File

@ -36,12 +36,14 @@ pub fn matrix_determinant(matrix: [[isize; 3]; 3]) -> isize {
Here is a program to test your function:
```rs
use matrix_determinant::*;
fn main() {
let matrix = [[1, 2, 4], [2, -1, 3], [4, 0, 1]];
println!(
"The determinant of the matrix:\n|1 2 4|\n|2 -1 3| = {}\n|4 0 1|",
matrix_determinant(matr)
matrix_determinant(matrix)
);
}
```

View File

@ -1,8 +1,10 @@
use matrix_determinant::*;
fn main() {
let matrix = [[1, 2, 4], [2, -1, 3], [4, 0, 1]];
println!(
"The determinant of the matrix:\n|1 2 4|\n|2 -1 3| = {}\n|4 0 1|",
matrix_determinant(matr)
matrix_determinant(matrix)
);
}

View File

@ -31,10 +31,13 @@ impl fmt::Display for Matrix {
Here is a possible program to test your function
```rust
use matrix_display::*;
fn main() {
let matrix = Matrix::new(&[&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]]);
println!("{}", matrix);
let matrix = Matrix::new(&[&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]]);
println!("{}", matrix);
}
```
And it's output:

View File

@ -1,3 +1,5 @@
use matrix_display::*;
fn main() {
let matrix = Matrix::new(&[&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]]);
println!("{}", matrix);

View File

@ -0,0 +1,6 @@
use negative_spelling::*;
fn main() {
println!("{}", negative_spell(-1234));
println!("{}", negative_spell(100));
}

View File

@ -21,10 +21,13 @@ pub fn next_prime(nbr: u64) -> u64 {
Here is a possible program to test your function :
```rust
use nextprime::*;
fn main() {
println!("The next prime after 4 is: {}", next_prime(4));
println!("The next prime after 11 is: {}", next_prime(11));
}
```
And its output :

View File

@ -1,3 +1,5 @@
use nextprime::*;
fn main() {
println!("The next prime after 4 is: {}", next_prime(4));
println!("The next prime after 11 is: {}", next_prime(11));

View File

@ -49,6 +49,8 @@ pub fn parts_sums(arr: &[u64]) -> Vec<64>{
Here is a program to test your function:
```rs
use partial_sums::*;
fn main() {
println!(
"Partial sums of [5, 18, 3, 23] is : {:?}",

View File

@ -1,3 +1,5 @@
use partial_sums::*;
fn main() {
println!(
"Partial sums of [5, 18, 3, 23] is : {:?}",

View File

@ -19,6 +19,8 @@ pub fn prev_prime(nbr: u64) -> u64 {
Here is a possible program to test your function :
```rust
use previousprime::*;
fn main() {
println!("The previous prime number before 34 is: {}", prev_prime(34));
}

View File

@ -1,3 +1,5 @@
use previousprime::*;
fn main() {
println!("The previous prime number before 34 is: {}", prev_prime(34));
}

View File

@ -7,6 +7,7 @@ In a chess game, a queen can attack pieces which are on the same rank (row), fil
The purpose of this exercise is to find out if two queens can attack each other.
The position of a chess piece on a chessboard will be represented by the struct `ChessPosition`. You must implement the associated function `new` which will return the position if it is valid, otherwise it will return `None`.
> Remember, chessboards have 8 files and 8 ranks (each from 0 to 7).
You will create the `Queen` struct with the associate function `can_attack`, which will return `true` if the queens can attack each other or not. You also need to implement the function `new` which creates a new `Queen` with a `ChessPosition`.
@ -62,6 +63,8 @@ impl Queen {
Here is a possible program to test your function :
```rust
use queens::*;
fn main() {
let white_queen = Queen::new(ChessPosition::new(2, 2).unwrap());
let black_queen = Queen::new(ChessPosition::new(0, 4).unwrap());
@ -79,6 +82,7 @@ fn main() {
white_queen.can_attack(&black_queen)
);
}
```
And its output:

View File

@ -1,3 +1,5 @@
use queens::*;
fn main() {
let white_queen = Queen::new(ChessPosition::new(2, 2).unwrap());
let black_queen = Queen::new(ChessPosition::new(0, 4).unwrap());

View File

@ -16,6 +16,8 @@ pub fn reverse_it(v: i32) -> String {
Here is a program to test your function,
```rust
use reverse_it::reverse_it;
fn main() {
println!("{}", reverse_it(123));
println!("{}", reverse_it(-123));

View File

@ -1,3 +1,5 @@
use reverse_it::reverse_it;
fn main() {
println!("{}", reverse_it(123));
println!("{}", reverse_it(-123));

View File

@ -4,7 +4,7 @@
Create a **function** which decode a scytale cipher (also known as spartan cipher).
In practice, it is represented by a strip wrapped around a cylinder. The message is written across the loops of the strip (not along the strip). The message becomes *coded* if the radius of the cylinder changes, or the strip is removed from the cylinder.
In practice, it is represented by a strip wrapped around a cylinder. The message is written across the loops of the strip (not along the strip). The message becomes _coded_ if the radius of the cylinder changes, or the strip is removed from the cylinder.
Your function will receive a `String` representing the ciphered message, and a `u32` representing the number of letters by turn of the strip around the cylinder.
@ -23,7 +23,6 @@ Your function will receive a `String` representing the ciphered message, and a `
**letters_per_turn 4:** `"scytale Code"` -> `"steoca dylCe"`
```console
------------------------------------------
|s| |c| |y|
@ -45,6 +44,8 @@ pub fn scytale_decoder(s: String, letters_per_turn: u32) -> Option<String> {
Here is a program to test your function
```rust
use scytale_decoder::*;
fn main() {
println!("\"sec yCtoadle\" size=2 -> {:?}",
scytale_decoder("sec yCtoadle".to_string(), 2));

View File

@ -1,3 +1,5 @@
use scytale_decoder::*;
fn main() {
println!(
"\"sec yCtoadle\" size=2 -> {:?}",