From 59b38baace107dcb4ff85befeebf52c5fa527617 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Wed, 18 Sep 2024 09:14:17 +0200 Subject: [PATCH] Wrote program for Day 55 --- README.md | 2 +- .../day55/Cargo.toml | 6 ++++ .../day55/src/lib.rs | 10 +++++++ .../day55/src/main.rs | 30 +++++++++++++++++++ .../day55/tests/examples.rs | 23 ++++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 Week-08/Day-55_Filter_Repeating-Character-Strings/day55/Cargo.toml create mode 100644 Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/lib.rs create mode 100644 Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/main.rs create mode 100644 Week-08/Day-55_Filter_Repeating-Character-Strings/day55/tests/examples.rs diff --git a/README.md b/README.md index d9e8dab..751502f 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #52 | [Switch On The Gravity](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-52_Switch-On-The-Gravity) | :white_check_mark: | | Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_check_mark: | | Day #54 | [RGB To Hex Color Convertor](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-54_RGB-To-Hex-Color-Converter) | :white_check_mark: | -| Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_large_square: | +| Day #55 | [Filter Repeating Character Strings](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-55_Filter_Repeating-Character-Strings) | :white_check_mark: | | Day #56 | [Convert To Hex](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-56_Convert-To-Hex) | :white_large_square: | | Day #57 | [Magic Sigil Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-57_Magic-Sigil-Generator) | :white_large_square: | | 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_large_square: | diff --git a/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/Cargo.toml b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/Cargo.toml new file mode 100644 index 0000000..35cb8bf --- /dev/null +++ b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day55" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/lib.rs b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/lib.rs new file mode 100644 index 0000000..942a150 --- /dev/null +++ b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/lib.rs @@ -0,0 +1,10 @@ +pub fn identical_filter<'a>(strings: &'a [&str]) -> Vec<&'a str> { + strings + .iter() + .filter(|x| { + x.chars() + .all(|c| x.is_empty() || c == x.chars().next().unwrap()) + }) + .cloned() + .collect() +} diff --git a/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/main.rs b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/main.rs new file mode 100644 index 0000000..d049227 --- /dev/null +++ b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/src/main.rs @@ -0,0 +1,30 @@ +use std::process::exit; + +use day55::identical_filter; + +fn read_strings() -> Result, Box> { + let mut strings = Vec::new(); + loop { + let mut input = String::new(); + std::io::stdin().read_line(&mut input)?; + if input.trim().is_empty() { + break; + } + strings.push(input.trim().to_string()); + } + Ok(strings) +} + +fn main() { + println!("Enter a list of strings separated by newlines. Press Enter twice to finish."); + let strings = match read_strings() { + Ok(strings) => strings, + Err(err) => { + eprintln!("Error reading strings: {}", err); + exit(1); + } + }; + let strings = strings.iter().map(|x| x.as_str()).collect::>(); + let filtered = identical_filter(&strings); + println!("Filtered strings: {:?}", filtered); +} diff --git a/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/tests/examples.rs b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/tests/examples.rs new file mode 100644 index 0000000..5f102a4 --- /dev/null +++ b/Week-08/Day-55_Filter_Repeating-Character-Strings/day55/tests/examples.rs @@ -0,0 +1,23 @@ +#[cfg(test)] +mod examples { + use day55::identical_filter; + + #[test] + fn test_1() { + assert_eq!( + identical_filter(&["aaaaaa", "bc", "d", "eeee", "xyz"]), + ["aaaaaa", "d", "eeee"] + ); + } + #[test] + fn test_2() { + assert_eq!( + identical_filter(&["88", "999", "22", "545", "133"]), + ["88", "999", "22"] + ); + } + #[test] + fn test_3() { + assert!(identical_filter(&["xxxxo", "oxo", "xox", "ooxxoo", "oxo"]).is_empty()); + } +}