Skip to content

Commit 177716d

Browse files
committed
solve hashmap
1 parent 3bb492a commit 177716d

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

exercises/hashmaps/hashmaps1.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@
1111
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
1212
// hint.
1313

14-
// I AM NOT DONE
15-
1614
use std::collections::HashMap;
1715

1816
fn fruit_basket() -> HashMap<String, u32> {
19-
let mut basket = // TODO: declare your hash map here.
17+
let mut basket = HashMap::new();// TODO: declare your hash map here.
2018

2119
// Two bananas are already given for you :)
2220
basket.insert(String::from("banana"), 2);
2321

2422
// TODO: Put more fruits in your basket here.
23+
basket.insert(String::from("apple"), 3);
24+
25+
basket.insert(String::from("mango"), 4);
2526

2627
basket
2728
}

exercises/hashmaps/hashmaps2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
1515
// hint.
1616

17-
// I AM NOT DONE
1817

1918
use std::collections::HashMap;
2019

@@ -40,6 +39,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
4039
// TODO: Insert new fruits if they are not already present in the
4140
// basket. Note that you are not allowed to put any type of fruit that's
4241
// already present!
42+
basket.entry(fruit).or_insert(5);
4343
}
4444
}
4545

exercises/hashmaps/hashmaps3.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
1515
// hint.
1616

17-
// I AM NOT DONE
1817

1918
use std::collections::HashMap;
2019

@@ -34,11 +33,28 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
3433
let team_1_score: u8 = v[2].parse().unwrap();
3534
let team_2_name = v[1].to_string();
3635
let team_2_score: u8 = v[3].parse().unwrap();
36+
3737
// TODO: Populate the scores table with details extracted from the
3838
// current line. Keep in mind that goals scored by team_1
3939
// will be the number of goals conceded from team_2, and similarly
4040
// goals scored by team_2 will be the number of goals conceded by
4141
// team_1.
42+
let team_1_count = scores.entry(team_1_name)
43+
.or_insert(Team{
44+
goals_scored: 0,
45+
goals_conceded: 0
46+
}
47+
);
48+
team_1_count.goals_scored += team_1_score;
49+
team_1_count.goals_conceded += team_2_score;
50+
let team_2_count = scores.entry(team_2_name)
51+
.or_insert(Team{
52+
goals_scored: 0,
53+
goals_conceded: 0
54+
}
55+
);
56+
team_2_count.goals_scored += team_2_score;
57+
team_2_count.goals_conceded += team_1_score;
4258
}
4359
scores
4460
}

0 commit comments

Comments
 (0)