Wrote program for Day 25
This commit is contained in:
parent
27747118b1
commit
9595929b38
@ -71,7 +71,7 @@ We encourage you to share your progress and ask questions in the Discussions sec
|
||||
| Day #22 | [Marcio Mellos Challenge](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-22_Marcio-Mellos-Challenge) | :white_check_mark: |
|
||||
| Day #23 | [The Dining Philosophers](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-23_The-Dining_Philosophers) | :white_check_mark: |
|
||||
| Day #24 | [The Josephus Problem](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-24_The-Josephus-Problem) | :white_check_mark: |
|
||||
| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_large_square: |
|
||||
| Day #25 | [Coin Trouble](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-25_Coin-Trouble) | :white_check_mark: |
|
||||
| Day #26 | [Briefcase Lock](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-26_Briefcase-Lock) | :white_large_square: |
|
||||
| Day #27 | [Task Scheduler](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-27_Task-Scheduler) | :white_large_square: |
|
||||
| Day #28 | [Word Search](https://github.com/LiveGray/100-Days-Of-Rust/tree/main/Week-04/Day-28_Word-Search) | :white_large_square: |
|
||||
|
6
Week-04/Day-25_Coin-Trouble/day25/Cargo.toml
Normal file
6
Week-04/Day-25_Coin-Trouble/day25/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "day25"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
35
Week-04/Day-25_Coin-Trouble/day25/src/lib.rs
Normal file
35
Week-04/Day-25_Coin-Trouble/day25/src/lib.rs
Normal file
@ -0,0 +1,35 @@
|
||||
pub fn coins_div(coins: &[usize]) -> bool {
|
||||
let sum: usize = coins.iter().copied().sum();
|
||||
let each = sum / 3;
|
||||
|
||||
if sum % 3 != 0 {
|
||||
false
|
||||
} else {
|
||||
try_coins_div(coins, 0, each)
|
||||
}
|
||||
}
|
||||
|
||||
fn try_coins_div(coins: &[usize], current_coins: usize, should_have: usize) -> bool {
|
||||
if coins.is_empty() {
|
||||
return current_coins == 0;
|
||||
}
|
||||
|
||||
for (i, c) in coins.iter().enumerate() {
|
||||
let coins_without_current = [&coins[..i], &coins[i + 1..]].concat();
|
||||
if current_coins + c <= should_have
|
||||
&& try_coins_div(
|
||||
&coins_without_current,
|
||||
if current_coins + c == should_have {
|
||||
0
|
||||
} else {
|
||||
current_coins + c
|
||||
},
|
||||
should_have,
|
||||
)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
25
Week-04/Day-25_Coin-Trouble/day25/src/main.rs
Normal file
25
Week-04/Day-25_Coin-Trouble/day25/src/main.rs
Normal file
@ -0,0 +1,25 @@
|
||||
use std::io::{self, Write};
|
||||
|
||||
use day25::coins_div;
|
||||
|
||||
fn main() {
|
||||
let mut buffer = String::new();
|
||||
|
||||
print!("Insert the value of the coins space separated: ");
|
||||
io::stdout().flush().expect("Failed to flush stdout");
|
||||
|
||||
io::stdin()
|
||||
.read_line(&mut buffer)
|
||||
.expect("Failed to read line");
|
||||
|
||||
let coins: Vec<usize> = buffer
|
||||
.split_whitespace()
|
||||
.map(|x| x.parse().expect("Input must be made of numbers"))
|
||||
.collect();
|
||||
|
||||
if coins_div(&coins) {
|
||||
println!("The coins can be divided equally")
|
||||
} else {
|
||||
println!("The coins can't be divided equally");
|
||||
}
|
||||
}
|
18
Week-04/Day-25_Coin-Trouble/day25/tests/examples.rs
Normal file
18
Week-04/Day-25_Coin-Trouble/day25/tests/examples.rs
Normal file
@ -0,0 +1,18 @@
|
||||
#[cfg(test)]
|
||||
mod examples {
|
||||
use day25::coins_div;
|
||||
|
||||
#[test]
|
||||
fn example1() {
|
||||
assert!(coins_div(&[1, 2, 3, 2, 2, 2, 3]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example2() {
|
||||
assert!(!coins_div(&[5, 3, 10, 1, 2]));
|
||||
}
|
||||
#[test]
|
||||
fn example3() {
|
||||
assert!(coins_div(&[2, 4, 3, 2, 4, 9, 7, 8, 6, 9]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user