- Implement the std::fmt::Display trait for the structure table so the table is printed like in the [Usage](#usage) the length of each column must adjust to the longest element of the column and the element must be centered in the "cell" when possible, if the length of the element doesn't allow to center exactly it must alight slightly to the right.
- Implement the `std::fmt::Display` trait for the structure table so that the table is printed like in the **[Usage](#usage)**. The length of each column must adjust to the longest element of the column and the element must be centered in the "cell" when possible. If the length of the element doees not allow to center exactly, it must descend slightly to the right.
- Note: If the table is empty `println!` must not print anything.
- Define the associated function `new`that create a new empty table.
- Define the associated function `new`which creates a new empty table.
- Define the method function `add_row`that adds a new row to the table created from a slice of strings.
- Define the method function `add_row`which adds a new row to the table created from a slice of strings.
- Define the functions `flatten_tree`that receives a std::collections::BTreeSet and returns a new `Vec` with the elements in the binary tree in order.
- Define the functions `flatten_tree`which receives a `std::collections::BTreeSet` and returns a new `Vec` with the elements of the binary tree in order.
- Implement the insertion sort algorithm by creating a function `insertion_sort(slice, steps)` which executes the iterations of the algorithm **up to** the number of steps indicated by the parameter `steps`. See the **Usage** for more information.
- To sort an array of size n in ascending order:
The insertion sort algorithm to sort an array of size n in ascending order:
1. Iterate from slice[1] to slice[n] over the slice.
1. Iterates from slice[1] to slice[n] over the slice.
2. Compare the current element (key) to its predecessor.
2. Compares the current element (key) to its predecessor.
3. If the key element is smaller than its predecessor, compare it to the elements before. Move the greater elements one position up to make space for the swapped element.
3. If the key element is smaller than its predecessor, compares it to the elements before. Move the greater elements one position up to make space for the swapped element.
Here is a visual example of sorting a slice step by step using the insertion sort algorithm.
![](Insertion-Sort-demo.jpg)
**Figure 1** - Step by step execution of the algorithm insertion sort
![image.png](Insertion-Sort-demo.png)
- 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.
**Figure 1** - Step by step execution of the algorithm insertion sort
You will have to create an API to organize a queue of people.
An API will have to be created to organize a queue of people.
Using the given code create the following functions:
Using the given code declare the following functions:
- `new` that will initialize the `Queue`.
- `add` that receives the person's information, so it can be added to the `Queue`
- `invert_queue` that invert the queue of persons
- `rm`, that will remove the person that finished ordering their food.
The method for organizing the manipulation of a data structure should be a
FIFO (first in first out) process. This function should return a tuple wrapped in an `Option`
with the information of that person
- `search`, that return a tuple with the information of a given person `id`.
- `new` which will initialize the `Queue`
- `add` which receives the person's information, so it can be added to the `Queue`
- `invert_queue` which inverts the queue of persons
- `rm`, which will remove the person who finished ordering their food.
The removal should respect a FIFO system (first in first out). This function should return a tuple wrapped in an `Option` with the information of the removed person (check the usage)
- `search`, which returns a tuple with the information of a given person `id`
You must also create a type called `Link` this will be the connection of the structures `Queue` and `Person`.
You must also create a type called `Link`. This will be the connection of the structures `Queue` and `Person`.
Do not forget that this will be a recursion type and it must point to `None` if there is no persons.
Use the Matrix struct given in the [expected struct](#expected-functions-and-struct) and implement the `std::fmt::Display` trait so it prints the matrix like in the [usage](#usage).
You will also have to implement the associated function `new` that creates a matrix from a slice of slices.
The associated function `new` that creates a matrix from a slice of slices also has to be implemented.
[paths for referring to an item in the module tree](https://doc.rust-lang.org/stable/book/ch07-03-paths-for-referring-to-an-item-in-the-module-tree.html)
Build a module called `library` with two sub-modules inside it:
Build a module called `library` with two sub-modules inside of 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).
- `writers` which contains a structure called `Writer`which has a first_name (String),a last_name (String) and a set of books (Vec\<Book\>).
- `books` which contains a structure called `Book`which has a title (String) and a year of publication (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.
A function `order_books` also has to be created (outside the previous modules which receives a writer (Writer) and orders the set of books alphabetically.
### Expected Functions and Structs
#### (The structs declarations need to be filled and added in the appropriate submodules)
```rs
pub fn order_books(writer: &mut Writer) {
pub struct Writer {
}
```
```rs
struct Writer {
pub struct Book {
}
```
```rs
struct Book {
pub fn order_books(writer: &mut Writer) {
}
```
@ -34,6 +36,9 @@ struct Book {
Here is a program to test your function and structs:
In a chess game, a queen can attack pieces which are on the same row,
column, or diagonal.
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 purpose of 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.
the purpose of finding out whether 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`.
with the function `new`. This will allow you to verify if the position is valid or not. If the position is valid it will return that position, otherwise 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:
For example, if the white queen is at (2, 3) and the black queen is at (5, 6),
then the set-up would be like so:
```
_ _ __ __ _ _
@ -33,26 +31,30 @@ _ _ _ _ _ _ _ _
In this case, the two queens can attack each other because both pieces share a diagonal.
Create a function called `reverse_it` that takes a number and returns a string with the number backwards followed by the original number. If the number is negative you should
just add the char `-` to the beginning of the string.
Create a function called `reverse_it` that takes a number and returns a string with the number backwards followed by the original number. If the number is negative a char `-` has to be added to the beginning of the string.