aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-24 10:49:28 +0100
committerGitHub <noreply@github.com>2021-07-24 10:49:28 +0100
commit4be8d9a4602a9d696ef3f70faae5210e36e1eb52 (patch)
tree24dca7ac3ac1f54ea35cbc0c8d4f7366a5a2cc55
parentf3659c61ec95ff25bf99c4ba9398813197002c56 (diff)
parentac4f07a6fbb5e534a97d14f1f76da8d5d17fb8a9 (diff)
downloadperlweeklychallenge-club-4be8d9a4602a9d696ef3f70faae5210e36e1eb52.tar.gz
perlweeklychallenge-club-4be8d9a4602a9d696ef3f70faae5210e36e1eb52.tar.bz2
perlweeklychallenge-club-4be8d9a4602a9d696ef3f70faae5210e36e1eb52.zip
Merge pull request #4584 from simongreen-net/swg-122
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);