Skip to content

Commit

Permalink
finish all hashmap problem.
Browse files Browse the repository at this point in the history
  • Loading branch information
王欽弘 authored and 王欽弘 committed Jan 30, 2025
1 parent 90f1169 commit 6de472d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
5 changes: 3 additions & 2 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
// 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<String, u32> {
let mut basket = // TODO: declare your hash map here.
let mut basket:HashMap<String, u32> = HashMap::new(); // TODO: declare your hash map here.

// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);
basket.insert(String::from("apple"), 114);
basket.insert(String::from("dick"), 514);

// TODO: Put more fruits in your basket here.

Expand Down
6 changes: 5 additions & 1 deletion exercises/hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

Expand All @@ -40,7 +39,12 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// 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
// already present!
if !basket.contains_key(&fruit) {
basket.insert(fruit, 1);
}

}

}

#[cfg(test)]
Expand Down
23 changes: 22 additions & 1 deletion exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

Expand All @@ -39,6 +38,28 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// 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
// team_1.
if let Some(value) = scores.get_mut(&team_1_name){
(*value).goals_scored += team_1_score;
(*value).goals_conceded += team_2_score;
} else {
scores.insert(team_1_name, Team{
goals_scored: team_1_score,
goals_conceded: team_2_score,
});
}


if let Some(value) = scores.get_mut(&team_2_name){
(*value).goals_scored += team_2_score;
(*value).goals_conceded += team_1_score;

} else {
scores.insert(team_2_name, Team{
goals_scored: team_2_score ,
goals_conceded: team_1_score,
});
}

}
scores
}
Expand Down

0 comments on commit 6de472d

Please sign in to comment.