From bf3efe73e070967d3d025bdf2a1f79a29bedb255 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Fri, 9 Aug 2024 11:45:11 +0200 Subject: [PATCH] Wrote program for Day 15 --- README.md | 2 +- Week-03/Day-15_Valid-Anagram/day15/Cargo.toml | 6 ++++ Week-03/Day-15_Valid-Anagram/day15/src/lib.rs | 14 +++++++++ .../Day-15_Valid-Anagram/day15/src/main.rs | 30 +++++++++++++++++++ .../day15/tests/examples.rs | 11 +++++++ 5 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 Week-03/Day-15_Valid-Anagram/day15/Cargo.toml create mode 100644 Week-03/Day-15_Valid-Anagram/day15/src/lib.rs create mode 100644 Week-03/Day-15_Valid-Anagram/day15/src/main.rs create mode 100644 Week-03/Day-15_Valid-Anagram/day15/tests/examples.rs diff --git a/README.md b/README.md index 888aade..a6d53c7 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #12 | [Mountains or Valleys](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-12_Mountains_And_Valleys) | :white_check_mark: | | Day #13 | [Need Help With Your Packing](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-13_Need-Help-With-Packing) | :white_check_mark: | | Day #14 | [The Karacas Encryption Algorithm](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-02/Day-14_Karacas-Encryption-Algorithm) | :white_check_mark: | -| Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_large_square: | +| Day #15 | [Valid Anagram](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-15_Valid-Anagram) | :white_check_mark: | | Day #16 | [Nim Game](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-16_Nim-Game) | :white_large_square: | | Day #17 | [Prison Break](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-17_Prison-Break) | :white_large_square: | | Day #18 | [Unique Paths](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-03/Day-18_Unique-Paths) | :white_large_square: | diff --git a/Week-03/Day-15_Valid-Anagram/day15/Cargo.toml b/Week-03/Day-15_Valid-Anagram/day15/Cargo.toml new file mode 100644 index 0000000..203eb55 --- /dev/null +++ b/Week-03/Day-15_Valid-Anagram/day15/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day15" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-03/Day-15_Valid-Anagram/day15/src/lib.rs b/Week-03/Day-15_Valid-Anagram/day15/src/lib.rs new file mode 100644 index 0000000..b6d25e9 --- /dev/null +++ b/Week-03/Day-15_Valid-Anagram/day15/src/lib.rs @@ -0,0 +1,14 @@ +pub fn is_anagram(s: &str, t: &str) -> bool { + let mut t = t.to_string(); + + for c in s.chars() { + if let Some(index) = t.find(c) { + let (before, after) = t.split_at(index); + t = format!("{}{}", before, &after[1..]); + } else { + return false; + } + } + + true +} diff --git a/Week-03/Day-15_Valid-Anagram/day15/src/main.rs b/Week-03/Day-15_Valid-Anagram/day15/src/main.rs new file mode 100644 index 0000000..7138078 --- /dev/null +++ b/Week-03/Day-15_Valid-Anagram/day15/src/main.rs @@ -0,0 +1,30 @@ +use std::io::{self, Write}; + +use day15::is_anagram; + +fn main() { + let mut buffer = String::new(); + + print!("Insert the first string: "); + io::stdout().flush().expect("Failed to flush stdout."); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read from input"); + + let s = buffer.trim(); + let mut buffer = String::new(); + + print!("Insert the first string: "); + io::stdout().flush().expect("Failed to flush stdout."); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read from input"); + + if is_anagram(s, buffer.trim()) { + println!("{s} is the anagram of {buffer}"); + } else { + println!("{s} is not the anagram of {buffer}") + } +} diff --git a/Week-03/Day-15_Valid-Anagram/day15/tests/examples.rs b/Week-03/Day-15_Valid-Anagram/day15/tests/examples.rs new file mode 100644 index 0000000..4ecf4e9 --- /dev/null +++ b/Week-03/Day-15_Valid-Anagram/day15/tests/examples.rs @@ -0,0 +1,11 @@ +use day15::is_anagram; + +#[test] +fn example1() { + assert!(is_anagram("anagram", "nagaram")); +} + +#[test] +fn example2() { + assert!(!is_anagram("rat", "car")); +}