From c3ccae96d637d6c0170df1e1bfbb418ab84deeca Mon Sep 17 00:00:00 2001 From: Mariano Riefolo Date: Wed, 28 Aug 2024 11:39:14 +0200 Subject: [PATCH] Wrote program for Day 34 --- README.md | 2 +- .../day34/Cargo.toml | 6 +++ .../day34/src/lib.rs | 22 ++++++++++ .../day34/src/main.rs | 42 +++++++++++++++++++ .../day34/tests/examples.rs | 34 +++++++++++++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 Week-05/Day-34_Primary-Arithmetic/day34/Cargo.toml create mode 100644 Week-05/Day-34_Primary-Arithmetic/day34/src/lib.rs create mode 100644 Week-05/Day-34_Primary-Arithmetic/day34/src/main.rs create mode 100644 Week-05/Day-34_Primary-Arithmetic/day34/tests/examples.rs diff --git a/README.md b/README.md index 209f8c6..9e57854 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | 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_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 #34 | [Primary Arithmetic](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-34_Primary-Arithmetic) | :white_check_mark: | | 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: | | Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_large_square: | diff --git a/Week-05/Day-34_Primary-Arithmetic/day34/Cargo.toml b/Week-05/Day-34_Primary-Arithmetic/day34/Cargo.toml new file mode 100644 index 0000000..60fb08f --- /dev/null +++ b/Week-05/Day-34_Primary-Arithmetic/day34/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day34" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-05/Day-34_Primary-Arithmetic/day34/src/lib.rs b/Week-05/Day-34_Primary-Arithmetic/day34/src/lib.rs new file mode 100644 index 0000000..756f8ab --- /dev/null +++ b/Week-05/Day-34_Primary-Arithmetic/day34/src/lib.rs @@ -0,0 +1,22 @@ +pub fn carry_operations(num1: usize, num2: usize) -> u8 { + let mut result = 0; + + let mut bigger = std::cmp::max(num1, num2); + let mut smaller = std::cmp::min(num1, num2); + + let mut next_reminder = 0; + + while smaller > 0 { + let remainder = bigger % 10 + smaller % 10 + next_reminder; + + bigger /= 10; + smaller /= 10; + + if remainder > 9 { + result += 1; + next_reminder += remainder / 10; + } + } + + result +} diff --git a/Week-05/Day-34_Primary-Arithmetic/day34/src/main.rs b/Week-05/Day-34_Primary-Arithmetic/day34/src/main.rs new file mode 100644 index 0000000..a2c0aed --- /dev/null +++ b/Week-05/Day-34_Primary-Arithmetic/day34/src/main.rs @@ -0,0 +1,42 @@ +use std::{ + io::{self, Write}, + process::exit, +}; + +use day34::carry_operations; + +fn read_usize(request: &str) -> Result { + print!("{}", request); + io::stdout().flush().expect("Failed to flush stdout"); + + let mut buffer = String::new(); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + match buffer.trim().parse() { + Ok(x) => Ok(x), + Err(_) => Err("Your input is not an unsigned integer"), + } +} + +fn main() { + let num1 = match read_usize("Insert the first number: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{}", e); + exit(1); + } + }; + let num2 = match read_usize("Insert the second number: ") { + Ok(x) => x, + Err(e) => { + eprintln!("{}", e); + exit(1); + } + }; + + let result = carry_operations(num1, num2); + println!("Carry operations: {}", result); +} diff --git a/Week-05/Day-34_Primary-Arithmetic/day34/tests/examples.rs b/Week-05/Day-34_Primary-Arithmetic/day34/tests/examples.rs new file mode 100644 index 0000000..8e78853 --- /dev/null +++ b/Week-05/Day-34_Primary-Arithmetic/day34/tests/examples.rs @@ -0,0 +1,34 @@ +#[cfg(test)] +mod examples { + use day34::carry_operations; + + #[test] + fn example1() { + assert_eq!(carry_operations(123, 456), 0); + } + + #[test] + fn example2() { + assert_eq!(carry_operations(555, 555), 3); + } + + #[test] + fn example3() { + assert_eq!(carry_operations(123, 594), 1); + } + + #[test] + fn example4() { + assert_eq!(carry_operations(555, 545), 3); + } + + #[test] + fn example5() { + assert_eq!(carry_operations(1, 20000), 0); + } + + #[test] + fn example6() { + assert_eq!(carry_operations(1, 2), 0); + } +}