Wrote program for Day 30
This commit is contained in:
parent
9f259a0850
commit
5df229b02f
@ -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 #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 #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 #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 #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 #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: |
|
| Day #33 | [WERTYU](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-33_WERTYU) | :white_large_square: |
|
||||||
|
6
Week-05/Day-30_The-Maximum-Value/day30/Cargo.toml
Normal file
6
Week-05/Day-30_The-Maximum-Value/day30/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day30"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
27
Week-05/Day-30_The-Maximum-Value/day30/src/lib.rs
Normal file
27
Week-05/Day-30_The-Maximum-Value/day30/src/lib.rs
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
pub fn max_value(mut n: isize, digit: char) -> Result<isize, String> {
|
||||||
|
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 })
|
||||||
|
}
|
46
Week-05/Day-30_The-Maximum-Value/day30/src/main.rs
Normal file
46
Week-05/Day-30_The-Maximum-Value/day30/src/main.rs
Normal file
@ -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),
|
||||||
|
}
|
||||||
|
}
|
24
Week-05/Day-30_The-Maximum-Value/day30/tests/examples.rs
Normal file
24
Week-05/Day-30_The-Maximum-Value/day30/tests/examples.rs
Normal file
@ -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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user