aboutsummaryrefslogtreecommitdiff
path: root/challenge-236
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2023-09-30 10:45:57 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2023-09-30 10:45:57 +0100
commit4f0cf89dcc92da3c3852d25b3e14e441f5787e04 (patch)
treeaeabe8291950f6f05188d09fe0a0ddc98011efc5 /challenge-236
parent8188967e793d50142991bf3bceda8d5aec305746 (diff)
downloadperlweeklychallenge-club-4f0cf89dcc92da3c3852d25b3e14e441f5787e04.tar.gz
perlweeklychallenge-club-4f0cf89dcc92da3c3852d25b3e14e441f5787e04.tar.bz2
perlweeklychallenge-club-4f0cf89dcc92da3c3852d25b3e14e441f5787e04.zip
- Added solutions by Laurent Rosenfeld.
- Added solutions by Robert DiCicco. - Added solutions by Bob Lied. - Added solutions by Steven Wilson. - Added solutions by Ali Moradi. - Added solutions by Niels van Dijke. - Added solutions by Athanasius. - Added solutions by Yves Orton. - Added solutions by Dave Jacoby. - Added solutions by Shimon Bollinger.
Diffstat (limited to 'challenge-236')
-rw-r--r--challenge-236/laurent-rosenfeld/blog1.txt1
-rw-r--r--challenge-236/laurent-rosenfeld/perl/ch-1.pl34
-rw-r--r--challenge-236/laurent-rosenfeld/raku/ch-1.raku33
-rwxr-xr-xchallenge-236/perlboy1967/perl/ch-1.pl (renamed from challenge-236/perlboy1967/perl/ch1.pl)0
-rwxr-xr-xchallenge-236/perlboy1967/perl/ch-2.pl (renamed from challenge-236/perlboy1967/perl/ch2.pl)0
-rw-r--r--challenge-236/robert-dicicco/perl/ch-2.pl94
-rw-r--r--challenge-236/robert-dicicco/python/ch-2.py93
-rw-r--r--challenge-236/robert-dicicco/raku/ch-2.raku97
-rw-r--r--challenge-236/robert-dicicco/ruby/ch-2.rb102
-rw-r--r--challenge-236/shimon-ben-avraham/blog.txt1
-rw-r--r--challenge-236/steven-wilson/python/ch-1.py (renamed from challenge-236/steven-wilson/python/ch-01.py)0
-rw-r--r--challenge-236/steven-wilson/python/ch-2.py (renamed from challenge-236/steven-wilson/python/ch-02.py)0
12 files changed, 455 insertions, 0 deletions
diff --git a/challenge-236/laurent-rosenfeld/blog1.txt b/challenge-236/laurent-rosenfeld/blog1.txt
new file mode 100644
index 0000000000..f9aeda964c
--- /dev/null
+++ b/challenge-236/laurent-rosenfeld/blog1.txt
@@ -0,0 +1 @@
+https://blogs.perl.org/users/laurent_r/2023/09/perl-weekly-challenge-236-exact-change.html
diff --git a/challenge-236/laurent-rosenfeld/perl/ch-1.pl b/challenge-236/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..5d193a51c8
--- /dev/null
+++ b/challenge-236/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,34 @@
+use strict;
+use warnings;
+use feature 'say';
+
+sub sell_juice {
+ # %change stores the stock of bank notes. No need to
+ # count $20 notes but it makes the code more generic
+ my %change = ('5' => 0, '10' => 0, '20' => 0);
+ for my $i (@_){
+ $change{$i}++;
+ next if $i == 5;
+ if ($i == 10) {
+ return "false" if $change{5} < 1;
+ $change{5}--;
+ next;
+ } elsif ($i == 20) {
+ if ($change{10} > 0 and $change{5} > 0) {
+ $change{10}--; $change{5}--;
+ next;
+ } elsif ($change{5} >= 3) {
+ $change{5} -= 3; next;
+ } else {
+ return "false";
+ }
+ }
+ }
+ return "true";
+}
+
+my @tests = ([<5 5 5 10 20>], [<5 5 10 10 20>], [<5 5 5 20>]);
+for my $test (@tests) {
+ printf "%-15s => ", "@$test";
+ say sell_juice @$test;
+}
diff --git a/challenge-236/laurent-rosenfeld/raku/ch-1.raku b/challenge-236/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..6152b6c528
--- /dev/null
+++ b/challenge-236/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,33 @@
+sub sell-juice (@in) {
+ # %change stores the stock of bank notes. No need to
+ # count $20 notes but it makes the code more generic
+ my %change = '5' => 0, '10' => 0, '20' => 0;
+ for @in -> $i {
+ %change{$i}++;
+ given $i {
+ when 5 {next}
+ when 10 {
+ return False if %change{5} < 1;
+ %change{5}--;
+ next;
+ }
+ when 20 {
+ if %change{10} > 0 and %change{5} > 0 {
+ %change{10}--; %change{5}--;
+ next;
+ } elsif %change{5} >= 3 {
+ %change{5} -= 3; next;
+ } else {
+ return False;
+ }
+ }
+ }
+ }
+ return True;
+}
+
+my @tests = <5 5 5 10 20>, <5 5 10 10 20>, <5 5 5 20>;
+for @tests -> @test {
+ printf "%-15s => ", "@test[]";
+ say sell-juice @test;
+}
diff --git a/challenge-236/perlboy1967/perl/ch1.pl b/challenge-236/perlboy1967/perl/ch-1.pl
index c0d8f73b9f..c0d8f73b9f 100755
--- a/challenge-236/perlboy1967/perl/ch1.pl
+++ b/challenge-236/perlboy1967/perl/ch-1.pl
diff --git a/challenge-236/perlboy1967/perl/ch2.pl b/challenge-236/perlboy1967/perl/ch-2.pl
index 0df3f42d16..0df3f42d16 100755
--- a/challenge-236/perlboy1967/perl/ch2.pl
+++ b/challenge-236/perlboy1967/perl/ch-2.pl
diff --git a/challenge-236/robert-dicicco/perl/ch-2.pl b/challenge-236/robert-dicicco/perl/ch-2.pl
new file mode 100644
index 0000000000..3f73b23c2b
--- /dev/null
+++ b/challenge-236/robert-dicicco/perl/ch-2.pl
@@ -0,0 +1,94 @@
+#!/usr/bin/env perl
+=begin comment
+---------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 09-26-2023
+Challenge 236 Task 02 Array Loops ( Perl )
+---------------------------------------------
+=cut
+use v5.38;
+
+my @myints =([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,1],
+ [9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]);
+
+my @seen = ();
+my $lps = 0;
+
+sub WalkLoop ($m, $s) {
+ my @outloop = ();
+ my $start = $m->[$s];
+ my $v = $m->[$start];
+ if ($v == $start) {
+ push(@seen, $s);
+ push(@outloop, $s);
+ my $ol = scalar @outloop;
+ if ($ol == 1) {
+ say "\tLoop: [@outloop]";
+ $lps++;
+ }
+ return;
+ } else {
+ push(@seen, $start);
+ push(@outloop, $start);
+ push(@seen, $v);
+ push(@outloop, $v);
+ }
+ while(1) {
+ $v = $m->[$v];
+ if ( grep( /^$v$/, @seen ) ) {
+ last;
+ }
+ if ( $v == $start) {
+ last;
+ } else {
+ push(@seen, $v);
+ push(@outloop, $v);
+ }
+ }
+ my $ol = scalar @outloop;
+ if (($ol > 2) or ($ol == 1)) {
+ say "\tLoop: [@outloop]";
+ $lps++;
+ }
+}
+
+for my $mints (@myints) {
+ @seen = ();
+ $lps = 0;
+ print "Input: \@myints = [@$mints]\n";
+ my $cnt = 0;
+ while ($cnt < 20) {
+ WalkLoop($mints,$cnt);
+ $cnt++;
+ }
+ say "\tOutput: $lps";
+ say "-----------------------------------";
+}
+
+=begin comment
+---------------------------------------------
+SAMPLE OUTPUT
+perl .\ArrayLoops.pl
+
+Input: @myints = [4 6 3 8 15 0 13 18 7 16 14 19 17 5 11 1 12 2 9 10]
+ Loop: [4 15 1 6 13 5 0]
+ Loop: [3 8 7 18 9 16 12 17 2]
+ Loop: [14 11 19 10]
+ Output: 3
+-----------------------------------
+Input: @myints = [0 1 13 7 6 8 10 11 2 14 16 4 12 9 17 5 3 18 15 1]
+ Loop: [0]
+ Loop: [1]
+ Loop: [13 9 14 17 18 15 5 8 2]
+ Loop: [7 11 4 6 10 16 3]
+ Loop: [12]
+ Loop: [19]
+ Output: 6
+-----------------------------------
+Input: @myints = [9 8 3 11 5 7 13 19 12 4 14 10 18 2 16 1 0 15 6 17]
+ Loop: [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0]
+ Output: 1
+---------------------------------------------
+=cut
+
+
diff --git a/challenge-236/robert-dicicco/python/ch-2.py b/challenge-236/robert-dicicco/python/ch-2.py
new file mode 100644
index 0000000000..f1dfc8157e
--- /dev/null
+++ b/challenge-236/robert-dicicco/python/ch-2.py
@@ -0,0 +1,93 @@
+#!/usr/bin/env python
+'''
+---------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 09-26-2023
+Challenge 236 Task 02 Array Loops ( Python )
+---------------------------------------------
+'''
+
+myints = [
+ [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,1],
+ [9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]
+ ]
+
+seen = []
+lps = 0
+
+def WalkLoop(m,s):
+ global lps
+ outloop = []
+ start = m[s]
+ v = m[start]
+ if v == start:
+ seen.append(s)
+ outloop.append(s)
+ ol = len(outloop)
+ if ol == 1:
+ print(f"\tLoop: {outloop}")
+ lps += 1
+ return
+ else:
+ seen.append(start)
+ outloop.append(start)
+ seen.append(v)
+ outloop.append(v)
+ while 1:
+ v = m[v]
+ if v in seen:
+ break
+ if v == start:
+ break
+ else:
+ seen.append(v)
+ outloop.append(v)
+ ol = len(outloop)
+ if ol > 2 or ol == 1:
+ print(f"\tLoop: {outloop}")
+ lps += 1
+
+
+for mints in myints:
+ print(f"Input: @ints = {mints}")
+ seen = []
+ lps = 0
+ cnt = 0
+ while cnt < 20:
+ WalkLoop(mints, cnt)
+ cnt += 1
+ print(f"\n\tOutput: {lps}")
+ print("----------------------------------------------------")
+
+
+'''
+----------------------------------------------------
+SAMPLE OUTPUT
+python .\ArrayLoops.py
+
+Input: @ints = [4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10]
+ Loop: [4, 15, 1, 6, 13, 5, 0]
+ Loop: [3, 8, 7, 18, 9, 16, 12, 17, 2]
+ Loop: [14, 11, 19, 10]
+
+ Output: 3
+----------------------------------------------------
+Input: @ints = [0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 1]
+ Loop: [0]
+ Loop: [1]
+ Loop: [13, 9, 14, 17, 18, 15, 5, 8, 2]
+ Loop: [7, 11, 4, 6, 10, 16, 3]
+ Loop: [12]
+ Loop: [19]
+
+ Output: 6
+----------------------------------------------------
+Input: @ints = [9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17]
+ Loop: [9, 4, 5, 7, 19, 17, 15, 1, 8, 12, 18, 6, 13, 2, 3, 11, 10, 14, 16, 0]
+
+ Output: 1
+----------------------------------------------------
+'''
+
+
diff --git a/challenge-236/robert-dicicco/raku/ch-2.raku b/challenge-236/robert-dicicco/raku/ch-2.raku
new file mode 100644
index 0000000000..78f6f77a89
--- /dev/null
+++ b/challenge-236/robert-dicicco/raku/ch-2.raku
@@ -0,0 +1,97 @@
+#!/usr/bin/env raku
+=begin comment
+---------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 09-26-2023
+Challenge 236 Task 02 Array Loops ( Raku )
+---------------------------------------------
+=end comment
+use v6;
+
+my @myints =([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,1],
+ [9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]);
+
+my @seen = ();
+my $lps = 0;
+
+sub WalkLoop (@m, $s) {
+ my @outloop = ();
+ my $start = @m[$s];
+ my $v = @m[$start];
+ if $v == $start {
+ @seen.push($s);
+ @outloop.push($s);
+ my $ol = @outloop.elems;
+ if $ol == 1 {
+ say "\tLoop: ",@outloop;
+ $lps++;
+ }
+ return;
+ } else {
+ @seen.push($start);
+ @outloop.push($start);
+ @seen.push($v);
+ @outloop.push($v);
+ }
+ while (1) {
+ $v = @m[$v];
+ if grep( /^$v$/, @seen ) {
+ last;
+ }
+ if ( $v == $start) {
+ last;
+ } else {
+ @seen.push($v);
+ @outloop.push($v);
+ }
+ }
+ my $ol = @outloop.elems;
+ if $ol > 2 or $ol == 1 {
+ say "\tLoop: ",@outloop;
+ $lps++;
+ }
+}
+
+for (@myints) -> @mints {
+ @seen = ();
+ $lps = 0;
+ say "Input: \@ints = ",@mints;
+ my $cnt = 0;
+ while $cnt < 20 {
+ WalkLoop(@mints,$cnt);
+ $cnt++;
+ }
+ say "\tOutput: $lps";
+ say "-----------------------------------";
+}
+
+=begin comment
+---------------------------------------------
+SAMPLE OUTPUT
+
+raku .\ArrayLoops.rk
+
+Input: @ints = [4 6 3 8 15 0 13 18 7 16 14 19 17 5 11 1 12 2 9 10]
+ Loop: [4 15 1 6 13 5 0]
+ Loop: [3 8 7 18 9 16 12 17 2]
+ Loop: [14 11 19 10]
+ Output: 3
+-----------------------------------
+Input: @ints = [0 1 13 7 6 8 10 11 2 14 16 4 12 9 17 5 3 18 15 1]
+ Loop: [0]
+ Loop: [1]
+ Loop: [13 9 14 17 18 15 5 8 2]
+ Loop: [7 11 4 6 10 16 3]
+ Loop: [12]
+ Loop: [19]
+ Output: 6
+-----------------------------------
+Input: @ints = [9 8 3 11 5 7 13 19 12 4 14 10 18 2 16 1 0 15 6 17]
+ Loop: [9 4 5 7 19 17 15 1 8 12 18 6 13 2 3 11 10 14 16 0]
+ Output: 1
+---------------------------------------------
+=end comment
+
+
+
+
diff --git a/challenge-236/robert-dicicco/ruby/ch-2.rb b/challenge-236/robert-dicicco/ruby/ch-2.rb
new file mode 100644
index 0000000000..1ba766c313
--- /dev/null
+++ b/challenge-236/robert-dicicco/ruby/ch-2.rb
@@ -0,0 +1,102 @@
+#!/usr/bin/env ruby
+=begin
+---------------------------------------------
+AUTHOR: Robert DiCicco
+DATE : 09-26-2023
+Challenge 236 Task 02 Array Loops ( Ruby )
+---------------------------------------------
+=end
+
+myints = [
+ [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,1],
+ [9,8,3,11,5,7,13,19,12,4,14,10,18,2,16,1,0,15,6,17]
+ ]
+$seen = Array.new
+$lps = 0
+
+def WalkLoop (m, s)
+ outloop = Array.new
+ start = m[s]
+ v = m[start]
+ if v == start
+ $seen.push(s)
+ outloop.push(s)
+ ol = outloop.length()
+ if ol == 1
+ puts("\tLoop: #{outloop}")
+ $lps += 1
+ end
+ return
+ else
+ $seen.push(start)
+ outloop.push(start)
+ $seen.push(v)
+ outloop.push(v)
+ end
+ while (1)
+ v = m[v]
+ if $seen.include? v
+ break
+ end
+ if ( v == start)
+ break
+ else
+ $seen.push(v)
+ outloop.push(v)
+ end
+ end
+ ol = outloop.length()
+ if ol > 2 || ol == 1
+ puts("\tLoop: #{outloop}")
+ $lps += 1
+ end
+
+end
+
+myints.each do |mints|
+ $seen = Array.new
+ $lps = 0
+ puts("Input: @ints = #{mints}")
+ cnt = 0
+ while cnt < 20
+ WalkLoop(mints, cnt)
+ cnt += 1
+ end
+ puts("\n\tOutput: #{$lps}")
+ puts("----------------------------------------------------")
+end
+
+=begin
+SAMPLE OUTPUT
+ruby .\ArrayLoops.rb
+
+Input: @ints = [4, 6, 3, 8, 15, 0, 13, 18, 7, 16, 14, 19, 17, 5, 11, 1, 12, 2, 9, 10]
+ Loop: [4, 15, 1, 6, 13, 5, 0]
+ Loop: [3, 8, 7, 18, 9, 16, 12, 17, 2]
+ Loop: [14, 11, 19, 10]
+
+ Output: 3
+----------------------------------------------------
+Input: @ints = [0, 1, 13, 7, 6, 8, 10, 11, 2, 14, 16, 4, 12, 9, 17, 5, 3, 18, 15, 1]
+ Loop: [0]
+ Loop: [1]
+ Loop: [13, 9, 14, 17, 18, 15, 5, 8, 2]
+ Loop: [7, 11, 4, 6, 10, 16, 3]
+ Loop: [12]
+ Loop: [19]
+
+ Output: 6
+----------------------------------------------------
+Input: @ints = [9, 8, 3, 11, 5, 7, 13, 19, 12, 4, 14, 10, 18, 2, 16, 1, 0, 15, 6, 17]
+ Loop: [9, 4, 5, 7, 19, 17, 15, 1, 8, 12, 18, 6, 13, 2, 3, 11, 10, 14, 16, 0]
+
+ Output: 1
+----------------------------------------------------
+=end
+
+
+
+
+
+
diff --git a/challenge-236/shimon-ben-avraham/blog.txt b/challenge-236/shimon-ben-avraham/blog.txt
new file mode 100644
index 0000000000..1ad8d65f01
--- /dev/null
+++ b/challenge-236/shimon-ben-avraham/blog.txt
@@ -0,0 +1 @@
+https://github.com/manwar/perlweeklychallenge-club/tree/master/challenge-236/shimon-ben-avraham#readme
diff --git a/challenge-236/steven-wilson/python/ch-01.py b/challenge-236/steven-wilson/python/ch-1.py
index 8bea4269ad..8bea4269ad 100644
--- a/challenge-236/steven-wilson/python/ch-01.py
+++ b/challenge-236/steven-wilson/python/ch-1.py
diff --git a/challenge-236/steven-wilson/python/ch-02.py b/challenge-236/steven-wilson/python/ch-2.py
index f854671e81..f854671e81 100644
--- a/challenge-236/steven-wilson/python/ch-02.py
+++ b/challenge-236/steven-wilson/python/ch-2.py