finished hasmap excercises and

This commit is contained in:
Ruud Salym J Erie 2022-12-07 20:48:38 -03:00
parent c50941b220
commit 0971fb6dec
6 changed files with 38 additions and 13 deletions

View File

@ -10,15 +10,15 @@
// //
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> { fn fruit_basket() -> HashMap<String, u32> {
let mut basket = // TODO: declare your hash map here. let mut basket = HashMap::<String, u32>::new();// TODO: declare your hash map here.
// Two bananas are already given for you :) // Two bananas are already given for you :)
basket.insert(String::from("banana"), 2); basket.insert(String::from("banana"), 2);
basket.insert(String::from("apple"), 2);
basket.insert(String::from("mango"), 2);
// TODO: Put more fruits in your basket here. // TODO: Put more fruits in your basket here.

View File

@ -11,8 +11,6 @@
// //
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Hash, PartialEq, Eq)] #[derive(Hash, PartialEq, Eq)]
@ -37,6 +35,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// TODO: Put new fruits if not already present. Note that you // TODO: Put new fruits if not already present. Note that you
// are not allowed to put any type of fruit that's already // are not allowed to put any type of fruit that's already
// present! // present!
if !basket.contains_key(&fruit) {
basket.insert(fruit, 1);
}
} }
} }

View File

@ -14,8 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
// A structure to store team name and its goal details. // A structure to store team name and its goal details.
@ -24,6 +22,22 @@ struct Team {
goals_scored: u8, goals_scored: u8,
goals_conceded: u8, goals_conceded: u8,
} }
impl Team {
fn named(name: String) -> Self {
Self { name: name.to_string(), goals_scored: 0, goals_conceded: 0}
}
}
fn update_score(
scores: &mut HashMap<String, Team>,
name: String,
goals_scored: u8,
goals_conceded: u8
)
{
let mut team = scores.entry(name.clone()).or_insert(Team::named(name));
team.goals_scored += goals_scored;
team.goals_conceded += goals_conceded;
}
fn build_scores_table(results: String) -> HashMap<String, Team> { fn build_scores_table(results: String) -> HashMap<String, Team> {
// The name of the team is the key and its associated struct is the value. // The name of the team is the key and its associated struct is the value.
@ -35,8 +49,12 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
let team_1_score: u8 = v[2].parse().unwrap(); let team_1_score: u8 = v[2].parse().unwrap();
let team_2_name = v[1].to_string(); let team_2_name = v[1].to_string();
let team_2_score: u8 = v[3].parse().unwrap(); let team_2_score: u8 = v[3].parse().unwrap();
update_score(&mut scores,team_1_name,team_1_score,team_2_score);
update_score(&mut scores,team_2_name,team_2_score,team_1_score);
// TODO: Populate the scores table with details extracted from the // TODO: Populate the scores table with details extracted from the
// current line. Keep in mind that goals scored by team_1 // current line.
//Keep in mind that goals scored by team_1
// will be number of goals conceded from team_2, and similarly // will be number of goals conceded from team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by // goals scored by team_2 will be the number of goals conceded by
// team_1. // team_1.

View File

@ -18,8 +18,6 @@
// - The output element is going to be a Vector of strings. // - The output element is going to be a Vector of strings.
// Execute `rustlings hint quiz2` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint quiz2` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
pub enum Command { pub enum Command {
Uppercase, Uppercase,
Trim, Trim,
@ -30,11 +28,19 @@ mod my_module {
use super::Command; use super::Command;
// TODO: Complete the function signature! // TODO: Complete the function signature!
pub fn transformer(input: ???) -> ??? { pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
// TODO: Complete the output declaration! // TODO: Complete the output declaration!
let mut output: ??? = vec![]; let mut output: Vec<String> = vec![];
for (string, command) in input.iter() { for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it! // TODO: Complete the function body. You can do it!
match command {
Command::Uppercase => output.push(string.to_uppercase()),
Command::Trim => output.push(string.trim().to_string()),
Command::Append(usize) => output.push(
format!("{string}{}","bar".repeat(*usize))
),
}
} }
output output
} }
@ -43,7 +49,7 @@ mod my_module {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// TODO: What do we have to import to have `transformer` in scope? // TODO: What do we have to import to have `transformer` in scope?
use ???; use my_module::transformer;
use super::Command; use super::Command;
#[test] #[test]

BIN
temp_23683_ThreadId1 Executable file

Binary file not shown.

BIN
temp_53856_ThreadId1 Executable file

Binary file not shown.