Wrote program for Day 38
This commit is contained in:
parent
08c85f8d72
commit
9c64d77532
@ -84,7 +84,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
|||||||
| Day #35 | [Dog And Gopher](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-05/Day-35_Dog-And-Gopher) | :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_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 #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_large_square: |
|
| 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_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 #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: |
|
||||||
|
6
Week-06/Day-38_Electronics-Shop/day38/Cargo.toml
Normal file
6
Week-06/Day-38_Electronics-Shop/day38/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "day38"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
44
Week-06/Day-38_Electronics-Shop/day38/src/lib.rs
Normal file
44
Week-06/Day-38_Electronics-Shop/day38/src/lib.rs
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
pub fn get_money_spent(
|
||||||
|
keyboards: &[usize],
|
||||||
|
mut drives: Vec<usize>,
|
||||||
|
budget: usize,
|
||||||
|
) -> Option<usize> {
|
||||||
|
let mut result = None;
|
||||||
|
drives.sort();
|
||||||
|
|
||||||
|
for k in keyboards {
|
||||||
|
match floor(&drives, budget - k) {
|
||||||
|
Some(d) => {
|
||||||
|
if result.is_none() || Some(k + d) > result {
|
||||||
|
result = Some(k + d)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => continue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
fn floor(array: &[usize], element: usize) -> Option<usize> {
|
||||||
|
let mut start = 0;
|
||||||
|
let mut end = array.len() - 1;
|
||||||
|
|
||||||
|
while start <= end {
|
||||||
|
println!("start: {start}, end: {end}");
|
||||||
|
let mid = (start + end) / 2;
|
||||||
|
match array[mid].cmp(&element) {
|
||||||
|
std::cmp::Ordering::Equal => return Some(element),
|
||||||
|
std::cmp::Ordering::Less => start = mid + 1,
|
||||||
|
std::cmp::Ordering::Greater => {
|
||||||
|
if mid > 0 {
|
||||||
|
end = mid - 1
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(array[start - 1])
|
||||||
|
}
|
67
Week-06/Day-38_Electronics-Shop/day38/src/main.rs
Normal file
67
Week-06/Day-38_Electronics-Shop/day38/src/main.rs
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
use std::{
|
||||||
|
io::{self, Write},
|
||||||
|
num::ParseIntError,
|
||||||
|
process::exit,
|
||||||
|
};
|
||||||
|
|
||||||
|
use day38::get_money_spent;
|
||||||
|
|
||||||
|
fn get_string(request: &str) -> String {
|
||||||
|
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");
|
||||||
|
buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_usize_vec(request: &str) -> Result<Vec<usize>, String> {
|
||||||
|
let buffer = get_string(request);
|
||||||
|
Ok(buffer
|
||||||
|
.split_whitespace()
|
||||||
|
.try_fold(Vec::new(), |mut acc, x| match x.parse::<usize>() {
|
||||||
|
Ok(n) => {
|
||||||
|
acc.push(n);
|
||||||
|
Ok(acc)
|
||||||
|
}
|
||||||
|
Err(_) => Err("Prices must be valid integers".to_string()),
|
||||||
|
})
|
||||||
|
.unwrap_or(Vec::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_usize(request: &str) -> Result<usize, ParseIntError> {
|
||||||
|
let buffer = get_string(request);
|
||||||
|
buffer.parse()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let keyboards = match get_usize_vec("Insert the price of the keyboards space separated: ") {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let drives = match get_usize_vec("Insert the price of the drivers space separated: ") {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let budget = match get_usize("Insert the budget: ") {
|
||||||
|
Ok(x) => x,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("{}", e);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let result = get_money_spent(&keyboards, drives, budget);
|
||||||
|
match result {
|
||||||
|
Some(x) => println!("The maximum that can be spent is {x}."),
|
||||||
|
None => println!("It is not possible to buy both items."),
|
||||||
|
}
|
||||||
|
}
|
9
Week-06/Day-38_Electronics-Shop/day38/tests/example.rs
Normal file
9
Week-06/Day-38_Electronics-Shop/day38/tests/example.rs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod example {
|
||||||
|
use day38::get_money_spent;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test() {
|
||||||
|
assert_eq!(get_money_spent(&[40, 50, 60], vec![5, 8, 12], 60), Some(58));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user