Browse Source

1st pass review on quest 05 subjects

content-update
Chris 3 years ago
parent
commit
f1b1822bec
  1. 114
      subjects/does_it_fit/README.md
  2. 25
      subjects/expected_variable/README.md
  3. 20
      subjects/macro_calculator/README.md
  4. 14
      subjects/middle_day/README.md
  5. 28
      subjects/mobs/README.md
  6. 204
      subjects/shopping_mall/README.md

114
subjects/does_it_fit/README.md

@ -1,30 +1,31 @@
## doe_it_fit
## does_it_fit
### Instructions
Using the `areas_volumes` module provided, create two functions:
Using the `areas_volumes` module provided, create two **functions**:
- `area_fit` that receives 6 arguments:
- `area_fit` which receives 6 arguments and returns a boolean:
- `x` and `y`, size of the square in which it is going to be tried to fit the geometrical shapes (both usize)
- `objects`, the type of geometrical shape(s) that it is going to be tried to be fitted in the square (areas_volumes::GeometricalShapes)
- `times`, the number of geometrical shapes that are going to be tried to be fitted in the square (usize)
- `a` and `b`, the dimensions that the plane(s) shape(s) passed will have (both usize)
- `x` and `y`, length and width of the square in which it is going to be tried to fit the geometrical shapes (both usize)
- `objects`, the type of geometrical shape(s) which are going to be tried to be fitted in the square (areas_volumes::GeometricalShapes)
- `times`, the number of geometrical shapes which are going to be tried to be fitted in the square (usize)
- `a` and `b`, the dimensions which the plane(s) shape(s) passed will have (both usize)
- `a` will refer to the side of the Square, the radius of the Circle, the side_a of the Rectangle or the base of the Triangle
- `b` will refer to the side_b of the Rectangle or the height of the Triangle
- `area_fit` should return `true` if the geometrical shape(s) fit inside of the square.
- `area_fit` should return if the geometrical shape(s) fit inside of the square.
- `volume_fit` that receives 8 arguments:
- `x`, `y` and `z`, size of the box in which it is going to be tried to fit the geometrical volumes (both usize)
- `objects`, the type of geometrical volume(s) that it is going to be tried to be fitted in the box (areas_volumes::GeometricalVolumes)
- `times`, the number of geometrical volumes that are going to be tried to be fitted in the box (usize)
- `a`, `b` and `c`, the dimensions that the geometrical volume(s) passed will have (all of them usize)
- `a` will refer to the side of the Cube, the radius of the Sphere, the side_a of the Parallelepiped, the area of the base of the Triangular Pyramid or the base radius of the Cone
- `volume_fit` which receives 8 arguments and returns a boolean:
- `x`, `y` and `z`, length, width and depth of the box in which it is going to be tried to fit the geometrical volumes (both usize)
- `objects`, the type of geometrical volume(s) which are going to be tried to be fitted in the box (areas_volumes::GeometricalVolumes)
- `times`, the number of geometrical volumes which are going to be tried to be fitted in the box (usize)
- `a`, `b` and `c`, the dimensions which the geometrical volume(s) passed will have (all of them usize)
- `a` will refer to the side of the Cube, the radius of the Sphere, the side_a of the Parallelepipede, the area of the base of the Triangular Pyramid or the base radius of the Cone
- `b` will refer to the side_b of the Parallelepiped, the height of the Triangular Pyramid or the height of the Cone
- `c` will refer to the side_c of the Parallelepiped
- `volume_fit` should return if the geometrical volume(s) fit inside of the box.
- `volume_fit` should return `true` if the geometrical volume(s) fit inside of the box.
### Expected Functions (and Structures)
### Expected Functions
```rs
pub fn area_fit(
@ -34,7 +35,9 @@ pub fn area_fit(
times: usize,
a: usize,
b: usize,
) {}
) -> bool {
}
pub fn volume_fit(
x: usize,
y: usize,
@ -44,7 +47,64 @@ pub fn volume_fit(
a: usize,
b: usize,
c: usize,
) {}
) -> bool {
}
```
### areas_volumes.rs
```rust
pub enum GeometricalShapes {
Square,
Circle,
Rectangle,
Triangle,
}
pub enum GeometricalVolumes {
Cube,
Sphere,
Cone,
Pyramid,
Parallelepiped,
}
pub fn square_area(side: usize) -> usize {
side.pow(2)
}
pub fn triangle_area(base: usize, height: usize) -> f64 {
(base as f64 * height as f64) / 2.0
}
pub fn circle_area(radius: usize) -> f64 {
std::f64::consts::PI * (radius.pow(2) as f64)
}
pub fn rectangle_area(side_a: usize, side_b: usize) -> usize {
side_a * side_b
}
pub fn cube_volume(side: usize) -> usize {
side.pow(3)
}
pub fn sphere_volume(radius: usize) -> f64 {
(4.0 / 3.0) * std::f64::consts::PI * (radius.pow(3) as f64)
}
pub fn triangular_pyramid_volume(base_area: f64, height: usize) -> f64 {
(base_area * height as f64) / 3.0
}
pub fn parallelepiped_volume(side_a: usize, side_b: usize, side_c: usize) -> usize {
side_a * side_b * side_c
}
pub fn cone_volume(base_radius: usize, height: usize) -> f64 {
(1.0 / 3.0) * std::f64::consts::PI * base_radius.pow(2) as f64 * height as f64
}
```
### Usage
@ -52,21 +112,23 @@ pub fn volume_fit(
Here is a program to test your function:
```rust
use does_it_fit::*;
fn main() {
println!(
"Does 100 rectangles (2x1) fit in a 2 by 4 square? {}",
"Do 100 rectangles (2x1) fit in a 2 by 4 square? {}",
area_fit(2, 4, GeometricalShapes::Rectangle, 100, 2, 1)
);
println!(
"Does 3 triangles (5 base and 3 height) fit in a 5 by 5 square? {}",
"Do 3 triangles (5 base and 3 height) fit in a 5 by 5 square? {}",
area_fit(5, 5, GeometricalShapes::Triangle, 3, 5, 3)
);
println!(
"Does 3 spheres (2 radius) fit in a 5 by 5 by 5 box? {}",
"Do 3 spheres (2 radius) fit in a 5 by 5 by 5 box? {}",
volume_fit(5, 5, 5, GeometricalVolumes::Sphere, 3, 2, 0, 0)
);
println!(
"Does 3 triangles (5 base and 3 height) fit in a 5 by 7 by 5 box? {}",
"Do 3 triangles (5 base and 3 height) fit in a 5 by 7 by 5 box? {}",
volume_fit(5, 7, 5, GeometricalVolumes::Parallelepiped, 1, 6, 7, 4)
);
}
@ -76,9 +138,9 @@ And its output:
```sh
$ cargo run
false
true
false
true
Do 100 rectangles (2x1) fit in a 2 by 4 square? false
Do 3 triangles (5 base and 3 height) fit in a 5 by 5 square? true
Do 3 spheres (2 radius) fit in a 5 by 5 by 5 box? true
Do 3 triangles (5 base and 3 height) fit in a 5 by 7 by 5 box? true
$
```

25
subjects/expected_variable/README.md

@ -2,17 +2,30 @@
### Instructions
Create a function `expected_variable` that receives two strings: one to be evaluated and the other to be compared to (expected) and returns an Option. Every comparison should be case insensitive.
Create a **function** `expected_variable` that receives two strings: one to be evaluated and the other to be compared to (expected) and returns an Option. Every comparison should be case insensitive.
If the evaluated string is not in camel case or in snake case according to the `case` crate that you should use, `expected_variable` returns None.
Otherwise the evaluated string should be compared to the expected string using the `edit_distance` function that you did in one of the previous quests.
If the evaluated string is not in camel case or in snake case (use the `case` crate for this evaluation) `expected_variable` returns `None`.
Otherwise the evaluated string should be compared to the expected string using the `edit_distance` **function** that you did in one of the previous quests.
If the result of `edit_distance` has more than 50% of 'alikeness' to the expected string, considering the length of the expected string and the result of `edit_distance`, return that same percentage with a '%' symbol in front of the number.
Otherwise `expected_value` should return None.
If the result of `edit_distance` has more than 50% of 'alikeness' to the expected string, considering the length of the expected string and the result of `edit_distance`, the function should return that same percentage with a '%' symbol in front of the number.
Otherwise `expected_value` should return `None`.
Example:
### Notions
- [Crate case](https://crates.io/crates/case)
- [Specifying Dependencies](https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html)
### Expected Function
#### For this exercise the signature of the function has to be found out.
### Usage
Here is a program to test your function:
```rs
use expected_variable::*;
fn main() {
println!(
"{} close to it",

20
subjects/macro_calculator/README.md

@ -2,9 +2,9 @@
### Instructions
Create a function `calculate_macros` that receives a vector of `serde_json::Value` and returns a `serde_json::Value`.
Create a **function** `calculate_macros` which receives a vector of `serde_json::Value` and returns a `serde_json::Value`.
The vector you will receive will contain jsons in the following format:
The vector the function will receive will contain jsons in the following format:
```json
{
@ -17,9 +17,9 @@ The vector you will receive will contain jsons in the following format:
}
```
Besides the name and the content of the calories array, that are strings, everything else are floats.
The name and the content of the calories array will be `strings`, all other values will be `floats`.
As the result of the function you should return a json with the following format (Macros struct):
The function should return a json with the following format (Macros struct):
```json
"cals": <calories>,
@ -34,9 +34,19 @@ Every value should be rounded to two decimal places (ex: 12.29) or one decimal p
Hint: You will need the `serde`, `serde_json` and `serde_derive` crates.
### Example
### Notions
- [serde](https://crates.io/crates/serde)
- [serde JSON](https://crates.io/crates/serde_json)
- [serde_derive](https://crates.io/crates/serde_derive)
### Usage
Here is a program to test your function:
```rust
use macro_calculator::*;
fn main() {
let a = serde_json::json!(
{

14
subjects/middle_day/README.md

@ -2,10 +2,20 @@
### Instructions
Use the [`chrono`](https://docs.rs/chrono/0.4.19/chrono/index.html) crate to create a function called `middle_day`, that returns the Weekday of the middle day of the year passed as an argument, wrapped in an Option.
You also should refer to chrono::Weekday as `wd`.
Use the [`chrono crate`](https://docs.rs/chrono/0.4.19/chrono/index.html) to create a **function** called `middle_day`, which returns, wrapped in an Option, the Weekday of the middle day of the year passed as an argument.
`chrono::Weekday` has to be refered as `wd`.
### Expected Function
#### For this exercise the signature of the function has to be found out.
### Usage
Here is a program to test your function:
```rs
use middle_day::*;
fn main() {
let date = Utc.ymd(2011, 12, 2).and_hms(21, 12, 09);

28
subjects/mobs/README.md

@ -2,9 +2,9 @@
### Instructions
Create a module `mob`, in which it has to be present:
Create a module `mob`, in which has to be present:
- a `Mob` structure that consists of:
- a `Mob` structure which consists of:
- a String `name`
- a Boss struct `boss`
- a vector of Members `members`
@ -13,43 +13,49 @@ Create a module `mob`, in which it has to be present:
The Mob struct should implement the following functions:
- `recruit`, that adds a member to the members vector of the Mob when receiving a `member_name` (&str) and a `member_age` (u8) (the role should be Associate)
- `attack`, that receives another Mob and will remove the last member from the vector of Members from the Mob with the less power combat (in case of draw, the loser is the attacker). The power combat is calculated by its members:
- `recruit`, which adds a member to the members vector of the Mob when receiving a `member_name` (&str) and a `member_age` (u8) (the role should be Associate)
- `attack`, which receives another Mob and will remove the last member from the vector of Members from the Mob with the less power combat (in case of draw, the loser is the attacker). The power combat is calculated by its members:
- an Underboss power combat is 4
- a Caporegime power combat is 3
- a Soldier power combat is 2
- an Associate power combat is 1
- In case of one of the mobs stays without members, the winner mob adds to its cities every city of the loser mob and the same happens to the wealth, and the loser mob loses all cities and all wealth
- `steal`, that receives the targeted mob (Mob) and a value (u32) and adds to the own mob a value and subtracts the value
- `conquer_city`, that receives a vector of mobs, a city name (String) and a value (u8) and adds it to the vector of cities of the own mob, only if no mob in the vector owns a city with that name
- `steal`, which receives the targeted mob (Mob) and a value (u32) and adds to the own mob a value and subtracts the value
- `conquer_city`, which receives a vector of mobs, a city name (String) and a value (u8) and adds it to the vector of cities of the own mob, only if no mob in the vector owns a city with this name
Also create two submodules of mob:
Create also two submodules of mob:
- `boss` submodule which should contain:
- a `Boss` struct that consists of:
- a `Boss` struct which consists of:
- a String `name`
- an u8 `age`
- a function `new` that returns a Boss on receiving a name (&str) and an age (u8)
- a function `new` which returns a Boss on receiving a name (&str) and an age (u8)
- `member` submodule which consists of:
- a enum `Role` with the variants:
- Underboss
- Caporegime
- Soldier
- Associate
- a `Member` struct that consists of:
- a `Member` struct which consists of:
- a String name
- a enum Role `role`
- a u8 `age`
- the Member struct should implement a function `get_promotion` which will change the member role. If a member is an Associate, it will get promoted to Soldier; a Soldier is promoted to a Caporegime and a Caporegime is promoted to Underboss
- a function `new` that return a Member on receiving a name(&str), a role (Role) and an age (u8)
- a function `new` which return a Member on receiving a name(&str), a role (Role) and an age (u8)
You must include `#[derive(Debug, CLone, PartialEq)]` on top of every struct and the enum.
### Expected Function
#### For this exercise the signature of the function has to be found out.
### Usage
Here is a program to test your function:
```rust
use mobs::*;
fn main() {
let (mafia1, mafia2) = (
Mob {

204
subjects/shopping_mall/README.md

@ -2,18 +2,212 @@
### Instructions
You will have to create several functions to help run a shopping mall, with the help of the `mall` module provided:
Using the `mall` module provided create the following **functions** to help run a shopping mall:
- `biggest_store`: receives a `mall::Mall` and returns the `Store` with the biggest `square_meters`;
- `highest_paid_employees`: receives a `mall::Mall` and returns a vector containing the `Employee`(s) with the highest salaries;
- `nbr_of_employees`: receives a `mall::Mall` and returns the number of employees and securities, as an `usize`, in that mall.
- `nbr_of_employees`: receives a `mall::Mall` and returns the number of employees and securities, as a `usize`, in that mall.
- `fire_old_securities`: receives a `mall::Mall` and removes from the `mall::Mall.securities` all securities who are 50 years old or older.
- `check_securities`: receives a `mall::Mall` and a vector of `Security` and if there are not at least 1 security for every 200 square meters of floor size, there should be added a security to the `mall::Mall.securities`
- `cut_or_raise`: receives a `mall::Mall and raises or cuts the salary of every employee in the mall by 10% depending if the employee works for more than 10 hours
- `check_securities`: receives a `mall::Mall` and a vector of `Security` and, if there is not at least 1 security for every 200 square meters of floor size, a security should be added to the `mall::Mall.securities`
- `cut_or_raise`: receives a `mall::Mall` and raises or cuts, the salary of every employee in the mall by 10%, if the employee works for more than 10 hours
### Example
### Expected Function
#### For this exercise the signature of the function has to be found out.
### mall.rs
```rust
#[derive(Debug, Clone, PartialEq)]
pub struct Mall {
pub name: String,
pub securities: Vec<security::Security>,
pub floors: Vec<floor::Floor>,
}
impl Mall {
#[allow(dead_code)]
pub fn new(name: &str, securities: Vec<security::Security>, floors: Vec<floor::Floor>) -> Mall {
Mall {
name: name.to_string(),
securities: securities,
floors: floors,
}
}
#[allow(dead_code)]
pub fn change_name(&mut self, new_name: &str) {
self.name = new_name.to_string();
}
#[allow(dead_code)]
pub fn hire_security(&mut self, security: security::Security) {
self.securities.push(security);
}
#[allow(dead_code)]
pub fn fire_security(&mut self, name: String) {
self.securities.retain(|x| x.name != name);
}
}
pub mod security {
#[derive(Debug, Clone, PartialEq)]
pub struct Security {
pub name: String,
pub age: u8,
pub years_experience: u8,
}
impl Security {
#[allow(dead_code)]
pub fn new(name: &str, age: u8, years_experience: u8) -> Security {
Security {
name: name.to_string(),
age: age,
years_experience: years_experience,
}
}
}
}
pub mod floor {
#[derive(Debug, Clone, PartialEq)]
pub struct Floor {
pub name: String,
pub stores: Vec<store::Store>,
pub size_limit: u64,
}
impl Floor {
#[allow(dead_code)]
pub fn new(name: &str, stores: Vec<store::Store>, store_limit: u64) -> Floor {
Floor {
name: name.to_string(),
stores: stores,
size_limit: store_limit,
}
}
#[allow(dead_code)]
pub fn change_store(&mut self, store: &str, new_store: store::Store) {
let pos = self.stores.iter().position(|x| x.name == store).unwrap();
self.stores[pos] = new_store;
}
#[allow(dead_code)]
pub fn add_store(&mut self, new_store: store::Store) {
let mut current_floor_size = 0;
for store in self.stores.iter() {
current_floor_size += store.square_meters;
}
if self.size_limit < current_floor_size + new_store.square_meters {
self.stores.push(new_store);
}
}
#[allow(dead_code)]
pub fn remove_store(&mut self, store_name: String) {
self.stores.retain(|x| x.name != store_name);
}
}
pub mod store {
#[derive(Debug, Clone, PartialEq)]
pub struct Store {
pub name: String,
pub square_meters: u64,
pub employees: Vec<employee::Employee>,
}
impl Store {
#[allow(dead_code)]
pub fn new(name: &str, space: u64, employees: Vec<employee::Employee>) -> Store {
Store {
name: name.to_string(),
square_meters: space,
employees: employees,
}
}
#[allow(dead_code)]
pub fn hire_employee(&mut self, employee: employee::Employee) {
self.employees.push(employee);
}
#[allow(dead_code)]
pub fn fire_employee(&mut self, employee_name: &str) {
self.employees.retain(|x| x.name != employee_name);
}
#[allow(dead_code)]
pub fn expand(&mut self, square_meters: u64) {
self.square_meters += square_meters;
}
}
pub mod employee {
#[derive(Debug, Clone, PartialEq)]
pub struct Employee {
pub name: String,
pub age: u8,
pub working_hours: (u8, u8),
pub salary: f64,
}
impl Employee {
#[allow(dead_code)]
pub fn new(
name: &str,
age: u8,
entry_hour: u8,
exit_hour: u8,
salary: f64,
) -> Employee {
Employee {
name: name.to_string(),
age: age,
working_hours: (entry_hour, exit_hour),
salary: salary,
}
}
#[allow(dead_code)]
pub fn birthday(&mut self) {
self.age += 1;
}
#[allow(dead_code)]
pub fn change_workload(&mut self, entry_hour: u8, exit_hour: u8) {
self.working_hours = (entry_hour, exit_hour);
}
#[allow(dead_code)]
pub fn raise(&mut self, amount: f64) {
self.salary += amount;
}
#[allow(dead_code)]
pub fn cut(&mut self, amount: f64) {
self.salary = self.salary - amount;
}
}
}
}
}
```
### Usage
Here is a program to test your function:
```rust
use shopping_mall::*;
fn main() {
let secs = vec![
mall::security::Security::new("John Oliver", 34, 7),

Loading…
Cancel
Save