Skip to content

Commit

Permalink
update 48
Browse files Browse the repository at this point in the history
  • Loading branch information
HGZ-20 committed Oct 20, 2023
1 parent cc83b91 commit 8704bae
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 40 deletions.
10 changes: 8 additions & 2 deletions exercises/hashmaps/hashmaps1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,23 @@
// 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();// TODO: declare your hash map here.

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

// TODO: Put more fruits in your basket here.
basket.insert(String::from("banana"), 2);
basket.insert(String::from("banan"), 2);
basket.insert(String::from("bnana"), 2);
basket.insert(String::from("baana"), 2);
basket.insert(String::from("bana"), 2);
basket.insert(String::from("anana"), 2);

basket
}
Expand Down
5 changes: 4 additions & 1 deletion exercises/hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


use std::collections::HashMap;

Expand All @@ -40,6 +40,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
// 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!
if !basket.contains_key(&fruit) {
basket.insert(fruit, 3);
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion exercises/hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


use std::collections::HashMap;

Expand All @@ -39,6 +39,28 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// 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
// team_1.
if !scores.contains_key(&team_1_name) {
scores.insert(team_1_name, Team{
goals_scored:team_1_score,
goals_conceded:team_2_score});
} else {
let team = scores.get(&team_1_name).unwrap();
scores.insert(team_1_name,
Team{
goals_scored:team.goals_scored + team_1_score,
goals_conceded:team.goals_conceded + team_2_score}
);
}
if !scores.contains_key(&team_2_name) {
scores.insert(team_2_name, Team{goals_scored:team_2_score, goals_conceded:team_1_score});
} else {
let team = scores.get(&team_2_name).unwrap();
scores.insert(team_2_name,
Team{
goals_scored:team.goals_scored + team_2_score,
goals_conceded:team.goals_conceded + team_1_score}
);
}
}
scores
}
Expand Down
4 changes: 2 additions & 2 deletions exercises/modules/modules1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
// Execute `rustlings hint modules1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


mod sausage_factory {
// Don't let anybody outside of this module see this!
fn get_secret_recipe() -> String {
String::from("Ginger")
}

fn make_sausage() {
pub fn make_sausage() {
get_secret_recipe();
println!("sausage!");
}
Expand Down
6 changes: 3 additions & 3 deletions exercises/modules/modules2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// Execute `rustlings hint modules2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


mod delicious_snacks {
// TODO: Fix these use statements
use self::fruits::PEAR as ???
use self::veggies::CUCUMBER as ???
pub use self::fruits::PEAR as fruit;
pub use self::veggies::CUCUMBER as veggie;

mod fruits {
pub const PEAR: &'static str = "Pear";
Expand Down
5 changes: 3 additions & 2 deletions exercises/modules/modules3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
// Execute `rustlings hint modules3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


// TODO: Complete this use statement
use ???
use std::time::SystemTime as SystemTime;
use std::time::UNIX_EPOCH;

fn main() {
match SystemTime::now().duration_since(UNIX_EPOCH) {
Expand Down
12 changes: 9 additions & 3 deletions exercises/options/options1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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
Expand All @@ -13,7 +13,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 {
Some(5)
} else if time_of_day <= 23 {
Some(0)
} else {
None
}
}

#[cfg(test)]
Expand All @@ -33,7 +39,7 @@ mod tests {
fn raw_value() {
// 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).unwrap();
assert_eq!(icecreams, 5);
}
}
6 changes: 3 additions & 3 deletions exercises/options/options2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ mod tests {
let optional_target = Some(target);

// TODO: Make this an if let statement whose value is "Some" type
word = optional_target {
assert_eq!(word, target);
}
let word = optional_target {
assert_eq!(word, target)
};
}

#[test]
Expand Down
17 changes: 13 additions & 4 deletions exercises/quiz2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//
// No hints this time!

// I AM NOT DONE


pub enum Command {
Uppercase,
Expand All @@ -32,11 +32,20 @@ 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 => {
output.push(string.to_uppercase());
},
Command::Trim => output.push(string.trim().to_string()),
Command::Append(x) => {
output.push(string.to_string() + &"bar".repeat(*x));
},
};
}
output
}
Expand All @@ -45,7 +54,7 @@ mod my_module {
#[cfg(test)]
mod tests {
// TODO: What do we need to import to have `transformer` in scope?
use ???;
use crate::my_module::transformer;
use super::Command;

#[test]
Expand Down
3 changes: 1 addition & 2 deletions exercises/strings/strings1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
// Execute `rustlings hint strings1` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

fn main() {
let answer = current_favorite_color();
println!("My current favorite color is {}", answer);
}

fn current_favorite_color() -> String {
"blue"
"blue".to_string()
}
4 changes: 2 additions & 2 deletions exercises/strings/strings2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Execute `rustlings hint strings2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn main() {
let word = String::from("green"); // Try not changing this line :)
if is_a_color_word(word) {
if is_a_color_word(&word) {
println!("That is a color word I know!");
} else {
println!("That is not a color word I know.");
Expand Down
13 changes: 9 additions & 4 deletions exercises/strings/strings3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@
// Execute `rustlings hint strings3` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE


fn trim_me(input: &str) -> String {
// TODO: Remove whitespace from both ends of a string!
???
let tmp = String::from(input);
String::from(tmp.trim())
}

fn compose_me(input: &str) -> String {
// TODO: Add " world!" to the string! There's multiple ways to do this!
???
let mut tmp = String::from(input);
let str2 = String::from(" world!");
tmp += &str2;
tmp
}

fn replace_me(input: &str) -> String {
// TODO: Replace "cars" in the string with "balloons"!
???
let tmp = String::from(input);
tmp.replace("cars", "balloons")
}

#[cfg(test)]
Expand Down
22 changes: 11 additions & 11 deletions exercises/strings/strings4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//
// No hints this time!

// I AM NOT DONE


fn string_slice(arg: &str) {
println!("{}", arg);
Expand All @@ -17,14 +17,14 @@ fn string(arg: String) {
}

fn main() {
???("blue");
???("red".to_string());
???(String::from("hi"));
???("rust is fun!".to_owned());
???("nice weather".into());
???(format!("Interpolation {}", "Station"));
???(&String::from("abc")[0..1]);
???(" hello there ".trim());
???("Happy Monday!".to_string().replace("Mon", "Tues"));
???("mY sHiFt KeY iS sTiCkY".to_lowercase());
string_slice("blue");
string("red".to_string());
string(String::from("hi"));
string("rust is fun!".to_owned());
string_slice("nice weather".into());
string(format!("Interpolation {}", "Station"));
string_slice(&String::from("abc")[0..1]);
string_slice(" hello there ".trim());
string("Happy Monday!".to_string().replace("Mon", "Tues"));
string("mY sHiFt KeY iS sTiCkY".to_lowercase());
}
1 change: 1 addition & 0 deletions rustlings
Submodule rustlings added at 9a743f

0 comments on commit 8704bae

Please sign in to comment.