diff --git a/README.md b/README.md index 709ff4c..b0c511b 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | 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_check_mark: | | Day #62 | [Funny Plant](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-62_Funny-Plant) | :white_check_mark: | | Day #63 | [The Rabbit Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-63_The-Rabbit-Problem) | :white_check_mark: | -| Day #64 | [First Recurring Character](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-64_First-Recurring-Character) | :white_large_square: | +| Day #64 | [First Recurring Character](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-64_First-Recurring-Character) | :white_check_mark: | | Day #65 | [ISBN Validator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-65_ISBN-Validator) | :white_large_square: | | Day #66 | [ISBN Generator](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-66_ISBN-Generator) | :white_large_square: | | Day #67 | [Color Maze](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-10/Day-67_Color-Maze) | :white_large_square: | diff --git a/Week-10/Day-64_First-Recurring-Character/day64/Cargo.toml b/Week-10/Day-64_First-Recurring-Character/day64/Cargo.toml new file mode 100644 index 0000000..5a032b2 --- /dev/null +++ b/Week-10/Day-64_First-Recurring-Character/day64/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day64" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-10/Day-64_First-Recurring-Character/day64/src/lib.rs b/Week-10/Day-64_First-Recurring-Character/day64/src/lib.rs new file mode 100644 index 0000000..96ccdd0 --- /dev/null +++ b/Week-10/Day-64_First-Recurring-Character/day64/src/lib.rs @@ -0,0 +1,12 @@ +use std::collections::HashMap; + +pub fn recurring_character(string: &str) -> Option { + let mut chars = HashMap::new(); + for c in string.chars() { + if chars.contains_key(&c) { + return Some(c); + } + chars.entry(c).or_insert(()); + } + None +} diff --git a/Week-10/Day-64_First-Recurring-Character/day64/src/main.rs b/Week-10/Day-64_First-Recurring-Character/day64/src/main.rs new file mode 100644 index 0000000..5980c56 --- /dev/null +++ b/Week-10/Day-64_First-Recurring-Character/day64/src/main.rs @@ -0,0 +1,14 @@ +use std::io::{self, Write}; + +use day64::recurring_character; + +fn main() { + let mut buffer = String::new(); + print!("Enter a string to find its first recurring character: "); + io::stdout().flush().unwrap(); + io::stdin().read_line(&mut buffer).unwrap(); + match recurring_character(buffer.trim()) { + Some(x) => println!("The first recurring character is '{x}'"), + None => println!("The characters are all different from each other"), + } +} diff --git a/Week-10/Day-64_First-Recurring-Character/day64/tests/examples.rs b/Week-10/Day-64_First-Recurring-Character/day64/tests/examples.rs new file mode 100644 index 0000000..30b8432 --- /dev/null +++ b/Week-10/Day-64_First-Recurring-Character/day64/tests/examples.rs @@ -0,0 +1,14 @@ +#[cfg(test)] +mod examples { + use day64::recurring_character; + + #[test] + fn example1() { + assert_eq!(recurring_character("ABCDEBC"), Some('B')); + } + + #[test] + fn example2() { + assert_eq!(recurring_character("ABBA"), Some('B')); + } +}