mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 13:49:19 +00:00
Merge pull request #15 from hoangph271/exercises/hashmaps
[Add] exercises/hashmaps
This commit is contained in:
commit
d5f21f29c2
@ -10,17 +10,16 @@
|
|||||||
//
|
//
|
||||||
// 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::new();
|
||||||
|
|
||||||
// Two bananas are already given for you :)
|
|
||||||
basket.insert(String::from("banana"), 2);
|
basket.insert(String::from("banana"), 2);
|
||||||
|
basket.insert(String::from("pear"), 1);
|
||||||
// TODO: Put more fruits in your basket here.
|
basket.insert(String::from("BTC"), 1);
|
||||||
|
basket.insert(String::from("ETH"), 1);
|
||||||
|
basket.insert(String::from("FIL"), 1);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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)]
|
||||||
@ -34,9 +32,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||||||
];
|
];
|
||||||
|
|
||||||
for fruit in fruit_kinds {
|
for fruit in fruit_kinds {
|
||||||
// TODO: Put new fruits if not already present. Note that you
|
if basket.get_mut(&fruit).is_none() {
|
||||||
// are not allowed to put any type of fruit that's already
|
basket.insert(fruit, 1);
|
||||||
// present!
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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.
|
||||||
@ -35,11 +33,22 @@ 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();
|
||||||
// TODO: Populate the scores table with details extracted from the
|
|
||||||
// current line. Keep in mind that goals scored by team_1
|
let team_1_entry = scores.entry(team_1_name.clone()).or_insert(Team {
|
||||||
// will be the number of goals conceded from team_2, and similarly
|
name: team_1_name,
|
||||||
// goals scored by team_2 will be the number of goals conceded by
|
goals_scored: 0,
|
||||||
// team_1.
|
goals_conceded: 0,
|
||||||
|
});
|
||||||
|
team_1_entry.goals_scored += team_1_score;
|
||||||
|
team_1_entry.goals_conceded += team_2_score;
|
||||||
|
|
||||||
|
let team_2_entry = scores.entry(team_2_name.clone()).or_insert(Team {
|
||||||
|
name: team_2_name,
|
||||||
|
goals_scored: 0,
|
||||||
|
goals_conceded: 0,
|
||||||
|
});
|
||||||
|
team_2_entry.goals_scored += team_2_score;
|
||||||
|
team_2_entry.goals_conceded += team_1_score;
|
||||||
}
|
}
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user