hashmaps done

This commit is contained in:
David E. Perez Negron R. 2023-11-04 13:03:25 -06:00
parent 224044b2f3
commit 4af39ccfde
3 changed files with 36 additions and 4 deletions

View File

@ -11,18 +11,19 @@
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
// hint. // 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::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);
// TODO: Put more fruits in your basket here. // TODO: Put more fruits in your basket here.
basket.insert(String::from("apple"), 3);
basket.insert(String::from("mango"), 5);
basket basket
} }

View File

@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
@ -40,7 +39,11 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// TODO: Insert new fruits if they are not already present in the // TODO: Insert new fruits if they are not already present in the
// basket. Note that you are not allowed to put any type of fruit that's // basket. Note that you are not allowed to put any type of fruit that's
// already present! // already present!
if fruit == Fruit::Banana || fruit == Fruit::Pineapple {
basket.insert(fruit, 1);
}
} }
} }
#[cfg(test)] #[cfg(test)]

View File

@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
@ -39,6 +38,35 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded from team_2, and similarly // will be the 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.
let mut team_1 = Team{
goals_scored: team_1_score,
goals_conceded: team_2_score
};
let mut team_2 = Team{
goals_scored: team_2_score,
goals_conceded: team_1_score
};
match scores.get(&team_1_name) {
None => {scores.insert(team_1_name, team_1);},
Some(t) => {
team_1.goals_scored += t.goals_scored;
team_1.goals_conceded += t.goals_conceded;
scores.insert(team_1_name, team_1);
}
};
match scores.get(&team_2_name) {
None => {scores.insert(team_2_name, team_2);},
Some(t) => {
team_2.goals_scored += t.goals_scored;
team_2.goals_conceded += t.goals_conceded;
scores.insert(team_2_name, team_2);
}
};
} }
scores scores
} }