From 4ed0deba71bce42e1663762e25cf55d54206378f Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Sun, 22 Sep 2024 12:38:48 +0200 Subject: [PATCH] Wrote program for Day 59 --- README.md | 2 +- .../day59/Cargo.toml | 6 +++ .../day59/src/lib.rs | 13 +++++ .../day59/src/main.rs | 14 ++++++ .../day59/tests/examples.rs | 49 +++++++++++++++++++ 5 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 Week-09/Day-59_Perfectly-Balanced/day59/Cargo.toml create mode 100644 Week-09/Day-59_Perfectly-Balanced/day59/src/lib.rs create mode 100644 Week-09/Day-59_Perfectly-Balanced/day59/src/main.rs create mode 100644 Week-09/Day-59_Perfectly-Balanced/day59/tests/examples.rs diff --git a/README.md b/README.md index 48de579..d344079 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_check_mark: | | Day #57 | [Magic Sigil Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-57_Magic-Sigil-Generator) | :white_check_mark: | | Day #58 | [Create A Dice Roller](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-58_Create-A-Dice-Roller) | :white_check_mark: | -| Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_large_square: | +| Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_check_mark: | | Day #60 | [A Game Of Threes](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-60_A-Game-Of-Thrones) | :white_large_square: | | Day #61 | [Write A Web Crawler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-61_Write-A-Web-Crawler) | :white_large_square: | | Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_large_square: | diff --git a/Week-09/Day-59_Perfectly-Balanced/day59/Cargo.toml b/Week-09/Day-59_Perfectly-Balanced/day59/Cargo.toml new file mode 100644 index 0000000..2f36a33 --- /dev/null +++ b/Week-09/Day-59_Perfectly-Balanced/day59/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day59" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-09/Day-59_Perfectly-Balanced/day59/src/lib.rs b/Week-09/Day-59_Perfectly-Balanced/day59/src/lib.rs new file mode 100644 index 0000000..f60b428 --- /dev/null +++ b/Week-09/Day-59_Perfectly-Balanced/day59/src/lib.rs @@ -0,0 +1,13 @@ +use std::collections::HashMap; + +pub fn balanced_bonus(string: &str) -> bool { + let mut counts = HashMap::new(); + for c in string.chars() { + counts.entry(c).and_modify(|e| *e += 1usize).or_insert(1); + } + if string.is_empty() { + return true; + } + let idx = string.chars().next().unwrap(); + counts.iter().all(|x| *x.1 == counts[&idx]) +} diff --git a/Week-09/Day-59_Perfectly-Balanced/day59/src/main.rs b/Week-09/Day-59_Perfectly-Balanced/day59/src/main.rs new file mode 100644 index 0000000..b1a8c7e --- /dev/null +++ b/Week-09/Day-59_Perfectly-Balanced/day59/src/main.rs @@ -0,0 +1,14 @@ +use std::io::{self, Write}; + +use day59::balanced_bonus; + +fn main() { + print!("Enter a string to check if it is balanced: "); + io::stdout().flush().unwrap(); + let mut input = String::new(); + std::io::stdin().read_line(&mut input).unwrap(); + match balanced_bonus(input.trim()) { + true => println!("The string is balanced."), + false => println!("The string is not balanced."), + } +} diff --git a/Week-09/Day-59_Perfectly-Balanced/day59/tests/examples.rs b/Week-09/Day-59_Perfectly-Balanced/day59/tests/examples.rs new file mode 100644 index 0000000..7da5639 --- /dev/null +++ b/Week-09/Day-59_Perfectly-Balanced/day59/tests/examples.rs @@ -0,0 +1,49 @@ +#[cfg(test)] +mod examples { + use day59::balanced_bonus; + + #[test] + fn example1() { + assert!(balanced_bonus("xxxyyyzzz")); + } + + #[test] + fn example2() { + assert!(balanced_bonus("abccbaabccba")); + } + + #[test] + fn example3() { + assert!(!balanced_bonus("xxxyyyzzzz")); + } + + #[test] + fn example4() { + assert!(balanced_bonus("abcdefghijklmnopqrstuvwxyz")); + } + + #[test] + fn example5() { + assert!(!balanced_bonus("pqq")); + } + + #[test] + fn example6() { + assert!(!balanced_bonus("fdedfdeffeddefeeeefddf")); + } + + #[test] + fn example7() { + assert!(balanced_bonus("www")); + } + + #[test] + fn example8() { + assert!(balanced_bonus("x")); + } + + #[test] + fn example9() { + assert!(balanced_bonus("")); + } +}