From a04070a3510a655b591c20e180fd70815b66915a Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Wed, 20 Sep 2023 11:43:23 -0400 Subject: DAJ 235 blog --- challenge-235/dave-jacoby/blog.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 challenge-235/dave-jacoby/blog.txt diff --git a/challenge-235/dave-jacoby/blog.txt b/challenge-235/dave-jacoby/blog.txt new file mode 100644 index 0000000000..92c92df39c --- /dev/null +++ b/challenge-235/dave-jacoby/blog.txt @@ -0,0 +1,2 @@ +https://jacoby.github.io/2023/09/20/there-goes-my-zero-weekly-challenge-235.html + -- cgit From 524881738cfa647e37b0155bcebc948b198c8e7d Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 25 Sep 2023 10:37:02 +0200 Subject: Add solutions to 236: Exact Change & Array Loops by E. Choroba --- challenge-236/e-choroba/perl/ch-1.pl | 35 +++++++++++++++++++++++++++++++++++ challenge-236/e-choroba/perl/ch-2.pl | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100755 challenge-236/e-choroba/perl/ch-1.pl create mode 100755 challenge-236/e-choroba/perl/ch-2.pl diff --git a/challenge-236/e-choroba/perl/ch-1.pl b/challenge-236/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..4d2b2e34be --- /dev/null +++ b/challenge-236/e-choroba/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub exact_change(@bills) { + my %change = map +($_ => 0), 5, 10; + for my $bill (@bills) { + if ($bill == 10) { + return unless $change{5}--; + + } elsif ($bill == 20) { + # First try to return 10 + 5, we might need 5s later. + if ($change{10} && $change{5}) { + --$change{$_} for 5, 10; + } elsif ($change{5} > 2) { + $change{5} -= 3; + } else { + return + } + next # 20s are never returned. + } + + ++$change{$bill}; + } + return 1 +} + +use Test::More tests => 3 + 1; + +ok exact_change(5, 5, 5, 10, 20), 'Example 1'; +ok ! exact_change(5, 5, 10, 10, 20), 'Example 2'; +ok exact_change(5, 5, 5, 20), 'Example 3'; + +ok exact_change(5, 5, 5, 5, 10, 20, 10), 'Keep the fives'; diff --git a/challenge-236/e-choroba/perl/ch-2.pl b/challenge-236/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..9a06826a7b --- /dev/null +++ b/challenge-236/e-choroba/perl/ch-2.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub array_loops(@ints) { + my $count; + my @visited; + for my $i (0 .. $#ints) { + next if $visited[$i]; + + # Found a new loop. Visit all the members. + ++$count; + while (! $visited[$i]) { + $visited[$i] = 1; + $i = $ints[$i]; + } + } + return $count +} + +use Test::More tests => 3; + +is array_loops(4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, + 1, 12, 2, 9, 10), + 3, 'Example 1'; +is array_loops(0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, + 5, 3, 18, 15, 19), + 6, 'Example 2'; +is array_loops(9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, + 1, 0, 15, 6, 17), + 1, 'Example 3'; -- cgit From 90fb24771b5a2f072b9ca58efd6aa592df2d5c4d Mon Sep 17 00:00:00 2001 From: rcmlz Date: Mon, 25 Sep 2023 11:34:10 +0200 Subject: ch-236 --- challenge-236/rcmlz/raku/task-one.rakumod | 48 +++++++++++++++++++++++++++++++ challenge-236/rcmlz/raku/task-two.rakumod | 36 +++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 challenge-236/rcmlz/raku/task-one.rakumod create mode 100644 challenge-236/rcmlz/raku/task-two.rakumod diff --git a/challenge-236/rcmlz/raku/task-one.rakumod b/challenge-236/rcmlz/raku/task-one.rakumod new file mode 100644 index 0000000000..ca8a45ecf0 --- /dev/null +++ b/challenge-236/rcmlz/raku/task-one.rakumod @@ -0,0 +1,48 @@ +unit module rcmlz::raku::task-one:ver<0.0.1>:auth: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/task-two.rakumod b/challenge-236/rcmlz/raku/task-two.rakumod new file mode 100644 index 0000000000..13571552ec --- /dev/null +++ b/challenge-236/rcmlz/raku/task-two.rakumod @@ -0,0 +1,36 @@ +unit module rcmlz::raku::task-two:ver<0.0.1>:auth: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 -- cgit From 355cd3c42339e122d155186e76fb641744a7779e Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 25 Sep 2023 12:27:08 +0000 Subject: Challenge 236 Solutions (Raku) --- challenge-236/mark-anderson/raku/ch-1.raku | 44 +++++++++++++++++++++++ challenge-236/mark-anderson/raku/ch-2-slower.raku | 19 ++++++++++ challenge-236/mark-anderson/raku/ch-2.raku | 17 +++++++++ 3 files changed, 80 insertions(+) create mode 100644 challenge-236/mark-anderson/raku/ch-1.raku create mode 100644 challenge-236/mark-anderson/raku/ch-2-slower.raku create mode 100644 challenge-236/mark-anderson/raku/ch-2.raku diff --git a/challenge-236/mark-anderson/raku/ch-1.raku b/challenge-236/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..80d9278403 --- /dev/null +++ b/challenge-236/mark-anderson/raku/ch-1.raku @@ -0,0 +1,44 @@ +#!/usr/bin/env raku +use Test; + +ok exact-change(5,5,5,10,20); +nok exact-change(5,5,10,10,20); + +sub exact-change(*@a) +{ + my $bills = BagHash.new; + + while @a.shift -> $_ + { + when 5 + { + $bills<5>++ + } + when 10 + { + $bills<10>++; + $bills<5>-- || return False + } + when 20 + { + if $bills<10> and $bills<5> + { + $bills<10>--; + $bills<5>-- + } + else + { + if $bills<5> >= 3 + { + $bills<5> -= 3 + } + else + { + return False + } + } + } + } + + True +} diff --git a/challenge-236/mark-anderson/raku/ch-2-slower.raku b/challenge-236/mark-anderson/raku/ch-2-slower.raku new file mode 100644 index 0000000000..e4b1dde0db --- /dev/null +++ b/challenge-236/mark-anderson/raku/ch-2-slower.raku @@ -0,0 +1,19 @@ +#!/usr/bin/env raku +use Test; + +is array-loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10]), 3; +is array-loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19]), 6; +is array-loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]), 1; +say array-loops((^1e6).pick(*).Array); + +sub array-loops(@a) +{ + my %p = @a.pairs; + + sum do while %p + { + my $k = %p.keys.first; + $k = %p{$k}:delete while $k.defined; + 1 + } +} diff --git a/challenge-236/mark-anderson/raku/ch-2.raku b/challenge-236/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..84e2b08a26 --- /dev/null +++ b/challenge-236/mark-anderson/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/env raku +use Test; + +is array-loops([4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10]), 3; +is array-loops([0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19]), 6; +is array-loops([9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]), 1; +say array-loops((^1e6).pick(*).Array); + +sub array-loops(@a) +{ + sum do while @a + { + my $k = @a.first({ .defined }, :k); + $k = @a[$k]:delete while $k.defined; + 1 + } +} -- cgit From 0f4b53fe7ff6cb094dc566f297a77427d62e7807 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Sun, 24 Sep 2023 23:16:19 -0600 Subject: Solve PWC236 --- challenge-236/wlmb/blog.txt | 1 + challenge-236/wlmb/perl/ch-1.pl | 25 +++++++++++++++++++++++++ challenge-236/wlmb/perl/ch-2.pl | 15 +++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 challenge-236/wlmb/blog.txt create mode 100755 challenge-236/wlmb/perl/ch-1.pl create mode 100755 challenge-236/wlmb/perl/ch-2.pl diff --git a/challenge-236/wlmb/blog.txt b/challenge-236/wlmb/blog.txt new file mode 100644 index 0000000000..988194c2d9 --- /dev/null +++ b/challenge-236/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2023/09/24/PWC236/ diff --git a/challenge-236/wlmb/perl/ch-1.pl b/challenge-236/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..ea26c2556d --- /dev/null +++ b/challenge-236/wlmb/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +# Perl weekly challenge 236 +# Task 1: Exact Change +# +# See https://wlmb.github.io/2023/09/24/PWC236/#task-1-exact-change +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 B1 [B2...] + to see if you can sell $5 juice taking bills B1, B2... and giving + exact change. + FIN +my @sorted_input=sort {$a<=>$b} @ARGV; +my $success=1; +my %cash_box; +my $remaining_change; +for(@sorted_input){ + ++$cash_box{$_}; + $remaining_change=$_-5; + for(10,5){ + $remaining_change-=$_, --$cash_box{$_} + while $remaining_change>=$_ && $cash_box{$_}; + } + $success &&= !$remaining_change; +} +say "@ARGV -> ", $success?"true":"false"; diff --git a/challenge-236/wlmb/perl/ch-2.pl b/challenge-236/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..6e606b66b6 --- /dev/null +++ b/challenge-236/wlmb/perl/ch-2.pl @@ -0,0 +1,15 @@ +#!/usr/bin/env perl +# Perl weekly challenge 236 +# Task 2: Array Loops +# +# See https://wlmb.github.io/2023/09/24/PWC236/#task-2-array-loops +use v5.36; +my @in=@ARGV; +my @seen; +my $count=0; +for(0..@in-1){ #for each possible starting index + $seen[my $next=$_]++; + 1 while 0<=$next <@in && !$seen[$next=$in[$next]]++; + ++$count if $next==$_; +} +say "@in -> $count"; -- cgit From 068f401e62be754203ff85983a032348e87c6bac Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 25 Sep 2023 12:03:54 -0400 Subject: Week 236 --- challenge-236/zapwai/perl/ch-1.pl | 25 +++++++++++++++++++++ challenge-236/zapwai/perl/ch-2.pl | 46 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 challenge-236/zapwai/perl/ch-1.pl create mode 100644 challenge-236/zapwai/perl/ch-2.pl diff --git a/challenge-236/zapwai/perl/ch-1.pl b/challenge-236/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..53f44b7e47 --- /dev/null +++ b/challenge-236/zapwai/perl/ch-1.pl @@ -0,0 +1,25 @@ +use v5.30; +my @bills = (5, 5, 5, 10, 20); +#@bills = (5,5,10,10,20); +say "Input: \@bills = (" . join(", ", @bills) . ")"; +print "Output: "; +my %count; +my $flag = 1; +for my $i (0 .. $#bills) { + if ($bills[$i] == 10) { + if ($count{5} > 0) { + $count{5}--; + } else { + $flag = 0; + } + } elsif ($bills[$i] == 20) { + if (($count{5} > 0) && ($count{10} > 0)) { + $count{5}--; + $count{10}--; + } else { + $flag = 0; + } + } + $count{$bills[$i]}++; +} +$flag ? { say "True" } : { say "False" }; diff --git a/challenge-236/zapwai/perl/ch-2.pl b/challenge-236/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..67f68157dd --- /dev/null +++ b/challenge-236/zapwai/perl/ch-2.pl @@ -0,0 +1,46 @@ +use v5.30; +no warnings; +my @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10); +#@ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19); +#@ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17); +say "Input: \@ints = (" . join(", ", @ints) . ")"; +print "Output: "; +my $str; +my @used; +for my $i (0 .. $#ints) { + next if ($ints[$i] ~~ @used); + my $ret = cycle($i); + if ($ret == -1) { + break; + } else { + push @used, @$ret; + } +} +my $n = $str =~ tr/\n//; +say $n; +print $str; +sub cycle { + my $i = shift; + my $I = $ints[$i]; + if ($i == $I) { + $str .= "\t[$I]\n"; + return [$I]; + } + my @L; + push @L, $I; + my $count; + while(1) { + $I = $ints[$I]; + push @L, $I; + if ($L[0] == $I) { + $str .= "\t[@L]\n"; + return \@L; + } else { + $count++; + } + if ($count == $#ints + 1) { + $str = "[@L]"; + return -1; + } + }; +} -- cgit From 33629f292f36707ba5186fffcbe882f8d1e16a53 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 25 Sep 2023 17:05:12 +0100 Subject: Week 236 submission --- challenge-236/peter-campbell-smith/blog.txt | 1 + challenge-236/peter-campbell-smith/perl/ch-1.pl | 70 +++++++++++++++++++++++++ challenge-236/peter-campbell-smith/perl/ch-2.pl | 54 +++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 challenge-236/peter-campbell-smith/blog.txt create mode 100755 challenge-236/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-236/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-236/peter-campbell-smith/blog.txt b/challenge-236/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..fcbc342f69 --- /dev/null +++ b/challenge-236/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/236 diff --git a/challenge-236/peter-campbell-smith/perl/ch-1.pl b/challenge-236/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..c39ea506e6 --- /dev/null +++ b/challenge-236/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,70 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-09-25 +use utf8; # Week 236 task 1 - Exact change +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +exact_change(5, 5, 5, 10, 20); +exact_change(5, 5, 10, 10, 20); +exact_change(5, 5, 5, 20); +exact_change(5, 5, 10, 20); + +sub exact_change { + + my (@bills, $bill, $change, @till, $ok, $explain, $j); + + # initialise + @bills = @_; + $ok = 'true'; + + # till starts empty + $till[5] = $till[10] = $till[20] = 0; + + # loop over customers + for $bill (@bills) { + $explain .= qq[\n \$$bill paid]; + $change = $bill - 5; + + # customer presents $5 - no change needed + if ($change == 0) { + $explain .= q[, no change due]; + + # customer presents $10 so give him $5 change if we have a $5 + } elsif ($change == 5) { + if ($till[5] > 0) { + $till[5] --; + $explain .= q[, $5 change]; + } else { + $ok = 'false'; # we don't + } + + # customer presents $20 so give her a $10 and a $5, or 3 x $5 + } elsif ($change == 15) { + if ($till[10] > 0 and $till[5] > 0) { + $till[10] --; + $till[5] --; + $explain .= q[, $10 + $5 change]; + } elsif ($till[5] >= 3) { + $till[5] -= 3; + $explain .= q[, 3 x $5 change]; + } else { + $ok = 'false'; # we have neither + } + } + + # oh dear! + unless ($ok eq 'true') { + $explain .= q[, sorry, I don't have change]; + last; + } + + # add customer's payment to till + $till[$bill] ++; + $explain .= qq[, till now $till[5] x \$5, $till[10] x \$10 $till[20] x \$20 = \$] . + ($till[5] * 5 + $till[10] * 10 + $till[20] * 20); + } + + say qq[\nInput: \@bills = (] . join(', ', @bills) . ')'; + say qq[Output: $ok$explain]; +} diff --git a/challenge-236/peter-campbell-smith/perl/ch-2.pl b/challenge-236/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..31b01d5b91 --- /dev/null +++ b/challenge-236/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +use v5.16; # The Weekly Challenge - 2023-09-25 +use utf8; # Week 236 task 2 - Array loops +use strict; # Peter Campbell Smith +use warnings; # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +array_loops(4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10); +array_loops(0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19); +array_loops(9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17); + +# generate bigger example +my ($j, @used, @ints, $count); +while (1) { + $j = int(rand(100)); + next if $used[$j]; + push(@ints, $j); + $used[$j] = 1; + last if ++$count == 100; +} +array_loops(@ints); + +sub array_loops { + + my (@ints, $j, $k, $m, $loops, $loop, $explain); + + # initialise + @ints = @_; + $loops = 0; + + # loop over next unused number + for $j (0 .. scalar @ints - 1) { + next unless defined $ints[$j]; + + # loop over members of a loop, undefining numbers used + $k = $j; + while (1) { + $m = $ints[$k]; + undef $ints[$k]; + $k = $m; + last unless defined $k; + $loop .= qq[$k ]; + } + + # details of this loop + $loops ++; + $explain .= qq/ [/ . substr($loop, 0, -1) . qq/]\n/; + $loop = ''; + } + + # output results + say qq[\nInput: \@ints = (] . join(', ', @_) . ')'; + say qq[Output: $loops\n] . substr($explain, 0, -1); +} -- cgit From 198eab5c2d505233695172ca32e17022252af7c1 Mon Sep 17 00:00:00 2001 From: Michael Firkins Date: Tue, 26 Sep 2023 03:30:16 +1000 Subject: pwc236 solution in python --- challenge-236/pokgopun/python/ch-1.py | 77 +++++++++++++++++++++++++++++++++ challenge-236/pokgopun/python/ch-2.py | 80 +++++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 challenge-236/pokgopun/python/ch-1.py create mode 100644 challenge-236/pokgopun/python/ch-2.py diff --git a/challenge-236/pokgopun/python/ch-1.py b/challenge-236/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..18cd3ead0a --- /dev/null +++ b/challenge-236/pokgopun/python/ch-1.py @@ -0,0 +1,77 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-236/ +""" + +Task 1: Exact Change + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + 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 bi +ll. +For the last customer, we can not give the change of $15 back because we only ha +ve two $10 bills. +Since not every customer received the correct change, the answer is false. + +Example 3 + +Input: @bills = (5, 5, 5, 20) +Output: true + +Task 2: Array Loops +""" +### solution by pokgopun@gmail.com + +def sellable(pays): + bills = dict.fromkeys((20, 10, 5), 0) + #print(bills) + for pay in pays: + #print(f'pay = {pay}') + if pay not in bills: return False + bills[pay] += 1 + pay -= 5 + for bill in bills: + if bill > pay: continue + if bills[bill] > 0: + count = pay // bill + if count > bills[bill]: + pay -= bill*bills[bill] + bills[bill] = 0 + continue + else: + bills[bill] -= count + pay -= bill*count + continue + #print(bills) + if pay > 0: return False + return True + +for inpt, otpt in { + (5, 5, 5, 10, 20): True, + (5, 5, 10, 10, 20): False, + (5, 5, 5, 20): True, + }.items(): + print(sellable(inpt)==otpt) diff --git a/challenge-236/pokgopun/python/ch-2.py b/challenge-236/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..9740efff95 --- /dev/null +++ b/challenge-236/pokgopun/python/ch-2.py @@ -0,0 +1,80 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-236/ +""" + +Task 2: Array Loops + +Submitted by: [46]Mark Anderson + + 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. + +Example 1 + +Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +Output: 3 + +To determine the 1st loop, start at index 0, the number at that index is 4, proc +eed to index 4, the number at that index is 15, proceed to index 15 and so on un +til you're back at index 0. + +Loops are as below: +[4 15 1 6 13 5 0] +[3 8 7 18 9 16 12 17 2] +[14 11 19 10] + +Example 2 + +Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +Output: 6 + +Loops are as below: +[0] +[1] +[13 9 14 17 18 15 5 8 2] +[7 11 4 6 10 16 3] +[12] +[19] + +Example 3 + +Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +Output: 1 + +Loop is as below: +[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 1st October + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def countLoop(tup): + count = 0 + for i in range(len(tup)): + j = i + #lst = [] + while True: + j = tup[j] + #lst.append(j) + if j < i: break + if j == i: + count += 1 + #print(lst) + break + return count + +for inpt, otpt in { + (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10): 3, + (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19): 6, + (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17): 1, + }.items(): + print(countLoop(inpt)==otpt) -- cgit From f55f26ce46c572302f49ce768e7c6bf4cd8d5fdb Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 25 Sep 2023 21:49:24 +0200 Subject: feat(challenge-236/lubos-kolouch/perl,python,blog/): Challenge 236 LK Perl Python Blog --- challenge-236/lubos-kolouch/blog.txt | 1 + challenge-236/lubos-kolouch/perl/ch-1.pl | 40 ++++++++++++++++++++ challenge-236/lubos-kolouch/perl/ch-2.pl | 59 ++++++++++++++++++++++++++++++ challenge-236/lubos-kolouch/python/ch-1.py | 43 ++++++++++++++++++++++ challenge-236/lubos-kolouch/python/ch-2.py | 40 ++++++++++++++++++++ challenge-236/lubos-kolouch/raku/ch-1.raku | 31 ++++++++++++++++ challenge-236/lubos-kolouch/raku/ch-2.raku | 34 +++++++++++++++++ 7 files changed, 248 insertions(+) create mode 100644 challenge-236/lubos-kolouch/blog.txt create mode 100644 challenge-236/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-236/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-236/lubos-kolouch/python/ch-1.py create mode 100644 challenge-236/lubos-kolouch/python/ch-2.py create mode 100644 challenge-236/lubos-kolouch/raku/ch-1.raku create mode 100644 challenge-236/lubos-kolouch/raku/ch-2.raku diff --git a/challenge-236/lubos-kolouch/blog.txt b/challenge-236/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..ba518f1802 --- /dev/null +++ b/challenge-236/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-09-25_Weekly_challenge_236 diff --git a/challenge-236/lubos-kolouch/perl/ch-1.pl b/challenge-236/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..1aba5facd3 --- /dev/null +++ b/challenge-236/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,40 @@ +use strict; +use warnings; + +sub can_sell_with_exact_change { + my @bills = @_; + my $five = 0; + my $ten = 0; + + for my $bill (@bills) { + if ( $bill == 5 ) { + $five++; + } + elsif ( $bill == 10 ) { + return 0 if $five == 0; + $five--; + $ten++; + } + else { + if ( $ten > 0 && $five > 0 ) { + $ten--; + $five--; + } + elsif ( $five >= 3 ) { + $five -= 3; + } + else { + return 0; + } + } + } + + return 1; +} + +# Test Cases +print can_sell_with_exact_change( 5, 5, 5, 10, 20 ) + . "\n"; # Should return 1 (True) +print can_sell_with_exact_change( 5, 5, 10, 10, 20 ) + . "\n"; # Should return 0 (False) +print can_sell_with_exact_change( 5, 5, 5, 20 ) . "\n"; # Should return 1 (True) diff --git a/challenge-236/lubos-kolouch/perl/ch-2.pl b/challenge-236/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9c7fe5779d --- /dev/null +++ b/challenge-236/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,59 @@ +use strict; +use warnings; + +sub find_loops { + my @arr = @_; + my %visited; + my $loop_count = 0; + + for my $i ( 0 .. $#arr ) { + + # Skip if this index has been visited + next if exists $visited{$i}; + + # Initialize a new loop + $loop_count++; + my $current_index = $i; + + # Find members of this loop + while ( !exists $visited{$current_index} ) { + $visited{$current_index} = 1; + $current_index = $arr[$current_index]; + } + } + + return $loop_count; +} + +# Test cases +my @test_cases = ( + [ + [ + 4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, + 10 + ], + 3 + ], + [ + [ + 0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, + 19 + ], + 6 + ], + [ + [ + 9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, + 17 + ], + 1 + ] +); + +for my $i ( 0 .. $#test_cases ) { + my ( $arr, $expected ) = @{ $test_cases[$i] }; + my $result = find_loops(@$arr); + print "Test case ", $i + 1, ": ", + $result == $expected ? "Passed" : "Failed", + " (Got: $result, Expected: $expected)\n"; +} diff --git a/challenge-236/lubos-kolouch/python/ch-1.py b/challenge-236/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..6d2066b264 --- /dev/null +++ b/challenge-236/lubos-kolouch/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def can_sell_with_exact_change(bills: List[int]) -> bool: + """ + Determines if it is possible to sell juice to each customer with exact change. + + Args: + - bills (List[int]): List of bills from customers. + + Returns: + - bool: True if it is possible to give exact change to each customer, False otherwise. + """ + five, ten = 0, 0 # Initialize counters for $5 and $10 bills + + # Loop through each bill in the list + for bill in bills: + if bill == 5: + five += 1 # No change needed, just collect the $5 bill + elif bill == 10: + if five == 0: # Need a $5 bill to give change + return False + five -= 1 # Give a $5 bill as change + ten += 1 # Collect the $10 bill + else: # bill is $20 + if ten > 0 and five > 0: # First option to give change + ten -= 1 + five -= 1 + elif five >= 3: # Second option to give change + five -= 3 + else: + return False # Can't give change + + return True # All customers received exact change + + +# Test cases +print(can_sell_with_exact_change([5, 5, 5, 10, 20])) # Should return True +print(can_sell_with_exact_change([5, 5, 10, 10, 20])) # Should return False +print(can_sell_with_exact_change([5, 5, 5, 20])) # Should return True diff --git a/challenge-236/lubos-kolouch/python/ch-2.py b/challenge-236/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..ac3c40d8f8 --- /dev/null +++ b/challenge-236/lubos-kolouch/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Python implementation for "Array Loops" problem +from typing import List + + +def find_loops(arr: List[int]) -> int: + visited = set() + loop_count = 0 + + for i in range(len(arr)): + # Skip if this index has been visited + if i in visited: + continue + + # Initialize a new loop + loop_count += 1 + current_index = i + + # Find members of this loop + while current_index not in visited: + visited.add(current_index) + current_index = arr[current_index] + + return loop_count + + +# Test cases +test_cases = [ + ([4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10], 3), + ([0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19], 6), + ([9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17], 1), +] + +for i, (arr, expected) in enumerate(test_cases): + result = find_loops(arr) + print( + f"Test case {i+1}: {'Passed' if result == expected else 'Failed'} (Got: {result}, Expected: {expected})" + ) diff --git a/challenge-236/lubos-kolouch/raku/ch-1.raku b/challenge-236/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..fee87d1c9f --- /dev/null +++ b/challenge-236/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,31 @@ +sub can-sell-with-exact-change(@bills) { + my $five = 0; + my $ten = 0; + + for @bills -> $bill { + if $bill == 5 { + $five++; + } elsif $bill == 10 { + return False if $five == 0; + $five--; + $ten++; + } else { + if $ten > 0 && $five > 0 { + $ten--; + $five--; + } elsif $five >= 3 { + $five -= 3; + } else { + return False; + } + } + } + + return True; +} + +# Test Cases +say can-sell-with-exact-change([5, 5, 5, 10, 20]); # Should return True +say can-sell-with-exact-change([5, 5, 10, 10, 20]); # Should return False +say can-sell-with-exact-change([5, 5, 5, 20]); # Should return True + diff --git a/challenge-236/lubos-kolouch/raku/ch-2.raku b/challenge-236/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..bca37330d9 --- /dev/null +++ b/challenge-236/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,34 @@ +sub find-loops(@arr) { + my %visited; + my $loop-count = 0; + + for 0..^@arr.elems -> $i { + # Skip if this index has been visited + next if %visited{$i}:exists; + + # Initialize a new loop + $loop-count++; + my $current-index = $i; + + # Find members of this loop + while !(%visited{$current-index}:exists) { + %visited{$current-index} = True; + $current-index = @arr[$current-index]; + } + } + + return $loop-count; +} + +# Test cases +my @test-cases = ( + [[4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10], 3], + [[0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 19], 6], + [[9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17], 1] +); + +for @test-cases.kv -> $i, [$arr, $expected] { + my $result = find-loops(@$arr); + say "Test case ", $i + 1, ": ", $result == $expected ?? "Passed" !! "Failed", " (Got: $result, Expected: $expected)"; +} + -- cgit From 39871336e9eb02a28346bdce82b1745234ab6f7f Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 25 Sep 2023 22:05:27 +0200 Subject: Add solution 236 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-236/jeanluc2020/blog-1.txt | 1 + challenge-236/jeanluc2020/blog-2.txt | 1 + challenge-236/jeanluc2020/perl/ch-1.pl | 81 ++++++++++++++++++++++++++ challenge-236/jeanluc2020/perl/ch-2.pl | 98 ++++++++++++++++++++++++++++++++ challenge-236/jeanluc2020/python/ch-1.py | 35 ++++++++++++ challenge-236/jeanluc2020/python/ch-2.py | 34 +++++++++++ 6 files changed, 250 insertions(+) create mode 100644 challenge-236/jeanluc2020/blog-1.txt create mode 100644 challenge-236/jeanluc2020/blog-2.txt create mode 100755 challenge-236/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-236/jeanluc2020/perl/ch-2.pl create mode 100755 challenge-236/jeanluc2020/python/ch-1.py create mode 100755 challenge-236/jeanluc2020/python/ch-2.py diff --git a/challenge-236/jeanluc2020/blog-1.txt b/challenge-236/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..48e603fcda --- /dev/null +++ b/challenge-236/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-236-1.html diff --git a/challenge-236/jeanluc2020/blog-2.txt b/challenge-236/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..ef2868c9dd --- /dev/null +++ b/challenge-236/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-236-2.html diff --git a/challenge-236/jeanluc2020/perl/ch-1.pl b/challenge-236/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..f8d94b2f87 --- /dev/null +++ b/challenge-236/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-236/#TASK1 +# +# Task 1: Exact Change +# ==================== +# +# +# +# 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. +# +## Example 3 +## +## Input: @bills = (5, 5, 5, 20) +## Output: true +# +############################################################ +## +## discussion +## +############################################################ +# +# Each bill we get goes into our change hash. If we receive +# a bill > 5, we calculate the return value, then we try to +# pay it starting with a 10 bill if available. If in the end we +# still have something left to return, but no more fitting bills, +# we return false. Otherwise, we return true. +# +use strict; +use warnings; +use Data::Dumper; + +exact_change(5, 5, 5, 10, 20); +exact_change(5, 5, 10, 10, 20); +exact_change(5, 5, 5, 20); + +sub exact_change { + my @bills = @_; + my $change = {}; + print "Input: (" . join(", ", @bills) . ")\n"; + foreach my $bill (@bills) { + $change->{$bill}++; + my $to_return = $bill - 5; + foreach my $return_bill (10, 5) { + while ($to_return >= $return_bill && $change->{$return_bill} ) { + $to_return -= $return_bill; + $change->{$return_bill}--; + } + } + if($to_return > 0) { + print "Output: false\n"; + return; + } + } + print "Output: true\n"; +} diff --git a/challenge-236/jeanluc2020/perl/ch-2.pl b/challenge-236/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..f867968567 --- /dev/null +++ b/challenge-236/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,98 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-236/#TASK2 +# +# Task 2: Array Loops +# =================== +# +# 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. +# +## Example 1 +## +## Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +## Output: 3 +## +## To determine the 1st loop, start at index 0, the number at that index is 4, +## proceed to index 4, the number at that index is 15, proceed to index 15 and +## so on until you're back at index 0. +## +## Loops are as below: +## [4 15 1 6 13 5 0] +## [3 8 7 18 9 16 12 17 2] +## [14 11 19 10] +# +## Example 2 +## +## Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +## Output: 6 +## +## Loops are as below: +## [0] +## [1] +## [13 9 14 17 18 15 5 8 2] +## [7 11 4 6 10 16 3] +## [12] +## [19] +# +## Example 3 +## +## Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +## Output: 1 +## +## Loop is as below: +## [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +# +############################################################ +## +## discussion +## +############################################################ +# +# We start at index 0 and walk all the way up to the last index. +# Then we just calculate the loop starting at that index in the +# array. If by any chance we see an index that is already part +# of a loop, we can return. +# +use strict; +use warnings; + +array_loops(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10); +array_loops(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19); +array_loops(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17); + +my $seen_index = {}; + +sub array_loops { + my @ints = @_; + my @loops = (); + $seen_index = (); + print "Input: (" . join(", ", @ints) . ")\n"; + foreach my $index (0..$#ints) { + next if $seen_index->{$index}; + my $loop = get_loop($index, @ints); + push @loops, $loop if $loop; + } + print "Output: " . scalar(@loops) . "\n"; + foreach my $loop (@loops) { + print "[" . join(", ", @$loop) . "]\n"; + } +} + +sub get_loop { + my $index = shift; + my @ints = @_; + my $loop = []; + $index = $ints[$index]; + while(! $seen_index->{$index}) { + $seen_index->{$index} = 1; + push @$loop, $index; + $index = $ints[$index]; + } + return $loop; +} + diff --git a/challenge-236/jeanluc2020/python/ch-1.py b/challenge-236/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..b15c5c8847 --- /dev/null +++ b/challenge-236/jeanluc2020/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 +# +# This is just me learning a bit of python. +# Look at the perl solution for a discussion of the details. + +def exact_change(*args): + change = {} + print("Input: ", end="") + first = 1 + for i in args: + if first: + print("(", end="") + first = 0 + else: + print(", ", end="") + print(i, end="") + print(")") + + for bill in args: + change[bill] = change.get(bill, 0) + 1 + to_return = bill - 5 + for return_bill in [10, 5]: + change[return_bill] = change.get(return_bill, 0) + while to_return >= return_bill and change[return_bill] >= 1: + to_return -= return_bill + change[return_bill] -= 1 + if to_return > 0: + print("Output: false") + return + print("Output: true") + + +exact_change(5, 5, 5, 10, 20); +exact_change(5, 5, 10, 10, 20); +exact_change(5, 5, 5, 20); diff --git a/challenge-236/jeanluc2020/python/ch-2.py b/challenge-236/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..922e4ae219 --- /dev/null +++ b/challenge-236/jeanluc2020/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/python3 +# +# This is just me learning a bit of python. +# Look at the perl solution for a discussion of the details. + +seen_index = {} + +def get_loop(index, ints): + loop = [] + index = ints[index] + while not index in seen_index: + seen_index[index] = 1 + loop.append(index) + index = ints[index] + return loop + +def array_loops(*ints): + loops = [] + seen_index.clear() + for index in range(len(ints)): + if index in seen_index: + next + loop = get_loop(index, ints) + if len(loop) > 0: + loops.append(loop) + print("Output: ", len(loops)) + for loop in loops: + print("[", end="") + print(*loop, sep=", ", end="") + print("]") + +array_loops(4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +array_loops(0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +array_loops(9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) -- cgit From 49457a0c22fca1323966fd21850bd6bcaf62a13c Mon Sep 17 00:00:00 2001 From: deoac Date: Mon, 25 Sep 2023 16:29:52 -0400 Subject: Initial commit --- challenge-236/shimon-ben-avraham/raku/ch-1.md | 178 +++++++++++++++++++ challenge-236/shimon-ben-avraham/raku/ch-1.sl | 133 +++++++++++++++ challenge-236/shimon-ben-avraham/raku/ch-2.md | 188 +++++++++++++++++++++ challenge-236/shimon-ben-avraham/raku/ch-2.sl | 141 ++++++++++++++++ .../raku/perl-weekly-challenge.pod | 97 +++++++++++ 5 files changed, 737 insertions(+) create mode 100644 challenge-236/shimon-ben-avraham/raku/ch-1.md create mode 100755 challenge-236/shimon-ben-avraham/raku/ch-1.sl create mode 100644 challenge-236/shimon-ben-avraham/raku/ch-2.md create mode 100755 challenge-236/shimon-ben-avraham/raku/ch-2.sl create mode 100644 challenge-236/shimon-ben-avraham/raku/perl-weekly-challenge.pod diff --git a/challenge-236/shimon-ben-avraham/raku/ch-1.md b/challenge-236/shimon-ben-avraham/raku/ch-1.md new file mode 100644 index 0000000000..8d8f5c0b5f --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-1.md @@ -0,0 +1,178 @@ +# Challenge # 236 Task 1, Exact Change +> +## Table of Contents +[Submitted by: Mohammad S Anwar](#submitted-by-mohammad-s-anwar) +[The Challenge](#the-challenge) +[Example 1](#example-1) +[Example 2](#example-2) +[Example 3](#example-3) +[The Solution](#the-solution) +[TITLE](#title) +[VERSION](#version) +[SYNOPSIS](#synopsis) +[REQUIRED ARGUMENTS](#required-arguments) +[OPTIONS](#options) +[DESCRIPTION](#description) +[DIAGNOSTICS](#diagnostics) +[CONFIGURATION AND ENVIRONMENT](#configuration-and-environment) +[DEPENDENCIES](#dependencies) +[INCOMPATIBILITIES](#incompatibilities) +[BUGS AND LIMITATIONS](#bugs-and-limitations) +[AUTHOR](#author) +[LICENCE AND COPYRIGHT](#licence-and-copyright) + +---- +# Submitted by: Mohammad S Anwar +# The Challenge +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. + + +``` +## Example 3 +``` +Input: @bills = (5, 5, 5, 20) +Output: true + + +``` +# The Solution + + + + +``` + 3| multi MAIN ( ) { + 4| ; + 5| } + +``` + + + + +# TITLE + - + +# VERSION +This documentation refers to version 0.0.1 + +# SYNOPSIS +``` +# Brief working invocation example(s) here showing the most common usage(s) + +# This section will be as far as many users ever read +# so make it as educational and exemplary as possible. +``` +# REQUIRED ARGUMENTS +A complete list of every argument that must appear on the command line. when the application is invoked, explaining what each of them does, any restrictions on where each one may appear (i.e. flags that must appear before or after filenames), and how the various arguments and options may interact (e.g. mutual exclusions, required combinations, etc.) + +If all of the application's arguments are optional this section may be omitted entirely. + +# OPTIONS +A complete list of every available option with which the application can be invoked, explaining what each does, and listing any restrictions, or interactions. + +If the application has no options this section may be omitted entirely. + +# DESCRIPTION +A full description of the application and its features. May include numerous subsections (i.e. =head2, =head3, etc.) + +# DIAGNOSTICS +A list of every error and warning message that the application can generate (even the ones that will "never happen"), with a full explanation of each problem, one or more likely causes, and any suggested remedies. If the application generates exit status codes (e.g. under Unix) then list the exit status associated with each error. + +# CONFIGURATION AND ENVIRONMENT +A full explanation of any configuration system(s) used by the application, including the names and locations of any configuration files, and the meaning of any environment variables or properties that can be set. These descriptions must also include details of any configuration language used + +# DEPENDENCIES +A list of all the other modules that this module relies upon, including any restrictions on versions, and an indication whether these required modules are part of the standard Perl distribution, part of the module's distribution, or must be installed separately. + +# INCOMPATIBILITIES +A list of any modules that this module cannot be used in conjunction with. This may be due to name conflicts in the interface, or competition for system or program resources, or due to internal limitations of Perl (for example, many modules that use source code filters are mutually incompatible). + +# BUGS AND LIMITATIONS +A list of known problems with the module, together with some indication whether they are likely to be fixed in an upcoming release. + +Also a list of restrictions on the features the module does provide: data types that cannot be handled, performance issues and the circumstances in which they may arise, practical limitations on the size of data sets, special cases that are not (yet) handled, etc. + +The initial template usually just has: + +There are no known bugs in this module. + +# AUTHOR +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and Pull Requests are welcome. + +# LICENCE AND COPYRIGHT +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See [perlartistic](http://perldoc.perl.org/perlartistic.html). + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + + + + +``` + 6| multi MAIN (:$test!) { + 7| use Test; + 8| + 9| my @tests = [ + 10| %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + 11| ]; + 12| + 13| for @tests { + 14| } + 15| } + 16| + 17| my %*SUB-MAIN-OPTS = + 18| :named-anywhere, + 19| :bundling, + 20| :allow-no, + 21| :numeric-suffix-as-value, + 22| ; + 23| + 24| multi MAIN(Bool :$pod!) { + 25| for $=pod -> $pod-item { + 26| for $pod-item.contents -> $pod-block { + 27| $pod-block.raku.say; + 28| } + 29| } + 30| } + 31| + 32| multi MAIN(Bool :$doc!, Str :$format = 'Text') { + 33| run $*EXECUTABLE, "--doc=$format", $*PROGRAM; + 34| } + +``` + + + + + + +---- +Rendered from at 2023-09-25T20:12:20Z diff --git a/challenge-236/shimon-ben-avraham/raku/ch-1.sl b/challenge-236/shimon-ben-avraham/raku/ch-1.sl new file mode 100755 index 0000000000..218c1bd76a --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-1.sl @@ -0,0 +1,133 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge #236 Task 1 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 25 Sep 2023 04:25:50 PM EDT +# Version 0.0.1 + +# begin-no-weave +# always use the latest version of Raku +use v6.*; +# end-no-weave + +=begin pod +=TITLE Challenge # 236 Task 1, Exact Change + +=head1 Submitted by: Mohammad S Anwar + +=head1 The Challenge + +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. + +=head2 Example 1 + +=begin code :lang +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. +=end code + +=head2 Example 2 + +=begin code :lang +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. + +=end code + +=head2 Example 3 +=begin code :lang + +Input: @bills = (5, 5, 5, 20) +Output: true + +=end code + +=head1 The Solution + +=end pod + + +#| The actual program starts here. +multi MAIN ( ) { + ; +} # end of multi MAIN ( ) + + +=begin pod + +=head1 AUTHOR + +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and +Pull Requests are welcome. + +=head1 LICENCE AND COPYRIGHT + +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. +See L. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +=end pod + +#| Run with the option '--test' to test the program +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + ]; + + for @tests { +# cmp-ok ., ., ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + +my %*SUB-MAIN-OPTS = + :named-anywhere, # allow named variables at any location + :bundling, # allow bundling of named arguments +# :coerce-allomorphs-to(Str), # coerce allomorphic arguments to given type + :allow-no, # allow --no-foo as alternative to --/foo + :numeric-suffix-as-value, # allow -j2 as alternative to --j=2 +; + +#| Run with '--pod' to see all of the POD6 objects +multi MAIN(Bool :$pod!) { + for $=pod -> $pod-item { + for $pod-item.contents -> $pod-block { + $pod-block.raku.say; + } + } +} # end of multi MAIN (:$pod) + +#| Run with '--doc' to generate a document from the POD6 +#| It will be rendered in Text format +#| unless specified with the --format option. e.g. +#| --format=HTML +multi MAIN(Bool :$doc!, Str :$format = 'Text') { + run $*EXECUTABLE, "--doc=$format", $*PROGRAM; +} # end of multi MAIN(Bool :$man!) + + diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.md b/challenge-236/shimon-ben-avraham/raku/ch-2.md new file mode 100644 index 0000000000..8625d6862c --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.md @@ -0,0 +1,188 @@ +# Challenge # 236 Task 2, Array Loops +> +## Table of Contents +[Submitted By: Mark Anderson](#submitted-by-mark-anderson) +[The Challenge You are given an array of unique integers.](#the-challenge-you-are-given-an-array-of-unique-integers) +[Example 1](#example-1) +[Example 2](#example-2) +[Example 3](#example-3) +[The Solution](#the-solution) +[TITLE](#title) +[VERSION](#version) +[SYNOPSIS](#synopsis) +[REQUIRED ARGUMENTS](#required-arguments) +[OPTIONS](#options) +[DESCRIPTION](#description) +[DIAGNOSTICS](#diagnostics) +[CONFIGURATION AND ENVIRONMENT](#configuration-and-environment) +[DEPENDENCIES](#dependencies) +[INCOMPATIBILITIES](#incompatibilities) +[BUGS AND LIMITATIONS](#bugs-and-limitations) +[AUTHOR](#author) +[LICENCE AND COPYRIGHT](#licence-and-copyright) + +---- +# Submitted By: Mark Anderson +# The Challenge 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.** + + +## Example 1 +``` +Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +Output: 3 + +To determine the 1st loop, start at index 0, the number at that index is 4, +proceed to index 4, the number at that index is 15, proceed to index 15 and so +on until you're back at index 0. + +Loops are as below: +[4 15 1 6 13 5 0] +[3 8 7 18 9 16 12 17 2] +[14 11 19 10] + + +``` +## Example 2 +``` +Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +Output: 6 + +Loops are as below: +[0] +[1] +[13 9 14 17 18 15 5 8 2] +[7 11 4 6 10 16 3] +[12] +[19] + +``` +## Example 3 +``` +Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +Output: 1 + +Loop is as below: +[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] + +``` +# The Solution + + + + +``` + 3| multi MAIN ( ) { + 4| ; + 5| } + +``` + + + + +# TITLE + - + +# VERSION +This documentation refers to version 0.0.1 + +# SYNOPSIS +``` +# Brief working invocation example(s) here showing the most common usage(s) + +# This section will be as far as many users ever read +# so make it as educational and exemplary as possible. +``` +# REQUIRED ARGUMENTS +A complete list of every argument that must appear on the command line. when the application is invoked, explaining what each of them does, any restrictions on where each one may appear (i.e. flags that must appear before or after filenames), and how the various arguments and options may interact (e.g. mutual exclusions, required combinations, etc.) + +If all of the application's arguments are optional this section may be omitted entirely. + +# OPTIONS +A complete list of every available option with which the application can be invoked, explaining what each does, and listing any restrictions, or interactions. + +If the application has no options this section may be omitted entirely. + +# DESCRIPTION +A full description of the application and its features. May include numerous subsections (i.e. =head2, =head3, etc.) + +# DIAGNOSTICS +A list of every error and warning message that the application can generate (even the ones that will "never happen"), with a full explanation of each problem, one or more likely causes, and any suggested remedies. If the application generates exit status codes (e.g. under Unix) then list the exit status associated with each error. + +# CONFIGURATION AND ENVIRONMENT +A full explanation of any configuration system(s) used by the application, including the names and locations of any configuration files, and the meaning of any environment variables or properties that can be set. These descriptions must also include details of any configuration language used + +# DEPENDENCIES +A list of all the other modules that this module relies upon, including any restrictions on versions, and an indication whether these required modules are part of the standard Perl distribution, part of the module's distribution, or must be installed separately. + +# INCOMPATIBILITIES +A list of any modules that this module cannot be used in conjunction with. This may be due to name conflicts in the interface, or competition for system or program resources, or due to internal limitations of Perl (for example, many modules that use source code filters are mutually incompatible). + +# BUGS AND LIMITATIONS +A list of known problems with the module, together with some indication whether they are likely to be fixed in an upcoming release. + +Also a list of restrictions on the features the module does provide: data types that cannot be handled, performance issues and the circumstances in which they may arise, practical limitations on the size of data sets, special cases that are not (yet) handled, etc. + +The initial template usually just has: + +There are no known bugs in this module. + +# AUTHOR +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and Pull Requests are welcome. + +# LICENCE AND COPYRIGHT +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See [perlartistic](http://perldoc.perl.org/perlartistic.html). + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + + + + +``` + 6| multi MAIN (:$test!) { + 7| use Test; + 8| + 9| my @tests = [ + 10| %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + 11| ]; + 12| + 13| for @tests { + 14| } + 15| } + 16| + 17| my %*SUB-MAIN-OPTS = + 18| :named-anywhere, + 19| :bundling, + 20| :allow-no, + 21| :numeric-suffix-as-value, + 22| ; + 23| + 24| multi MAIN(Bool :$pod!) { + 25| for $=pod -> $pod-item { + 26| for $pod-item.contents -> $pod-block { + 27| $pod-block.raku.say; + 28| } + 29| } + 30| } + 31| + 32| multi MAIN(Bool :$doc!, Str :$format = 'Text') { + 33| run $*EXECUTABLE, "--doc=$format", $*PROGRAM; + 34| } + +``` + + + + + + +---- +Rendered from at 2023-09-25T20:20:54Z diff --git a/challenge-236/shimon-ben-avraham/raku/ch-2.sl b/challenge-236/shimon-ben-avraham/raku/ch-2.sl new file mode 100755 index 0000000000..3c8a4ad460 --- /dev/null +++ b/challenge-236/shimon-ben-avraham/raku/ch-2.sl @@ -0,0 +1,141 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge #236 Task 2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 25 Sep 2023 04:22:22 PM EDT +# Version 0.0.1 + +# begin-no-weave +# always use the latest version of Raku +use v6.*; +# end-no-weave + +=begin pod +=TITLE Challenge # 236 Task 2, Array Loops + +=head1 Submitted By: Mark Anderson + +=head1 The Challenge + +You are given an array of unique integers. + +Write a script to determine how many loops are in the given array. + +=defn +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. + +=head2 Example 1 + +=begin code :lang +Input: @ints = (4,6,3,8,15,0,13,18,7,16,14,19,17,5,11,1,12,2,9,10) +Output: 3 + +To determine the 1st loop, start at index 0, the number at that index is 4, +proceed to index 4, the number at that index is 15, proceed to index 15 and so +on until you're back at index 0. + +Loops are as below: +[4 15 1 6 13 5 0] +[3 8 7 18 9 16 12 17 2] +[14 11 19 10] + +=end code + +=head2 Example 2 + +=begin code :lang +Input: @ints = (0,1,13,7,6,8,10,11,2,14,16,4,12,9,17,5,3,18,15,19) +Output: 6 + +Loops are as below: +[0] +[1] +[13 9 14 17 18 15 5 8 2] +[7 11 4 6 10 16 3] +[12] +[19] +=end code + +=head2 Example 3 + +=begin code :lang +Input: @ints = (9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17) +Output: 1 + +Loop is as below: +[9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0] +=end code + + +=head1 The Solution +=end pod + + +#| The actual program starts here. +multi MAIN ( ) { + ; +} # end of multi MAIN ( ) + + +=begin pod + +=head1 AUTHOR + +Shimon Bollinger (deoac.shimon@gmail.com) + +Source can be located at: https://github.com/deoac/... . Comments and +Pull Requests are welcome. + +=head1 LICENCE AND COPYRIGHT + +© 2023 Shimon Bollinger. All rights reserved. + +This module is free software; you can redistribute it and/or +modify it under the same terms as Perl itself. +See L. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +=end pod + +#| Run with the option '--test' to test the program +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ got => '', op => 'eq', expected => '', desc => 'Example 1' }, + ]; + + for @tests { +# cmp-ok ., ., ., .; + } # end of for @tests +} # end of multi MAIN (:$test!) + +my %*SUB-MAIN-OPTS = + :named-anywhere, # allow named variables at any location + :bundling, # allow bundling of name