diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-09-26 11:27:12 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-09-26 11:27:12 +0100 |
| commit | debce30c9732613662c4b075bc70edee05255876 (patch) | |
| tree | e4438ff0a5519f9184cd93a3054bfc1d34424afc | |
| parent | 3860fbeb3d1dab16dc8fed677524f278b0f47b39 (diff) | |
| download | perlweeklychallenge-club-debce30c9732613662c4b075bc70edee05255876.tar.gz perlweeklychallenge-club-debce30c9732613662c4b075bc70edee05255876.tar.bz2 perlweeklychallenge-club-debce30c9732613662c4b075bc70edee05255876.zip | |
- Added solutions by Ulrich Rieke.
- Added solutions by Robert DiCicco.
- Added solutions by Eric Cheung.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Wanderdoc.
- Added solutions by W. Luis Mochan.
- Added solutions by E. Choroba.
- Added solutions by David Ferrone.
- Added solutions by Mark Anderson.
- Added solutions by rcmlz.
- Added solutions by Peter Campbell Smith.
- Added solutions by PokGoPun.
- Added solutions by Thomas Kohler.
- Added solutions by Lubos Kolouch.
- Added solutions by Luca Ferrari.
- Added solutions by Jaldhar H. Vyas.
34 files changed, 3640 insertions, 2507 deletions
diff --git a/challenge-236/eric-cheung/python/ch-1.py b/challenge-236/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..87c8692ed0 --- /dev/null +++ b/challenge-236/eric-cheung/python/ch-1.py @@ -0,0 +1,29 @@ +
+## arrInputBill = [5, 5, 5, 10, 20] ## Example 1
+## arrInputBill = [5, 5, 10, 10, 20] ## Example 2
+arrInputBill = [5, 5, 5, 20] ## Example 3
+
+arrChange = [0, 0, 0]
+bSucceed = True
+
+for nIndx in range(len(arrInputBill)):
+ if arrInputBill[nIndx] == 5:
+ arrChange[0] = arrChange[0] + 1
+ elif arrInputBill[nIndx] == 10:
+ if arrChange[0] < 1:
+ bSucceed = False
+ break
+ arrChange[0] = arrChange[0] - 1
+ arrChange[1] = arrChange[1] + 1
+ elif arrInputBill[nIndx] == 20:
+ if arrChange[0] < 3 and (arrChange[0] < 1 or arrChange[1] < 1):
+ bSucceed = False
+ break
+ arrChange[2] = arrChange[2] + 1
+ if arrChange[1] > 0:
+ arrChange[1] = arrChange[1] - 1
+ arrChange[0] = arrChange[0] - 1
+ else:
+ arrChange[0] = arrChange[0] - 3
+
+print (bSucceed)
diff --git a/challenge-236/eric-cheung/python/ch-2.py b/challenge-236/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..c503507ba1 --- /dev/null +++ b/challenge-236/eric-cheung/python/ch-2.py @@ -0,0 +1,24 @@ +
+arrInput = [4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10] ## Example 1
+## arrInput = [0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19] ## Example 2
+## arrInput = [9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17] ## Example 3
+
+arrIndx = [0] * len(arrInput)
+arrOutput = []
+
+for nIndxLoop in range(len(arrIndx)):
+ if arrIndx[nIndxLoop] == 1:
+ continue
+
+ arrTemp = []
+ nTempIndxLoop = nIndxLoop
+
+ while arrInput[nTempIndxLoop] not in arrTemp:
+ arrTemp.append(arrInput[nTempIndxLoop])
+ arrIndx[nTempIndxLoop] = 1
+ nTempIndxLoop = arrInput[nTempIndxLoop]
+
+ arrOutput.append(arrTemp)
+
+## print (arrOutput)
+print (len(arrOutput))
diff --git a/challenge-236/laurent-rosenfeld/blog.txt b/challenge-236/laurent-rosenfeld/blog.txt new file mode 100644 index 0000000000..8fcccd56d0 --- /dev/null +++ b/challenge-236/laurent-rosenfeld/blog.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/09/perl-weekly-challenge-236-array-loops.html diff --git a/challenge-236/laurent-rosenfeld/perl/ch-2.pl b/challenge-236/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..d0937e17a6 --- /dev/null +++ b/challenge-236/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use feature 'say'; + +sub find_loops { + my @in = @_; + my $count = 0; + for my $i (0..$#in) { + my $j = $i; + # say $i; + while (1) { + last unless defined $in[$j]; + # say "\t", $j, "\t", @in[$j]; + ++$count and last if $in[$j] == $i; + $j = $in[$j]; + } + } + return $count; +} + diff --git a/challenge-236/laurent-rosenfeld/raku/ch-2.raku b/challenge-236/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..0241c93c49 --- /dev/null +++ b/challenge-236/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,24 @@ +sub find-loops (@in) { + my $count = 0; + for 0..@in.end -> $i { + my $j = $i; + loop { + last unless @in[$j].defined; + # say "\t", $j, "\t", @in[$j]; + ++$count and last if @in[$j] == $i; + $j = @in[$j]; + } + } + return $count; +} + +my @tests = + (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10), + (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19), + (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17), + (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3); + +for @tests -> @test { + say @test; + say "\tNumber of loops: ", find-loops @test; +} diff --git a/challenge-236/rcmlz/raku/ch-1.raku b/challenge-236/rcmlz/raku/ch-1.raku new file mode 100644 index 0000000000..ca8a45ecf0 --- /dev/null +++ b/challenge-236/rcmlz/raku/ch-1.raku @@ -0,0 +1,48 @@ +unit module rcmlz::raku::task-one:ver<0.0.1>:auth<github:rcmlz>:api<1>; + +# run in terminal: raku --optimize=3 -I challenge-nr236/rcmlz/raku/ -- test/challenge-nr236/raku/task-one.rakutest +# or raku --optimize=3 -I challenge-nr236 -- test/benchmark-scalabiity.raku --task=task-one --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr236; cat /tmp/nr236_task-one.csv + +#|[ +You are asked to sell juice each costs $5. +You are given an array of bills. +You can only sell ONE juice to each customer but make sure you return exact change back. +You only have $5, $10 and $20 notes. +You do not have any change in hand at first. + +- Write a script to find out if it is possible to sell to each customers with correct change. +] +our sub solution(@input --> Bool) is export { + my $n = @input; + + my %change = + 5 => 0, + 10 => 0, + 20 => 0 + ; + + my $i = 0; + while (%change.values).all >= 0 and $i < $n { + given @input[$i++] { + when 5 { + %change<5>++ + } + when 10 { + %change<5>--; + %change<10>++ + } + when 20 { + if %change<10> { + %change<10>-- + }else { + %change<5>--; + %change<5>-- + } + %change<5>-- + } + default { die "$_ : only 5, 10 and 20 as input is accepted!"} + } + } + + (%change.values).all >= 0 ?? True !! False +}
\ No newline at end of file diff --git a/challenge-236/rcmlz/raku/ch-2.raku b/challenge-236/rcmlz/raku/ch-2.raku new file mode 100644 index 0000000000..13571552ec --- /dev/null +++ b/challenge-236/rcmlz/raku/ch-2.raku @@ -0,0 +1,36 @@ +unit module rcmlz::raku::task-two:ver<0.0.1>:auth<github:rcmlz>:api<1>; + +# run in terminal: raku --optimize=3 -I challenge-nr236/rcmlz/raku/ -- test/challenge-nr236/raku/task-two.rakutest +# or raku --optimize=3 -I challenge-nr236 -- test/benchmark-scalabiity.raku --task=task-two --user=rcmlz --max-run-times=1,3,7 --max-problem=10 --v=True --test-before-benchmark=True --out-folder=/tmp nr236; cat /tmp/nr236_task-two.csv + +#|[ +You are given an array of unique integers. + +- Write a script to determine how many loops are in the given array. +- To determine a loop: Start at an index and take the number at array[index] + and then proceed to that index and continue this until you end up at the starting index. +] +our sub solution(@input) is export { + my %graph = @input.pairs; + my $cycles = 0; + + while %graph.keys.elems { + + $cycles = $cycles + %graph.grep( -> $entry {$entry.key == $entry.value} ); + + %graph = %graph.grep( -> $entry {$entry.key != $entry.value} ); + + my $untouched = %graph.keys.Set; + + for %graph.kv -> $k, $v { + if %graph{$v}:exists { + %graph{$k} = %graph{$v}; + $untouched = $untouched ∖ $k; + } + } + + %graph{$_}:delete for $untouched.keys; + } + + return $cycles; +}
\ No newline at end of file diff --git a/challenge-236/robert-dicicco/julia/ch-1.jl b/challenge-236/robert-dicicco/julia/ch-1.jl new file mode 100644 index 0000000000..9650b34e02 --- /dev/null +++ b/challenge-236/robert-dicicco/julia/ch-1.jl @@ -0,0 +1,55 @@ +#!/usr/bin/env julia +#= +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-25 +Challenge 236 Task 01 Exact Change ( Julia ) +------------------------------------- +=# + +using Printf + +bills = [[5, 5, 5, 10, 20], [5, 5, 10, 10, 20],[5, 5, 5, 20]] + +for paid in bills + @printf("Input: @bills = %s\n", paid) + cnt = 1 + change = 0 + while cnt <=length(paid) + ticket = paid[cnt] + change_required = ticket - 5 + if ticket == 5 + change += 5 + elseif ticket == 10 + change += 0 + elseif ticket == 20 + if change_required > change + @printf("Output: false\n\n") + break + else + @printf("Output: true\n\n") + break + end + change -= 10 + end + cnt += 1 + end +end + +#= +-------------------------------------- +SAMPLE OUTPUT +julia .\ExactChange.jl + +Input: @bills = [5, 5, 5, 10, 20] +Output: true + +Input: @bills = [5, 5, 10, 10, 20] +Output: false + +Input: @bills = [5, 5, 5, 20] +Output: true +------------------------------------- +=# + + diff --git a/challenge-236/robert-dicicco/perl/ch-1.pl b/challenge-236/robert-dicicco/perl/ch-1.pl new file mode 100644 index 0000000000..be79d7d4de --- /dev/null +++ b/challenge-236/robert-dicicco/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/usr/bin/env perl +use v5.38; +=begin comment +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-25 +Challenge 236 Task 01 Exact Change ( Perl ) +------------------------------------- +=cut + +my @bills = ([5, 5, 5, 10, 20], [5, 5, 10, 10, 20],[5, 5, 5, 20]); + +for my $chg (@bills) { + say "Input: \@bills = [@$chg]"; + my $ln = scalar @$chg; + my $cnt = 0; + my $change = 0; + while ($cnt < @$chg) { + my $ticket = $chg->[$cnt]; + my $change_required = $ticket - 5; + if ($ticket == 5){ + $change += 5; + } elsif ($ticket == 10) { + $change -= 5; + $change += 5; + } elsif ($ticket == 20) { + if ($change_required > $change){ + say "Output: false\n"; + last; + } else { + say "Output: true\n"; + last; + } + $change -= 15; + $change += 5; + } + $cnt++; + } +} +=begin comment +-------------------------------------- +SAMPLE OUTPUT +perl .\ExactChange.pl + +Input: @bills = [5 5 5 10 20] +Output: true + +Input: @bills = [5 5 10 10 20] +Output: false + +Input: @bills = [5 5 5 20] +Output: true +------------------------------------- +=cut + + diff --git a/challenge-236/robert-dicicco/powershell/ch-1.psl b/challenge-236/robert-dicicco/powershell/ch-1.psl new file mode 100644 index 0000000000..ba05daec08 --- /dev/null +++ b/challenge-236/robert-dicicco/powershell/ch-1.psl @@ -0,0 +1,53 @@ +<# +-------------------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-25 +Challenge 236 Task 01 Exact Change ( Powershell ) +-------------------------------------------------- +#> + +$bills = @( (5, 5, 5, 10, 20), (5, 5, 10, 10, 20),(5, 5, 5, 20)) + +foreach ($paid in $bills ) { + write-host "Input: @bills = [$paid]" + $cnt = 0 + $change = 0 + $ln = $paid.Length + while ( $cnt -lt $ln ) { + $ticket = $paid[$cnt] + $change_required = $ticket - 5 + switch ($ticket) { + 5 {$change += 5} + 10 {} + 20 { + if ($change_required -gt $change) { + write-host "Output: false`n" + break + } else { + write-host "Output: true`n" + break + } + } + } + $cnt += 1 + } +} + +<# +-------------------------------------------------- +SAMPLE OUTPUT +.\ExactChange.ps1 + +Input: @bills = [5 5 5 10 20] +Output: true + +Input: @bills = [5 5 10 10 20] +Output: false + +Input: @bills = [5 5 5 20] +Output: true +-------------------------------------------------- +#> + + + diff --git a/challenge-236/robert-dicicco/python/ch-1.py b/challenge-236/robert-dicicco/python/ch-1.py new file mode 100644 index 0000000000..7f1521f673 --- /dev/null +++ b/challenge-236/robert-dicicco/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +''' +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-25 +Challenge 236 Task 01 Exact Change ( Python ) +------------------------------------- +''' +bills = [[5, 5, 5, 10, 20], [5, 5, 10, 10, 20],[5, 5, 5, 20]] + +for paid in bills: + print(f"Input: @bills = {paid}") + cnt = 0 + change = 0 + while cnt < len(paid): + ticket = paid[cnt] + change_required = ticket - 5 + if ticket == 5: + change += 5 + elif ticket == 10: + change += 0 + elif ticket == 20: + if change_required > change: + print("Output: false\n") + break + else: + print("Output: true\n") + break + change -= 10 + cnt += 1 + +''' +-------------------------------------- +SAMPLE OUTPUT +python .\ExactChange.py + +Input: @bills = [5, 5, 5, 10, 20] +Output: true + +Input: @bills = [5, 5, 10, 10, 20] +Output: false + +Input: @bills = [5, 5, 5, 20] +Output: true +------------------------------------- +''' + + + + diff --git a/challenge-236/robert-dicicco/raku/ch-1.raku b/challenge-236/robert-dicicco/raku/ch-1.raku new file mode 100644 index 0000000000..77664be74b --- /dev/null +++ b/challenge-236/robert-dicicco/raku/ch-1.raku @@ -0,0 +1,56 @@ +#!/usr/bin/env raku +use v6; + +=begin comment +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-25 +Challenge 236 Task 01 Exact Change ( Raku ) +------------------------------------- +=end comment + +my @bills = ([5, 5, 5, 10, 20], [5, 5, 10, 10, 20],[5, 5, 5, 20]); + +for (@bills) -> @chg { + say "Input: \@bills = ",@chg; + my $cnt = 0; + my $change = 0; + while ($cnt < @chg.elems) { + my $ticket = @chg[$cnt]; + my $change_required = $ticket - 5; + if $ticket == 5 { + $change += 5; + } elsif $ticket == 10 { + $change += 0; + } elsif $ticket == 20 { + if $change_required > $change { + say "Output: false\n"; + last; + } else { + say "Output: true\n"; + last; + } + $change -= 10; + } + $cnt++; + } +} + +=begin comment +-------------------------------------- +SAMPLE OUTPUT +raku .\ExactChange.rk + +Input: @bills = [5 5 5 10 20] +Output: true + +Input: @bills = [5 5 10 10 20] +Output: false + +Input: @bills = [5 5 5 20] +Output: true +------------------------------------- +=end comment + + + diff --git a/challenge-236/robert-dicicco/ruby/ch-1.rb b/challenge-236/robert-dicicco/ruby/ch-1.rb new file mode 100644 index 0000000000..160a48363f --- /dev/null +++ b/challenge-236/robert-dicicco/ruby/ch-1.rb @@ -0,0 +1,57 @@ +#!/usr/bin/env ruby +=begin +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-25 +Challenge 236 Task 01 Exact Change ( Ruby ) +------------------------------------- +=end + +bills = [[5, 5, 5, 10, 20], [5, 5, 10, 10, 20],[5, 5, 5, 20]] + +bills.each do |paid| + puts("Input: @bills = #{paid}") + cnt = 0 + change = 0 + while cnt < paid.length() + ticket = paid[cnt] + change_required = ticket - 5 + cnt += 1 + case ticket + when 5 + change += 5 + when 10 + # pass + when 20 + if change_required > change + puts("Output: false\n\n") + break + else + puts("Output: true\n\n") + break + end + change -= 10 + end + end + cnt += 1 +end + +=begin +-------------------------------------- +SAMPLE OUTPUT +ruby .\ExactChange.rb + +Input: @bills = [5, 5, 5, 10, 20] +Output: true + +Input: @bills = [5, 5, 10, 10, 20] +Output: false + +Input: @bills = [5, 5, 5, 20] +Output: true +------------------------------------- +=end + + + + diff --git a/challenge-236/robert-dicicco/tcl/ch-1.tcl b/challenge-236/robert-dicicco/tcl/ch-1.tcl new file mode 100644 index 0000000000..a6e97d6699 --- /dev/null +++ b/challenge-236/robert-dicicco/tcl/ch-1.tcl @@ -0,0 +1,58 @@ +#!/usr/bin/env tclsh +set comment { +-------------------------------------- +AUTHOR: Robert DiCicco +DATE : 2023-09-25 +Challenge 236 Task 01 Exact Change ( Tcl ) +------------------------------------- +} + +set bills { { 5 5 5 10 20 } { 5 5 10 10 20 } { 5 5 5 20 } } + +foreach paid $bills { + puts "Input: @bills = ($paid)" + set cnt 0 + set change 0 + set ln [expr [llength $paid]] + while { [ expr $cnt ] < $ln } { + set ticket [lindex $paid $cnt] + set change_required [expr $ticket - 5] + switch $ticket { + 5 { + set change [ expr $change + 5 ] + } + 10 { + } + 20 { + if { $change_required > $change } { + puts "Output: false\n" + break + } else { + puts "Output: true\n" + break + } + set change [ expr $change - 10] + } + } + set cnt [expr $cnt + 1] + } + set cnt 0 +} + +set comment { +-------------------------------------- +SAMPLE OUTPUT +tclsh .\ExactChange.tcl + +Input: @bills = ( 5 5 5 10 20 ) +Output: true + +Input: @bills = ( 5 5 10 10 20 ) +Output: false + +Input: @bills = ( 5 5 5 20 ) +Output: true +------------------------------------- +} + + diff --git a/challenge-236/ulrich-rieke/cpp/ch-2.cpp b/challenge-236/ulrich-rieke/cpp/ch-2.cpp new file mode 100755 index 0000000000..17a7d6bd94 --- /dev/null +++ b/challenge-236/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,38 @@ +#include <iostream>
+#include <vector>
+#include <numeric>
+#include <set>
+#include <algorithm>
+
+//my assumption is that you can always find a loop if the starting array
+//is a random shuffle of all the indices of a vector from 0 to len - 1 where
+//len is the length of the array.
+
+int main( ) {
+ std::vector<int> numbers( 20 ) ;
+ std::iota( numbers.begin( ) , numbers.end( ) , 1 ) ;
+ std::random_shuffle( numbers.begin( ) , numbers.end( ) ) ;
+ int loops = 0 ;
+ for ( int pos = 0 ; pos < 20 ; pos++ ) {
+ if ( numbers[ pos ] == pos ) {
+ loops++ ;
+ }
+ else {
+ std::set<int> already_seen ;
+ int num = numbers[ pos ] ;
+ auto p = already_seen.insert( num ) ;
+ while ( p.second ) {
+ if ( already_seen.find( num ) != already_seen.end( ) ) {
+ loops++ ;
+ break ;
+ }
+ else {
+ num = numbers[ num ] ;
+ p = already_seen.insert( num ) ;
+ }
+ }
+ }
+ }
+ std::cout << loops << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-236/ulrich-rieke/rust/ch-2.rs b/challenge-236/ulrich-rieke/rust/ch-2.rs new file mode 100755 index 0000000000..17724298c3 --- /dev/null +++ b/challenge-236/ulrich-rieke/rust/ch-2.rs @@ -0,0 +1,42 @@ +use std::io ; +use std::collections::HashSet ; + +fn main() { + println!("Enter some unique positive integers!"); + println!("They should be a random shuffle of integers from 0 to array length!") ; + let mut inline : String = String::new( ) ; + io::stdin( ).read_line( &mut inline ).unwrap( ) ; + let entered_line : &str = &*inline ; + let numbers : Vec<usize> = entered_line.split_whitespace( ).map( | s | + s.trim( ).parse::<usize>( ).unwrap( ) ).collect( ) ; + let mut loops : usize = 0 ; + //the strategy is as follows : if a number in the array represents its own + //index, it is a loop in itself , and the number of loops can be increased + //otherwise: for every index in the vector, we build a set of numbers we fill + //by adding numbers from numbers[current_index]. If we cannot add a number + //to the set because it is already there no loop can be formed at that index. + //Otherwise we continue until, eventually, numbers[current_index] == start_index + for pos in 0..numbers.len( ) { + if numbers[ pos ] == pos { + loops += 1 ; + } + else { + let mut already_seen : HashSet<usize> = HashSet::new( ) ; + let mut num : usize = numbers[ pos ] ; + let mut inserted : bool = already_seen.insert( num ) ; + 'inner: while inserted { + println!("{:?}" , already_seen ) ; + if already_seen.contains( &pos ) { + loops += 1 ; + break 'inner ; + } + else { + num = numbers[ num ] ; + inserted = already_seen.insert( num ) ; + } + } + } + } + println!("{}" , loops) ; +} + diff --git a/challenge-236/wanderdoc/perl/ch-1.pl b/challenge-236/wanderdoc/perl/ch-1.pl new file mode 100755 index 0000000000..1647898986 --- /dev/null +++ b/challenge-236/wanderdoc/perl/ch-1.pl @@ -0,0 +1,70 @@ +#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+You are asked to sell juice each costs $5. You are given an array of bills. You can only sell ONE juice to each customer but make sure you return exact change back. You only have $5, $10 and $20 notes. You do not have any change in hand at first.
+Write a script to find out if it is possible to sell to each customers with correct change.
+Example 1 Input: @bills = (5, 5, 5, 10, 20) Output: true
+From the first 3 customers, we collect three $5 bills in order.
+From the fourth customer, we collect a $10 bill and give back a $5.
+From the fifth customer, we give a $10 bill and a $5 bill.
+Since all customers got correct change, we output true.
+
+Example 2 Input: @bills = (5, 5, 10, 10, 20) Output: false
+From the first two customers in order, we collect two $5 bills.
+For the next two customers in order, we collect a $10 bill and give back a $5 bill.
+For the last customer, we can not give the change of $15 back because we only have two $10 bills.
+Since not every customer received the correct change, the answer is false.
+=cut
+
+use Test2::V0;
+
+sub exact_change
+{
+ my @bills = @_;
+ my %change;
+ for my $bill ( @bills )
+ {
+ if ( $bill == 5 )
+ {
+ $change{$bill}++;
+ }
+ elsif ( $bill == 10 )
+ {
+ if ( $change{5} > 0 )
+ {
+ $change{$bill}++;
+ $change{5}--;
+ }
+ else
+ {
+ return 'false';
+ }
+ }
+ elsif ( $bill == 20 )
+ {
+ if ( $change{5} > 2 )
+ {
+ $change{$bill}++;
+ $change{5} -= 3;
+ }
+ elsif ( $change{5} > 0 and $change{10} > 0 )
+ {
+ $change{$bill}++;
+ $change{5}--;
+ $change{10}--;
+ }
+ else
+ {
+ return 'false';
+ }
+ }
+ }
+ return 'true';
+}
+
+
+is(exact_change(5, 5, 5, 10, 20), 'true', 'Example 1');
+is(exact_change(5, 5, 10, 10, 20), 'false', 'Example 2');
+done_testing();
diff --git a/challenge-236/wanderdoc/perl/ch-2.pl b/challenge-236/wanderdoc/perl/ch-2.pl new file mode 100755 index 0000000000..df05db485f --- /dev/null +++ b/challenge-236/wanderdoc/perl/ch-2.pl @@ -0,0 +1,102 @@ +#!perl
+use strict;
|
