Wrote program for Day 39
This commit is contained in:
parent
9c64d77532
commit
311260e8a1
@ -85,7 +85,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| Day #36 | [LCD Display](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-36_LCD-Display) | :white_check_mark: |
|
| Day #36 | [LCD Display](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-36_LCD-Display) | :white_check_mark: |
|
||||||
| Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_check_mark: |
|
| Day #37 | [Breaking The Records](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-37_Breaking-The-Records) | :white_check_mark: |
|
||||||
| Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_check_mark: |
|
| Day #38 | [Electronics Shop](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-38_Electronics-Shop) | :white_check_mark: |
|
||||||
| Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_large_square: |
|
| Day #39 | [Halloween Sale](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-39_Halloween-Sale) | :white_check_mark: |
|
||||||
| Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_large_square: |
|
| Day #40 | [Larrys Array](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-40_Larrys-Array) | :white_large_square: |
|
||||||
| Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_large_square: |
|
| Day #41 | [Sales By Match](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-41_Sales-By-Match) | :white_large_square: |
|
||||||
| Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_large_square: |
|
| Day #42 | [Drawing Book](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-06/Day-42_Drawing-Book) | :white_large_square: |
|
||||||
|
6
Week-06/Day-39_Halloween-Sale/day39/Cargo.toml
Normal file
6
Week-06/Day-39_Halloween-Sale/day39/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day39"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
15
Week-06/Day-39_Halloween-Sale/day39/src/lib.rs
Normal file
15
Week-06/Day-39_Halloween-Sale/day39/src/lib.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
pub fn how_many_games(price: usize, discount: usize, min_price: usize, budget: usize) -> usize {
|
||||||
|
let mut games = 0;
|
||||||
|
let mut spent = 0;
|
||||||
|
|
||||||
|
while min_price < price - std::cmp::min(price, discount * games) {
|
||||||
|
spent += price - std::cmp::min(price, discount * games);
|
||||||
|
games += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if min_price >= price - std::cmp::min(price, discount * games) {
|
||||||
|
games += (budget - spent) / min_price;
|
||||||
|
}
|
||||||
|
|
||||||
|
games
|
||||||
|
}
|
55
Week-06/Day-39_Halloween-Sale/day39/src/main.rs
Normal file
55
Week-06/Day-39_Halloween-Sale/day39/src/main.rs
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
use std::process::exit;
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
io::{self, Write},
|
||||||
|
num::ParseIntError,
|
||||||
|
};
|
||||||
|
|
||||||
|
use day39::how_many_games;
|
||||||
|
|
||||||
|
fn read_usize(request: &str) -> Result<usize, ParseIntError> {
|
||||||
|
let mut buffer = String::new();
|
||||||
|
|
||||||
|
print!("{request}");
|
||||||
|
io::stdout().flush().expect("Failed to flush stdout");
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut buffer)
|
||||||
|
.expect("Failed to read line");
|
||||||
|
|
||||||
|
buffer.trim().parse()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let price = match read_usize("Insert the normal price: ") {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let discount = match read_usize("Insert the discount: ") {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let min_price = match read_usize("Insert the minimum price: ") {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let budget = match read_usize("Insert the budget: ") {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let result = how_many_games(price, discount, min_price, budget);
|
||||||
|
println!("You can buy {result} games!");
|
||||||
|
}
|
9
Week-06/Day-39_Halloween-Sale/day39/tests/example.rs
Normal file
9
Week-06/Day-39_Halloween-Sale/day39/tests/example.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod example {
|
||||||
|
use day39::how_many_games;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn example() {
|
||||||
|
assert_eq!(how_many_games(20, 3, 6, 80), 6);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user