diff --git a/README.md b/README.md index 6654d4e..4f6ade6 100644 --- a/README.md +++ b/README.md @@ -76,7 +76,7 @@ We encourage you to share your progress and ask questions in the Discussions sec | Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_check_mark: | | Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_check_mark: | | Day #29 | [Traffic Light Checker](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-29_Traffic-Light-Checker) | :white_check_mark: | -| Day #30 | [The Maximum Value](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-30_The-Maximum-Value) | :white_large_square: | +| 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_large_square: | | Day #32 | [Climbing The Leaderboard](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-32_Climbing-The-Leaderboard) | :white_large_square: | | Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_large_square: | diff --git a/Week-05/Day-30_The-Maximum-Value/day30/Cargo.toml b/Week-05/Day-30_The-Maximum-Value/day30/Cargo.toml new file mode 100644 index 0000000..80ad842 --- /dev/null +++ b/Week-05/Day-30_The-Maximum-Value/day30/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day30" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/Week-05/Day-30_The-Maximum-Value/day30/src/lib.rs b/Week-05/Day-30_The-Maximum-Value/day30/src/lib.rs new file mode 100644 index 0000000..5df9d2d --- /dev/null +++ b/Week-05/Day-30_The-Maximum-Value/day30/src/lib.rs @@ -0,0 +1,27 @@ +pub fn max_value(mut n: isize, digit: char) -> Result { + let digit = match digit.to_digit(10) { + Some(d) => Ok(d as isize), + None => Err("The digit is not valid"), + }?; + + let negative = n < 0; + if negative { + n *= -1; + } + + let nlen = (n.abs() as f32).log10().floor() as u32 + 1; + let mut max = n * 10 + digit; + + let cmp = if negative { + std::cmp::min + } else { + std::cmp::max + }; + + for i in 1..=nlen { + let option = (n / 10isize.pow(i) * 10 + digit) * 10isize.pow(i) + n % 10isize.pow(i); + max = cmp(max, option); + } + + Ok(if negative { -max } else { max }) +} diff --git a/Week-05/Day-30_The-Maximum-Value/day30/src/main.rs b/Week-05/Day-30_The-Maximum-Value/day30/src/main.rs new file mode 100644 index 0000000..41e0e10 --- /dev/null +++ b/Week-05/Day-30_The-Maximum-Value/day30/src/main.rs @@ -0,0 +1,46 @@ +use std::io::{self, Write}; +use std::process::exit; + +use day30::max_value; + +fn main() { + let mut buffer = String::new(); + + print!("Insert the starting number: "); + io::stdout().flush().expect("Failed to flush stdout"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let n: isize = match buffer.trim().parse() { + Ok(n) => n, + Err(_) => { + eprintln!("Invalid number"); + exit(1); + } + }; + + buffer.clear(); + + print!("Insert the digit to add to the number: "); + io::stdout().flush().expect("Failed to flush stdout"); + + io::stdin() + .read_line(&mut buffer) + .expect("Failed to read line"); + + let digit: char = match buffer.trim().parse() { + Ok(digit) => digit, + Err(_) => { + eprintln!("Invalid digit"); + exit(1); + } + }; + + let result = max_value(n, digit); + match result { + Ok(result) => println!("The maximum value possible is: {}", result), + Err(e) => eprintln!("Error: {}", e), + } +} diff --git a/Week-05/Day-30_The-Maximum-Value/day30/tests/examples.rs b/Week-05/Day-30_The-Maximum-Value/day30/tests/examples.rs new file mode 100644 index 0000000..cce62f8 --- /dev/null +++ b/Week-05/Day-30_The-Maximum-Value/day30/tests/examples.rs @@ -0,0 +1,24 @@ +#[cfg(test)] +mod examples { + use day30::max_value; + + #[test] + fn example1() { + assert_eq!(max_value(276, '3'), Ok(3276)); + } + + #[test] + fn example2() { + assert_eq!(max_value(-999, '4'), Ok(-4999)); + } + + #[test] + fn example3() { + assert_eq!(max_value(0, '3'), Ok(30)); + } + + #[test] + fn example4() { + assert_eq!(max_value(860, '7'), Ok(8760)); + } +}