aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2021-07-24 18:40:13 +1000
committerSimon Green <mail@simon.green>2021-07-24 18:40:13 +1000
commitac4f07a6fbb5e534a97d14f1f76da8d5d17fb8a9 (patch)
tree01220defd6f98db2c4e2819b11bb82d8c8279942
parent389a47dc49b5ec4a4dba9c46b361d252fd5bf78a (diff)
downloadperlweeklychallenge-club-ac4f07a6fbb5e534a97d14f1f76da8d5d17fb8a9.tar.gz
perlweeklychallenge-club-ac4f07a6fbb5e534a97d14f1f76da8d5d17fb8a9.tar.bz2
perlweeklychallenge-club-ac4f07a6fbb5e534a97d14f1f76da8d5d17fb8a9.zip
sgreen solution to challenge 122
-rw-r--r--challenge-122/sgreen/blog.txt1
-rwxr-xr-xchallenge-122/sgreen/perl/ch-1.pl18
-rwxr-xr-xchallenge-122/sgreen/perl/ch-2.pl28
3 files changed, 47 insertions, 0 deletions
diff --git a/challenge-122/sgreen/blog.txt b/challenge-122/sgreen/blog.txt
new file mode 100644
index 0000000000..221190c7ce
--- /dev/null
+++ b/challenge-122/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-122-4np8
diff --git a/challenge-122/sgreen/perl/ch-1.pl b/challenge-122/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..a0e5e9efd3
--- /dev/null
+++ b/challenge-122/sgreen/perl/ch-1.pl
@@ -0,0 +1,18 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my @N = @_;
+
+ # Sanity check
+ die "You must specify one or more numbers\n" unless @N;
+ die "One or more values is not a number\n" if grep { !/^[0-9]+(?:\.[0-9]+)?$/ } @N;
+
+ my $cnt = my $sum = 0;
+ say join ' ', map { ++$cnt; $sum += $_; $sum / $cnt } @N;
+}
+
+main(@ARGV);
diff --git a/challenge-122/sgreen/perl/ch-2.pl b/challenge-122/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..5a194322b1
--- /dev/null
+++ b/challenge-122/sgreen/perl/ch-2.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub _score {
+ my ( $remaining, $these_shots ) = @_;
+
+ if ( $remaining <= 3 ) {
+ # Display this score
+ say join ' ', @$these_shots, $remaining;
+ }
+
+ foreach my $shot ( 1, 2, 3 ) {
+ # Call the recursive function again
+ _score( $remaining - $shot, [ @$these_shots, $shot ] ) if $remaining > $shot;
+ }
+}
+
+sub main {
+ my $S = shift;
+
+ # Call a recursive function to calculate the possible shots
+ _score( $S, [] );
+}
+
+main(@ARGV);