Browse Source

exam-01: first 3 exercises

pull/765/head
lee 3 years ago
parent
commit
0375bd79bd
  1. BIN
      subjects/insertion_sort/Insertion-Sort-demo.jpg
  2. BIN
      subjects/insertion_sort/Insertion-Sort-demo.png
  3. 27
      subjects/insertion_sort/README.md
  4. 26
      subjects/matrix_transposition_4by3/README.md
  5. 4
      subjects/reverse_it/README.md
  6. 24
      subjects/rpn/README.md

BIN
subjects/insertion_sort/Insertion-Sort-demo.jpg

diff.bin_not_shown

Before

Width:  |  Height:  |  Size: 66 KiB

BIN
subjects/insertion_sort/Insertion-Sort-demo.png

diff.bin_not_shown

After

Width:  |  Height:  |  Size: 119 KiB

27
subjects/insertion_sort/README.md

@ -14,10 +14,11 @@ The insertion sort algorithm:
Here is a visual example of sorting a slice step by step using the insertion sort algorithm. Here is a visual example of sorting a slice step by step using the insertion sort algorithm.
![](Insertion-Sort-demo.jpg) ![image.png](Insertion-Sort-demo.png)
**Figure 1** - Step by step execution of the algorithm insertion sort **Figure 1** - Step by step execution of the algorithm insertion sort
- Implement the algorithm insertion sort by creating a function `insertion_sort(slice, steps)` that executes the iterations of the algorithm the number of steps indicated by the parameter `steps`. See the [Usage](#usage) for more information. - Implement the algorithm insertion sort by creating a function `insertion_sort(slice, steps)` that executes the iterations of the algorithm the number of steps indicated by the parameter `steps`. See the **Usage** for more information.
### Expected Function ### Expected Function
@ -32,17 +33,17 @@ Here is a possible program to test your function
```rust ```rust
fn main() { fn main() {
let mut target = [5, 3, 7, 2, 1, 6, 8, 4]; let mut target = [5, 3, 7, 2, 1, 6, 8, 4];
// executes the first iteration of the algorithm // executes the first iteration of the algorithm
insertion_sort(&mut target, 1); insertion_sort(&mut target, 1);
println!("{:?}", target); println!("{:?}", target);
let mut target = [5, 3, 7, 2, 1, 6, 8, 4]; let mut target = [5, 3, 7, 2, 1, 6, 8, 4];
let len = target.len(); let len = target.len();
// executes len - 1 iterations of the algorithm // executes len - 1 iterations of the algorithm
// i.e. sorts the slice // i.e. sorts the slice
insertion_sort(&mut target, len - 1); insertion_sort(&mut target, len - 1);
println!("{:?}", target); println!("{:?}", target);
} }
``` ```

26
subjects/matrix_transposition_4by3/README.md

@ -8,11 +8,11 @@
- Note: - Note:
- The transpose of a matrix `A` is the matrix `A'` where `A'`'s columns are `A`'s row and the rows are the columns: - The transpose of a matrix `A` is the matrix `A'` where `A'`'s columns are `A`'s row and the rows are the columns:
Example: Example:
``` ```console
( a b c ) __ transposition __> ( a d g j ) ( a b c ) __ transposition __> ( a d g j )
( d e f ) ( b e h k ) ( d e f ) ( b e h k )
( g h i ) ( c f i l ) ( g h i ) ( c f i l )
@ -25,22 +25,22 @@ Example:
### Notions ### Notions
[Chapter 7]( https://doc.rust-lang.org/stable/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html ) [Chapter 7](https://doc.rust-lang.org/stable/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html)
### Expected Function and Structs ### Expected Function and Structs
```rust ```rust
pub struct Matrix4by3( pub struct Matrix4by3(
pub (i32, i32, i32), pub (i32, i32, i32),
pub (i32, i32, i32), pub (i32, i32, i32),
pub (i32, i32, i32), pub (i32, i32, i32),
pub (i32, i32, i32), pub (i32, i32, i32),
); );
pub struct Matrix3by4( pub struct Matrix3by4(
pub (i32, i32, i32, i32), pub (i32, i32, i32, i32),
pub (i32, i32, i32, i32), pub (i32, i32, i32, i32),
pub (i32, i32, i32, i32), pub (i32, i32, i32, i32),
); );
pub fn transpose(m: Matrix4by3) -> Matrix3by4 { pub fn transpose(m: Matrix4by3) -> Matrix3by4 {
@ -49,17 +49,17 @@ pub fn transpose(m: Matrix4by3) -> Matrix3by4 {
### Usage ### Usage
Here is a posible program to test your function Here is a possible program to test your function
```rust ```rust
fn main() { fn main() {
let matrix = Matrix4by3((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12)); let matrix = Matrix4by3((1, 2, 3), (4, 5, 6), (7, 8, 9), (10, 11, 12));
println!("Original matrix {:?}", matrix); println!("Original matrix {:?}", matrix);
println!("Transpose matrix {:?}", transpose(matrix)); println!("Transpose matrix {:?}", transpose(matrix));
} }
``` ```
And it's output: And its output:
```console ```console
student@ubuntu:~/[[ROOT]]/test$ cargo run student@ubuntu:~/[[ROOT]]/test$ cargo run

4
subjects/reverse_it/README.md

@ -8,7 +8,9 @@ just add the char `-` to the beginning of the string.
### Expected Functions ### Expected Functions
```rust ```rust
fn reverse_it(v: i32) -> String {} pub fn reverse_it(v: i32) -> String {
}
``` ```
### Usage ### Usage

24
subjects/rpn/README.md

@ -2,20 +2,16 @@
### Instructions ### Instructions
Write a program that takes a `string` which contains an equation written in Write a program that takes a `string` which contains an equation written in `Reverse Polish Notation` (RPN) as its first argument,
`Reverse Polish Notation` (RPN) as its first argument, that evaluates the equation, and that that evaluates the equation, and that prints the result on the standard output followed by a newline (`'\n'`).
prints the result on the standard output followed by a newline (`'\n'`).
`Reverse Polish Notation` is a mathematical notation in which every operator `Reverse Polish Notation` is a mathematical notation in which every operator follows all of its operands. In RPN,
follows all of its operands. In RPN, every operator encountered evaluates the every operator encountered evaluates the previous 2 operands, and the result of this operation then becomes the first of
previous 2 operands, and the result of this operation then becomes the first of the two operands for the subsequent operator. Operands and operators must be spaced by at least one space.
the two operands for the subsequent operator. Operands and operators must be
spaced by at least one space.
The following operators must be implemented : `+`, `-`, `*`, `/`, and `%`. The following operators must be implemented : `+`, `-`, `*`, `/`, and `%`.
If the `string` is not valid or if there is not exactly one argument, `Error` must be printed If the `string` is not valid or if there is not exactly one argument, `Error` must be printed on the standard output followed by a newline.
on the standard output followed by a newline.
If the `string` has extra spaces it is still considered valid. If the `string` has extra spaces it is still considered valid.
All the given operands must fit in a `int`. All the given operands must fit in a `int`.
@ -30,7 +26,7 @@ Examples of formulas converted in RPN:
Here is how to evaluate a formula in RPN: Here is how to evaluate a formula in RPN:
``` ```console
1 2 * 3 * 4 - 1 2 * 3 * 4 -
2 3 * 4 - 2 3 * 4 -
6 4 - 6 4 -
@ -39,7 +35,7 @@ Here is how to evaluate a formula in RPN:
Or: Or:
``` ```console
3 1 2 * * 4 - 3 1 2 * * 4 -
3 2 * 4 - 3 2 * 4 -
6 4 - 6 4 -
@ -69,5 +65,5 @@ Error
$ cargo run " 1 3 * 2 -" $ cargo run " 1 3 * 2 -"
1 1
$ cargo run " 1 3 * ksd 2 -" $ cargo run " 1 3 * ksd 2 -"
Error``` Error
```` ```

Loading…
Cancel
Save