Browse Source

rust exercises for exams

pull/701/head
OGordoo 4 years ago
parent
commit
b3167f4114
  1. 56
      subjects/matrix_determinant/README.md
  2. 91
      subjects/order_books/README.md
  3. 67
      subjects/partial_sums/README.md

56
subjects/matrix_determinant/README.md

@ -0,0 +1,56 @@
## matrix_determinant
### Instructions
Write a matrix_determinant function that receives a 3x3 matrix ([[isize; 3]; 3]) and returns its determinant (isize).
This is how you calculate a 2x2 matrix determinant:
```sh
|a b|
|c d|
a*d - b*c
```
To calculate a 3x3 matrix determinant (below) you have to take 'a' and multiply it by the determinant of the matrix you get if you get rid of 'a' column and row. Then you subtract the multiplication of 'b' and the determinant of the matrix you get if you get rid of 'b' column and row. And finally, you do the same process for 'c' and add it to the previous result.
```sh
|a b c|
|d e f|
|g h i|
```
### Expected Function
```rs
pub fn matrix_determinant(matrix: [[isize; 3]; 3]) -> isize {
}
```
### Example
Here is a program to test your function:
```rs
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)
);
}
```
And its output:
```sh
$ cargo run
The determinant of the matrix:
|1 2 4|
|2 -1 3| = 35
|4 0 1|
$
```

91
subjects/order_books/README.md

@ -0,0 +1,91 @@
## order_books
### Instructions
Build a module called `library` with two sub-modules inside it:
- `writers` which contains a structure called `Writer` that has a first_name (String), last_name (String) and a set of books (Vec\<Book\>).
- `books` which contains a structure called `Book` that has a title (String) and a year of publish (u64).
You will also have to create (outside the previous modules) a function `order_books` that receives a writer (Writer) and orders the set of books alphabetically.
### Expected Functions and Structs
```rs
pub fn order_books(writer: &mut Writer) {
}
```
```rs
struct Writer {
}
```
```rs
struct Book {
}
```
### Example
Here is a program to test your function and structs:
```rs
fn main() {
let mut writer_a = Writer {
first_name: "William".to_string(),
last_name: "Shakespeare".to_string(),
books: vec![
Book {
title: "Hamlet".to_string(),
year: 1600,
},
Book {
title: "Othelo".to_string(),
year: 1603,
},
Book {
title: "Romeo and Juliet".to_string(),
year: 1593,
},
Book {
title: "MacBeth".to_string(),
year: 1605,
},
],
};
println!("Before ordering");
for b in &writer_a.books {
println!("{:?}", b.title);
}
order_books(&mut writer_a);
println!("\nAfter ordering");
for b in writer_a.books {
println!("{:?}", b.title);
}
}
```
And its output:
```sh
$ cargo run
Before ordering
"Hamlet"
"Othelo"
"Romeo and Juliet"
"MacBeth"
After ordering
"Hamlet"
"MacBeth"
"Othelo"
"Romeo and Juliet"
$
```

67
subjects/partial_sums/README.md

@ -0,0 +1,67 @@
## partial_sums
### Instructions
Write a `partial_sums` function that receives a reference of an array of u64 and returns a vector with the partial sums of the received array.
This is how partial sums work:
1- First you split the array in its partitions:
```sh
[1, 2, 3, 4, 5]
|
V
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
[1, 2, 3]
[1, 2]
[1]
[]
```
2- Then you add each partition together:
```sh
[1, 2, 3, 4, 5] = 15
[1, 2, 3, 4] = 10
[1, 2, 3] = 6
[1, 2] = 3
[1] = 1
[] = 0
```
3- So, in conclusion:
```rs
partial_sums(&[1, 2, 3, 4, 5]) // == [15, 10, 6, 3 ,1, 0]
```
### Expected Result
```rs
pub fn matrix_determinant(arr: &[u64]) -> Vec<64>{
}
```
### Example
Here is a program to test your function:
```rs
fn main() {
println!(
"Partial sums of [5, 18, 3, 23] is : {:?}",
parts_sums(&[5, 18, 3, 23])
);
}
```
And its output:
```sh
$ cargo run
Partial sums of [5, 18, 3, 23] is : [49, 26, 23, 5, 0]
$
```
Loading…
Cancel
Save