From d475cef554b6c244743599ce95a539e695a30745 Mon Sep 17 00:00:00 2001 From: enforcer007 Date: Sun, 13 Nov 2022 13:31:41 +0530 Subject: [PATCH] hashmaps --- exercises/hashmaps/hashmaps1.rs | 7 +++---- exercises/hashmaps/hashmaps2.rs | 6 +++--- exercises/hashmaps/hashmaps3.rs | 20 ++++++++++++++++++-- exercises/modules/modules3.rs | 2 -- 4 files changed, 24 insertions(+), 11 deletions(-) diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index fd8dd2f8..157d2e8c 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -10,18 +10,17 @@ // // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::collections::HashMap; fn fruit_basket() -> HashMap { - let mut basket = // TODO: declare your hash map here. + let mut basket = HashMap::new(); // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); // TODO: Put more fruits in your basket here. - + basket.insert(String::from("apple"), 4); + basket.insert(String::from("kiwi"), 4); basket } diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index 454b3e1d..048d3007 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -11,8 +11,6 @@ // // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Hash, PartialEq, Eq)] @@ -37,9 +35,11 @@ fn fruit_basket(basket: &mut HashMap) { // TODO: Put new fruits if not already present. Note that you // are not allowed to put any type of fruit that's already // present! + if let None = basket.get(&fruit) { + basket.insert(fruit, 1); + } } } - #[cfg(test)] mod tests { use super::*; diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 18dd44c9..82d8b2c2 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -14,8 +14,6 @@ // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::collections::HashMap; // A structure to store team name and its goal details. @@ -40,6 +38,24 @@ fn build_scores_table(results: String) -> HashMap { // will be number of goals conceded from team_2, and similarly // goals scored by team_2 will be the number of goals conceded by // team_1. + for team_score in [ + (team_1_name, team_1_score, team_2_score), + (team_2_name, team_2_score, team_1_score), + ] { + if let Some(team) = scores.get_mut(&team_score.0) { + team.goals_scored = team.goals_scored + team_score.1; + team.goals_conceded = team.goals_conceded + team_score.2; + } else { + scores.insert( + team_score.0.clone(), + Team { + name: team_score.0, + goals_scored: team_score.1, + goals_conceded: team_score.2, + }, + ); + }; + } } scores } diff --git a/exercises/modules/modules3.rs b/exercises/modules/modules3.rs index 15fc44d0..0d0264c6 100644 --- a/exercises/modules/modules3.rs +++ b/exercises/modules/modules3.rs @@ -5,8 +5,6 @@ // from the std::time module. Bonus style points if you can do it with one line! // Execute `rustlings hint modules3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - // TODO: Complete this use statement use std::time::{SystemTime, UNIX_EPOCH};