Wrote program for Day 55

This commit is contained in:
Mariano Riefolo 2024-09-18 09:14:17 +02:00
parent e1c0448375
commit 59b38baace
5 changed files with 70 additions and 1 deletions

View File

@ -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: |

View File

@ -0,0 +1,6 @@
[package]
name = "day55"
version = "0.1.0"
edition = "2021"
[dependencies]

View File

@ -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()
}

View File

@ -0,0 +1,30 @@
use std::process::exit;
use day55::identical_filter;
fn read_strings() -> Result<Vec<String>, Box<dyn std::error::Error>> {
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::<Vec<&str>>();
let filtered = identical_filter(&strings);
println!("Filtered strings: {:?}", filtered);
}

View File

@ -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());
}
}