From 5eb41602b951bd0cf61da42b667838ab142331cc Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Tue, 27 Aug 2024 12:02:27 +0200 Subject: [PATCH] Wrote program for Day 33 --- README.md | 2 +- Week-05/Day-33_WERTYU/day33/Cargo.toml | 6 ++++++ Week-05/Day-33_WERTYU/day33/src/lib.rs | 19 +++++++++++++++++++ Week-05/Day-33_WERTYU/day33/src/main.rs | 19 +++++++++++++++++++ Week-05/Day-33_WERTYU/day33/tests/example.rs | 9 +++++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 Week-05/Day-33_WERTYU/day33/Cargo.toml create mode 100644 Week-05/Day-33_WERTYU/day33/src/lib.rs create mode 100644 Week-05/Day-33_WERTYU/day33/src/main.rs create mode 100644 Week-05/Day-33_WERTYU/day33/tests/example.rs diff --git a/README.md b/README.md index beeb11b..209f8c6 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_check_mark: | | Day #31 | [The Time In Words](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-31_The-Time-In-Words) | :white_check_mark: | | Day #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_check_mark: | -| Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_large_square: | +| Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_check_mark: | | Day #34 | [Primary Arithmetic](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-34_Primary-Arithmetic) | :white_large_square: | | Day #35 | [Dog And Gopher](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-35_Dog-And-Gopher) | :white_large_square: | | Day #36 | [LCD Display](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-36_LCD-Display) | :white_large_square: | diff --git a/Week-05/Day-33_WERTYU/day33/Cargo.toml b/Week-05/Day-33_WERTYU/day33/Cargo.toml new file mode 100644 index 0000000..64dfc88 --- /dev/null +++ b/Week-05/Day-33_WERTYU/day33/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day33" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-05/Day-33_WERTYU/day33/src/lib.rs b/Week-05/Day-33_WERTYU/day33/src/lib.rs new file mode 100644 index 0000000..c5479e3 --- /dev/null +++ b/Week-05/Day-33_WERTYU/day33/src/lib.rs @@ -0,0 +1,19 @@ +pub fn keyboard_mistake_fix(shifted: &str) -> String { + let error = "wertyuiop[]\\sdfghjkl;'xcvbnm,./".to_uppercase(); + let correct = "qwertyuiop[]asdfghjkl;zxcvbnm,.".to_uppercase(); + + assert_eq!(error.len(), correct.len()); + + let shifted = shifted.to_uppercase(); + + let mut fixed = String::new(); + for c in shifted.chars() { + if let Some(i) = error.find(c) { + fixed.push(correct.chars().nth(i).unwrap()); + } else { + fixed.push(c); + } + } + + fixed +} diff --git a/Week-05/Day-33_WERTYU/day33/src/main.rs b/Week-05/Day-33_WERTYU/day33/src/main.rs new file mode 100644 index 0000000..086a1a3 --- /dev/null +++ b/Week-05/Day-33_WERTYU/day33/src/main.rs @@ -0,0 +1,19 @@ +use day33::keyboard_mistake_fix; +use std::io::{self, Write}; + +fn main() { + println!("Insert the text you wrote with your hands on the keyboard one row to the right of the correct position."); + print!("> "); + io::stdout().flush().expect("Failed to flush"); + + let mut buffer = String::new(); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let shifted = buffer.trim(); + + let fixed = keyboard_mistake_fix(shifted); + println!("The text you should have typed: {}", fixed); +} diff --git a/Week-05/Day-33_WERTYU/day33/tests/example.rs b/Week-05/Day-33_WERTYU/day33/tests/example.rs new file mode 100644 index 0000000..e93f26a --- /dev/null +++ b/Week-05/Day-33_WERTYU/day33/tests/example.rs @@ -0,0 +1,9 @@ +#[cfg(test)] +mod example { + use day33::keyboard_mistake_fix; + + #[test] + fn test_example() { + assert_eq!(keyboard_mistake_fix("O S, GOMR YPFSU/"), "I AM FINE TODAY."); + } +}