mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-08 19:59:19 +00:00
update:done the quiz2 and options1
This commit is contained in:
parent
b2b1afcc42
commit
5ec35b41d5
@ -11,15 +11,15 @@
|
||||
// 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::new();
|
||||
|
||||
// Two bananas are already given for you :)
|
||||
basket.insert(String::from("banana"), 2);
|
||||
basket.insert(String::from("apple"), 3);
|
||||
basket.insert(String::from("mange"), 3);
|
||||
|
||||
// TODO: Put more fruits in your basket here.
|
||||
|
||||
|
||||
@ -14,8 +14,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,6 +35,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||
];
|
||||
|
||||
for fruit in fruit_kinds {
|
||||
basket.entry(fruit).or_insert(1);
|
||||
// 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!
|
||||
@ -81,7 +80,7 @@ mod tests {
|
||||
let count = basket.values().sum::<u32>();
|
||||
assert!(count > 11);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn all_fruit_types_in_basket() {
|
||||
let mut basket = get_fruit_basket();
|
||||
|
||||
@ -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 the goal details of a team.
|
||||
@ -34,6 +32,18 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||
let team_1_score: u8 = v[2].parse().unwrap();
|
||||
let team_2_name = v[1].to_string();
|
||||
let team_2_score: u8 = v[3].parse().unwrap();
|
||||
let team1 = scores.entry(team_1_name).or_insert(Team {
|
||||
goals_scored: 0,
|
||||
goals_conceded: 0,
|
||||
});
|
||||
team1.goals_scored += team_1_score;
|
||||
team1.goals_conceded += team_2_score;
|
||||
let team2 = scores.entry(team_2_name).or_insert(Team {
|
||||
goals_scored: 0,
|
||||
goals_conceded: 0,
|
||||
});
|
||||
team2.goals_scored += team_2_score;
|
||||
team2.goals_conceded += team_1_score;
|
||||
// TODO: Populate the scores table with details extracted from the
|
||||
// current line. Keep in mind that goals scored by team_1
|
||||
// will be the number of goals conceded from team_2, and similarly
|
||||
|
||||
@ -3,8 +3,6 @@
|
||||
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a
|
||||
// hint.
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
// This function returns how much icecream there is left in the fridge.
|
||||
// If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
|
||||
// all, so there'll be no more left :(
|
||||
@ -13,7 +11,13 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
|
||||
// value of 0 The Option output should gracefully handle cases where
|
||||
// time_of_day > 23.
|
||||
// TODO: Complete the function body - remember to return an Option!
|
||||
???
|
||||
if time_of_day < 22 {
|
||||
return Some(5);
|
||||
}
|
||||
if time_of_day >= 22 && time_of_day <= 24 {
|
||||
return Some(0);
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -34,6 +38,6 @@ mod tests {
|
||||
// TODO: Fix this test. How do you get at the value contained in the
|
||||
// Option?
|
||||
let icecreams = maybe_icecream(12);
|
||||
assert_eq!(icecreams, 5);
|
||||
assert_eq!(icecreams, Some(5));
|
||||
}
|
||||
}
|
||||
|
||||
@ -20,8 +20,6 @@
|
||||
//
|
||||
// No hints this time!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub enum Command {
|
||||
Uppercase,
|
||||
Trim,
|
||||
@ -32,11 +30,25 @@ mod my_module {
|
||||
use super::Command;
|
||||
|
||||
// TODO: Complete the function signature!
|
||||
pub fn transformer(input: ???) -> ??? {
|
||||
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
|
||||
// TODO: Complete the output declaration!
|
||||
let mut output: ??? = vec![];
|
||||
let mut output: Vec<String> = vec![];
|
||||
for (string, command) in input.iter() {
|
||||
// TODO: Complete the function body. You can do it!
|
||||
match command {
|
||||
Command::Uppercase => {
|
||||
let transformed_string = string.to_uppercase();
|
||||
output.push(transformed_string);
|
||||
}
|
||||
Command::Trim => {
|
||||
let transformed_string = string.trim().to_string();
|
||||
output.push(transformed_string);
|
||||
}
|
||||
Command::Append(n) => {
|
||||
let appended_string = format!("{}{}", string, "bar".repeat(*n));
|
||||
output.push(appended_string);
|
||||
}
|
||||
}
|
||||
}
|
||||
output
|
||||
}
|
||||
@ -44,8 +56,7 @@ mod my_module {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// TODO: What do we need to import to have `transformer` in scope?
|
||||
use ???;
|
||||
use super::my_module::*;
|
||||
use super::Command;
|
||||
|
||||
#[test]
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user