From 7b89e4760ac404fc8d0629ce71011b0873b45273 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Fri, 20 Sep 2024 09:10:36 +0200 Subject: [PATCH] Wrote program for Day 57 --- README.md | 2 +- .../day57/Cargo.toml | 6 ++++ .../day57/src/lib.rs | 20 +++++++++++++ .../day57/src/main.rs | 11 +++++++ .../day57/tests/examples.rs | 29 +++++++++++++++++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 Week-09/Day-57_Magic-Sigil-Generator/day57/Cargo.toml create mode 100644 Week-09/Day-57_Magic-Sigil-Generator/day57/src/lib.rs create mode 100644 Week-09/Day-57_Magic-Sigil-Generator/day57/src/main.rs create mode 100644 Week-09/Day-57_Magic-Sigil-Generator/day57/tests/examples.rs diff --git a/README.md b/README.md index f53b2f3..40b465d 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | 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_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_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_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_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_large_square: | | Day #59 | [Perfectly Balanced](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-09/Day-59_Perfectly-Balanced) | :white_large_square: | | 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: | diff --git a/Week-09/Day-57_Magic-Sigil-Generator/day57/Cargo.toml b/Week-09/Day-57_Magic-Sigil-Generator/day57/Cargo.toml new file mode 100644 index 0000000..111e808 --- /dev/null +++ b/Week-09/Day-57_Magic-Sigil-Generator/day57/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day57" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-09/Day-57_Magic-Sigil-Generator/day57/src/lib.rs b/Week-09/Day-57_Magic-Sigil-Generator/day57/src/lib.rs new file mode 100644 index 0000000..0ecdcf6 --- /dev/null +++ b/Week-09/Day-57_Magic-Sigil-Generator/day57/src/lib.rs @@ -0,0 +1,20 @@ +use std::collections::HashMap; + +pub fn sigilize(desire: &str) -> String { + let vowels = ['A', 'E', 'I', 'O', 'U']; + let mut found = HashMap::new(); + let mut result = String::new(); + for ch in desire + .to_uppercase() + .split_whitespace() + .collect::() + .chars() + .rev() + { + if !vowels.iter().any(|c| *c == ch) && !found.contains_key(&ch) { + result.push(ch); + found.entry(ch).or_insert(()); + } + } + result.chars().rev().collect() +} diff --git a/Week-09/Day-57_Magic-Sigil-Generator/day57/src/main.rs b/Week-09/Day-57_Magic-Sigil-Generator/day57/src/main.rs new file mode 100644 index 0000000..6ab2bbf --- /dev/null +++ b/Week-09/Day-57_Magic-Sigil-Generator/day57/src/main.rs @@ -0,0 +1,11 @@ +use std::io::{self, Write}; + +use day57::sigilize; + +fn main() { + print!("Enter your desire: "); + io::stdout().flush().unwrap(); + let mut desire = String::new(); + io::stdin().read_line(&mut desire).unwrap(); + println!("Sigilized desire: {}", sigilize(desire.trim())); +} diff --git a/Week-09/Day-57_Magic-Sigil-Generator/day57/tests/examples.rs b/Week-09/Day-57_Magic-Sigil-Generator/day57/tests/examples.rs new file mode 100644 index 0000000..905c2e8 --- /dev/null +++ b/Week-09/Day-57_Magic-Sigil-Generator/day57/tests/examples.rs @@ -0,0 +1,29 @@ +/* +sigilize("i am healthy") ➞ "MLTHY" + +sigilize("I FOUND MY SOULMATE") ➞ "FNDYSLMT" + +sigilize("I have a job I enjoy and it pays well") ➞ "HVBJNDTPYSWL" +*/ +#[cfg(test)] +mod examples { + use day57::sigilize; + + #[test] + fn example1() { + assert_eq!(sigilize("i am healthy"), "MLTHY"); + } + + #[test] + fn example2() { + assert_eq!(sigilize("I FOUND MY SOULMATE"), "FNDYSLMT"); + } + + #[test] + fn example3() { + assert_eq!( + sigilize("I have a job I enjoy and it pays well"), + "HVBJNDTPYSWL" + ); + } +}