update:done the quiz2 and options1

This commit is contained in:
catwithtudou 2023-10-25 10:56:30 +08:00
parent b2b1afcc42
commit 5ec35b41d5
5 changed files with 42 additions and 18 deletions

View File

@ -11,15 +11,15 @@
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
// hint. // 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 :) // Two bananas are already given for you :)
basket.insert(String::from("banana"), 2); 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. // TODO: Put more fruits in your basket here.

View File

@ -14,8 +14,6 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
#[derive(Hash, PartialEq, Eq)] #[derive(Hash, PartialEq, Eq)]
@ -37,6 +35,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
]; ];
for fruit in fruit_kinds { for fruit in fruit_kinds {
basket.entry(fruit).or_insert(1);
// TODO: Insert new fruits if they are not already present in the // 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 // basket. Note that you are not allowed to put any type of fruit that's
// already present! // already present!
@ -81,7 +80,7 @@ mod tests {
let count = basket.values().sum::<u32>(); let count = basket.values().sum::<u32>();
assert!(count > 11); assert!(count > 11);
} }
#[test] #[test]
fn all_fruit_types_in_basket() { fn all_fruit_types_in_basket() {
let mut basket = get_fruit_basket(); let mut basket = get_fruit_basket();

View File

@ -14,8 +14,6 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
use std::collections::HashMap; use std::collections::HashMap;
// A structure to store the goal details of a team. // 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_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();
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 // TODO: Populate the scores table with details extracted from the
// current line. Keep in mind that goals scored by team_1 // current line. Keep in mind that goals scored by team_1
// will be the number of goals conceded from team_2, and similarly // will be the number of goals conceded from team_2, and similarly

View File

@ -3,8 +3,6 @@
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a // Execute `rustlings hint options1` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
// This function returns how much icecream there is left in the fridge. // 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 // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them
// all, so there'll be no more left :( // 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 // value of 0 The Option output should gracefully handle cases where
// time_of_day > 23. // time_of_day > 23.
// TODO: Complete the function body - remember to return an Option! // 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)] #[cfg(test)]
@ -34,6 +38,6 @@ mod tests {
// TODO: Fix this test. How do you get at the value contained in the // TODO: Fix this test. How do you get at the value contained in the
// Option? // Option?
let icecreams = maybe_icecream(12); let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5); assert_eq!(icecreams, Some(5));
} }
} }

View File

@ -20,8 +20,6 @@
// //
// No hints this time! // No hints this time!
// I AM NOT DONE
pub enum Command { pub enum Command {
Uppercase, Uppercase,
Trim, Trim,
@ -32,11 +30,25 @@ mod my_module {
use super::Command; use super::Command;
// TODO: Complete the function signature! // TODO: Complete the function signature!
pub fn transformer(input: ???) -> ??? { pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
// TODO: Complete the output declaration! // TODO: Complete the output declaration!
let mut output: ??? = vec![]; let mut output: Vec<String> = vec![];
for (string, command) in input.iter() { for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it! // 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 output
} }
@ -44,8 +56,7 @@ mod my_module {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// TODO: What do we need to import to have `transformer` in scope? use super::my_module::*;
use ???;
use super::Command; use super::Command;
#[test] #[test]