Skip to content

Commit

Permalink
Add problem 2241: Design an ATM Machine
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Jun 5, 2024
1 parent 7b40766 commit 0b864f0
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,7 @@ pub mod problem_2231_largest_number_after_digit_swaps_by_parity;
pub mod problem_2235_add_two_integers;
pub mod problem_2236_root_equals_sum_of_children;
pub mod problem_2239_find_closest_number_to_zero;
pub mod problem_2241_design_an_atm_machine;
pub mod problem_2243_calculate_digit_sum_of_a_string;
pub mod problem_2244_minimum_rounds_to_complete_all_tasks;
pub mod problem_2248_intersection_of_multiple_arrays;
Expand Down
100 changes: 100 additions & 0 deletions src/problem_2241_design_an_atm_machine/greedy.rs
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>();
}
}
42 changes: 42 additions & 0 deletions src/problem_2241_design_an_atm_machine/mod.rs
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),
),
}
}
}
}
}

0 comments on commit 0b864f0

Please sign in to comment.