Wrote program for Day 64

This commit is contained in:
Mariano Riefolo 2024-09-27 09:30:59 +02:00
parent 72ad5ee38a
commit 3fc852ef4d
5 changed files with 47 additions and 1 deletions

View File

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

View File

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

View File

@ -0,0 +1,12 @@
use std::collections::HashMap;
pub fn recurring_character(string: &str) -> Option<char> {
let mut chars = HashMap::new();
for c in string.chars() {
if chars.contains_key(&c) {
return Some(c);
}
chars.entry(c).or_insert(());
}
None
}

View File

@ -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"),
}
}

View File

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