-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 2241: Design an ATM Machine
- Loading branch information
Showing
3 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
use std::convert::TryInto; | ||
|
||
#[allow(clippy::struct_field_names, clippy::upper_case_acronyms)] | ||
pub struct ATM { | ||
count_20: u32, | ||
count_50: u32, | ||
count_100: u32, | ||
count_200: u32, | ||
count_500: u32, | ||
} | ||
|
||
impl ATM { | ||
fn new() -> Self { | ||
Self { | ||
count_20: 0, | ||
count_50: 0, | ||
count_100: 0, | ||
count_200: 0, | ||
count_500: 0, | ||
} | ||
} | ||
|
||
fn deposit(&mut self, banknotes_count: Vec<i32>) { | ||
let [count_20, count_50, count_100, count_200, count_500]: [_; 5] = banknotes_count.try_into().ok().unwrap(); | ||
|
||
self.count_20 += count_20 as u32; | ||
self.count_50 += count_50 as u32; | ||
self.count_100 += count_100 as u32; | ||
self.count_200 += count_200 as u32; | ||
self.count_500 += count_500 as u32; | ||
} | ||
|
||
fn withdraw(&mut self, amount: i32) -> Vec<i32> { | ||
let mut amount = amount as u32; | ||
|
||
let count_500 = (amount / 500).min(self.count_500); | ||
|
||
amount -= 500 * count_500; | ||
|
||
let count_200 = (amount / 200).min(self.count_200); | ||
|
||
amount -= 200 * count_200; | ||
|
||
let count_100 = (amount / 100).min(self.count_100); | ||
|
||
amount -= 100 * count_100; | ||
|
||
let count_50 = (amount / 50).min(self.count_50); | ||
|
||
amount -= 50 * count_50; | ||
|
||
let count_20 = (amount / 20).min(self.count_20); | ||
|
||
amount -= 20 * count_20; | ||
|
||
if amount == 0 { | ||
self.count_20 -= count_20; | ||
self.count_50 -= count_50; | ||
self.count_100 -= count_100; | ||
self.count_200 -= count_200; | ||
self.count_500 -= count_500; | ||
|
||
vec![ | ||
count_20 as _, | ||
count_50 as _, | ||
count_100 as _, | ||
count_200 as _, | ||
count_500 as _, | ||
] | ||
} else { | ||
vec![-1] | ||
} | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::ATM for ATM { | ||
fn new() -> Self { | ||
Self::new() | ||
} | ||
|
||
fn deposit(&mut self, banknotes_count: Vec<i32>) { | ||
self.deposit(banknotes_count); | ||
} | ||
|
||
fn withdraw(&mut self, amount: i32) -> Vec<i32> { | ||
self.withdraw(amount) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::ATM>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
pub mod greedy; | ||
|
||
#[allow(clippy::upper_case_acronyms)] // Expected. | ||
pub trait ATM { | ||
fn new() -> Self; | ||
fn deposit(&mut self, banknotes_count: Vec<i32>); | ||
fn withdraw(&mut self, amount: i32) -> Vec<i32>; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::ATM; | ||
|
||
enum Operation { | ||
Deposit([i32; 5]), | ||
Withdraw(i32, Option<[i32; 5]>), | ||
} | ||
|
||
pub fn run<A: ATM>() { | ||
let test_cases = [&[ | ||
Operation::Deposit([0, 0, 1, 2, 1]), | ||
Operation::Withdraw(600, Some([0, 0, 1, 0, 1])), | ||
Operation::Deposit([0, 1, 0, 1, 1]), | ||
Operation::Withdraw(600, None), | ||
Operation::Withdraw(550, Some([0, 1, 0, 0, 1])), | ||
] as &[_]]; | ||
|
||
for operations in test_cases { | ||
let mut atm = A::new(); | ||
|
||
for operation in operations { | ||
match *operation { | ||
Operation::Deposit(banknotes_count) => atm.deposit(banknotes_count.to_vec()), | ||
Operation::Withdraw(amount, expected) => assert_eq!( | ||
atm.withdraw(amount), | ||
expected.as_ref().map_or(&[-1] as &[_], |expected| expected), | ||
), | ||
} | ||
} | ||
} | ||
} | ||
} |