Wrote program for Day 34
This commit is contained in:
parent
5eb41602b9
commit
c3ccae96d6
@ -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 #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 #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 #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 #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 #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: |
|
| 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: |
|
||||||
|
6
Week-05/Day-34_Primary-Arithmetic/day34/Cargo.toml
Normal file
6
Week-05/Day-34_Primary-Arithmetic/day34/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day34"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
22
Week-05/Day-34_Primary-Arithmetic/day34/src/lib.rs
Normal file
22
Week-05/Day-34_Primary-Arithmetic/day34/src/lib.rs
Normal file
@ -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
|
||||||
|
}
|
42
Week-05/Day-34_Primary-Arithmetic/day34/src/main.rs
Normal file
42
Week-05/Day-34_Primary-Arithmetic/day34/src/main.rs
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
use std::{
|
||||||
|
io::{self, Write},
|
||||||
|
process::exit,
|
||||||
|
};
|
||||||
|
|
||||||
|
use day34::carry_operations;
|
||||||
|
|
||||||
|
fn read_usize(request: &str) -> Result<usize, &str> {
|
||||||
|
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);
|
||||||
|
}
|
34
Week-05/Day-34_Primary-Arithmetic/day34/tests/examples.rs
Normal file
34
Week-05/Day-34_Primary-Arithmetic/day34/tests/examples.rs
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user