finished quiz 2 and started options

This commit is contained in:
Abrar Habib 2023-03-30 15:15:21 -04:00
parent 9142f52e2e
commit d722b0c2e3
4 changed files with 47 additions and 12 deletions

View File

@ -14,7 +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;
@ -40,12 +39,24 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded from team_2, and similarly // 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 // goals scored by team_2 will be the number of goals conceded by
// team_1. // team_1.
scores.entry(team_1_name).or_insert(Team{ // If the key doesn't exist, initialize the goals to be 0. then add the goals.
let score1 = scores.entry(team_1_name.clone()).or_insert(Team{
name: team_1_name, name: team_1_name,
goals_scored: team_1_score, goals_scored: 0,
goals_conceded: team_2_score goals_conceded: 0
}); });
score1.goals_scored += team_1_score;
score1.goals_conceded += team_2_score;
let score2 = scores.entry(team_2_name.clone()).or_insert(Team{
name: team_2_name,
goals_scored: 0,
goals_conceded: 0
});
score2.goals_scored += team_2_score;
score2.goals_conceded += team_1_score;
} }
scores scores
} }

View File

@ -1,7 +1,8 @@
// options1.rs // options1.rs
// Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint. // Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint.
// I AM NOT DONE
use std::time;
// 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
@ -10,7 +11,20 @@ fn maybe_icecream(time_of_day: u16) -> Option<u16> {
// We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0 // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0
// The Option output should gracefully handle cases where time_of_day > 23. // The Option output should gracefully handle cases where time_of_day > 23.
// TODO: Complete the function body - remember to return an Option! // TODO: Complete the function body - remember to return an Option!
???
// match time_of_day {
// 0..=21 => Some(5),
// 22..=24 => Some(0),
// _ => None,
// }
if time_of_day < 22 {
Some(5)
} else if time_of_day < 24 {
Some(0)
} else {
None
}
} }
#[cfg(test)] #[cfg(test)]
@ -30,6 +44,6 @@ mod tests {
fn raw_value() { fn raw_value() {
// TODO: Fix this test. How do you get at the value contained in the Option? // TODO: Fix this test. How do you get at the value contained in the Option?
let icecreams = maybe_icecream(12); let icecreams = maybe_icecream(12);
assert_eq!(icecreams, 5); assert_eq!(icecreams, Some(5));
} }
} }

View File

@ -11,7 +11,7 @@ mod tests {
let optional_target = Some(target); let optional_target = Some(target);
// TODO: Make this an if let statement whose value is "Some" type // TODO: Make this an if let statement whose value is "Some" type
word = optional_target { if let Some(word) = target {
assert_eq!(word, target); assert_eq!(word, target);
} }
} }

View File

@ -18,7 +18,6 @@
// - The output element is going to be a Vector of strings. // - The output element is going to be a Vector of strings.
// No hints this time! // No hints this time!
// I AM NOT DONE
pub enum Command { pub enum Command {
Uppercase, Uppercase,
@ -30,21 +29,32 @@ 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 => output.push(string.to_uppercase().to_string()),
Command::Append(n) => output.push(push_bar(string, *n)),
Command::Trim => output.push(string.trim().to_string()),
}
} }
output output
} }
fn push_bar(string: &str, n: usize) -> String {
let bar = "bar".repeat(n);
let mut res = String::from(string.to_string() + &bar);
res
}
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
// TODO: What do we need to import to have `transformer` in scope? // TODO: What do we need to import to have `transformer` in scope?
use ???;
use super::Command; use super::Command;
use super::my_module::transformer;
#[test] #[test]
fn it_works() { fn it_works() {