From 22bf0a9d519fbcbd17317a6344aaed9c07ab11c4 Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Thu, 19 Sep 2024 08:42:30 +0200 Subject: [PATCH] Wrote program for Day 56 --- README.md | 2 +- .../Day-56_Convert-To-Hex/day56/Cargo.toml | 6 +++++ .../Day-56_Convert-To-Hex/day56/src/lib.rs | 11 ++++++++ .../Day-56_Convert-To-Hex/day56/src/main.rs | 12 +++++++++ .../day56/tests/examples.rs | 27 +++++++++++++++++++ 5 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Week-08/Day-56_Convert-To-Hex/day56/Cargo.toml create mode 100644 Week-08/Day-56_Convert-To-Hex/day56/src/lib.rs create mode 100644 Week-08/Day-56_Convert-To-Hex/day56/src/main.rs create mode 100644 Week-08/Day-56_Convert-To-Hex/day56/tests/examples.rs diff --git a/README.md b/README.md index 751502f..f53b2f3 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #53 | [Javelin Parabolic Throw](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-08/Day-53_Javelin-Parabolic-Throw) | :white_check_mark: | | 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_large_square: | +| 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 #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: | diff --git a/Week-08/Day-56_Convert-To-Hex/day56/Cargo.toml b/Week-08/Day-56_Convert-To-Hex/day56/Cargo.toml new file mode 100644 index 0000000..86a05f7 --- /dev/null +++ b/Week-08/Day-56_Convert-To-Hex/day56/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day56" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-08/Day-56_Convert-To-Hex/day56/src/lib.rs b/Week-08/Day-56_Convert-To-Hex/day56/src/lib.rs new file mode 100644 index 0000000..1ac3cb9 --- /dev/null +++ b/Week-08/Day-56_Convert-To-Hex/day56/src/lib.rs @@ -0,0 +1,11 @@ +use std::fmt::Write; + +pub fn to_hex(text: &str) -> String { + text.chars() + .fold(String::new(), |mut output, b| { + let _ = write!(&mut output, "{:02x} ", b as u8); + output + }) + .trim() + .to_owned() +} diff --git a/Week-08/Day-56_Convert-To-Hex/day56/src/main.rs b/Week-08/Day-56_Convert-To-Hex/day56/src/main.rs new file mode 100644 index 0000000..f646ee8 --- /dev/null +++ b/Week-08/Day-56_Convert-To-Hex/day56/src/main.rs @@ -0,0 +1,12 @@ +use std::io::{self, Write}; + +use day56::to_hex; + +fn main() { + print!("Enter a string: "); + io::stdout().flush().unwrap(); + let mut buffer = String::new(); + io::stdin().read_line(&mut buffer).unwrap(); + let text = buffer.trim(); + println!("Hexadecimal representation: {}", to_hex(text)); +} diff --git a/Week-08/Day-56_Convert-To-Hex/day56/tests/examples.rs b/Week-08/Day-56_Convert-To-Hex/day56/tests/examples.rs new file mode 100644 index 0000000..2f39ca1 --- /dev/null +++ b/Week-08/Day-56_Convert-To-Hex/day56/tests/examples.rs @@ -0,0 +1,27 @@ +/* +toHex("hello world") ➞ "68 65 6c 6c 6f 20 77 6f 72 6c 64" + +toHex("Big Boi") ➞ "42 69 67 20 42 6f 69" + +toHex("Marty Poppinson") ➞ "4d 61 72 74 79 20 50 6f 70 70 69 6e 73 6f 6e" +*/ +#[cfg(test)] +mod examples { + use day56::to_hex; + + #[test] + fn test_1() { + assert_eq!(to_hex("hello world"), "68 65 6c 6c 6f 20 77 6f 72 6c 64"); + } + #[test] + fn test_2() { + assert_eq!(to_hex("Big Boi"), "42 69 67 20 42 6f 69"); + } + #[test] + fn test_3() { + assert_eq!( + to_hex("Marty Poppinson"), + "4d 61 72 74 79 20 50 6f 70 70 69 6e 73 6f 6e" + ); + } +}