From fbcb245a97f8b4e5420324982a75b8ce6af2ae62 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 27 Sep 2023 18:34:14 +0100 Subject: Solution to task 1 --- challenge-236/jo-37/perl/ch-1.pl | 82 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100755 challenge-236/jo-37/perl/ch-1.pl diff --git a/challenge-236/jo-37/perl/ch-1.pl b/challenge-236/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..36ae37769e --- /dev/null +++ b/challenge-236/jo-37/perl/ch-1.pl @@ -0,0 +1,82 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util qw(min); + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <@{5, 10, 20} = (0, 0, 0); + state $price = 5; + + # Try to sell a juice for every given bill. + for my $bill (@_) { + # The amount to be returned. + my $return = $bill - $price; + # There is only one case, where the action is not uniquely + # defined: If we are to return $15, have three or more $5 bills + # and one or more $10 bills. In this situation we give one $10 + # bill and one $5 bill. + for my $tray (10, 5) { + my $cnt = min $register{$tray}, int $return / $tray; + # Take the bill(s) from the tray and adjust the to be + # returned amount. + $register{$tray} -= $cnt; + $return -= $cnt * $tray; + } + # Put the received bill into its tray. + $register{$bill}++; + # Fail if we cannot return the exact change. + return if $return; + } + # Here we succeeded. + 1; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok sell_juice(5, 5, 5, 10, 20), 'example 1'; + ok !sell_juice(5, 5, 10, 10, 20), 'example 2'; + ok sell_juice(5, 5, 5, 20), 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + ok sell_juice(5, 5, 5, 5, 10, 20, 10), 'prefer 1x10 + 1x5 over 3x5'; + } + + done_testing; + exit; +} -- cgit