aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-26 08:48:36 +0100
committerGitHub <noreply@github.com>2021-07-26 08:48:36 +0100
commit102dc36ea04e3e38bcd381fb15627d1862ce9ecf (patch)
treeaa4febec4298a2df72718d5e8d2ad8f5a769ca4a
parent95ffd74dc207eabbc1a58b0a39f5a6cc53c99ded (diff)
parent756072ff779d1de5723e31d8889bf2b368c12b81 (diff)
downloadperlweeklychallenge-club-102dc36ea04e3e38bcd381fb15627d1862ce9ecf.tar.gz
perlweeklychallenge-club-102dc36ea04e3e38bcd381fb15627d1862ce9ecf.tar.bz2
perlweeklychallenge-club-102dc36ea04e3e38bcd381fb15627d1862ce9ecf.zip
Merge pull request #4598 from mattneleigh/branch-for-challenge-122
new file: challenge-122/mattneleigh/perl/ch-1.pl
-rwxr-xr-xchallenge-122/mattneleigh/perl/ch-1.pl46
-rwxr-xr-xchallenge-122/mattneleigh/perl/ch-2.pl102
2 files changed, 148 insertions, 0 deletions
diff --git a/challenge-122/mattneleigh/perl/ch-1.pl b/challenge-122/mattneleigh/perl/ch-1.pl
new file mode 100755
index 0000000000..150881a332
--- /dev/null
+++ b/challenge-122/mattneleigh/perl/ch-1.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use English;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @args = (10, 20, 30, 40, 50, 60, 70, 80, 90);
+
+stream_average(@args);
+
+exit(0);
+################################################################################
+# End main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Print to STDOUT a running average of a list of numbers- that is to say, for
+# each number in the list, print the average of every number seen up to that
+# point, including said number
+# Takes a list of numbers as a series of arguments; this list may be of any
+# length
+# Returns no meaningful value
+################################################################################
+sub stream_average{
+
+ my $i;
+ my $accumulator = 0;
+
+ for($i=0; $i<scalar(@ARG); $i++){
+ $accumulator += $ARG[$i];
+ print(
+ $accumulator / ($i + 1),
+ ($i == $#ARG) ? "\n" : ", "
+ );
+ }
+
+}
+
+
+
diff --git a/challenge-122/mattneleigh/perl/ch-2.pl b/challenge-122/mattneleigh/perl/ch-2.pl
new file mode 100755
index 0000000000..4cf156b2e0
--- /dev/null
+++ b/challenge-122/mattneleigh/perl/ch-2.pl
@@ -0,0 +1,102 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+################################################################################
+# Begin main execution
+################################################################################
+
+my @scores = (4, 5);
+
+# Uncomment this list instead if you
+# want to see some epic sets of
+# permutations- 121415 and 53798080
+# respectively
+# my @scores = (20, 30);
+
+foreach(@scores){
+ print("Score: $_\n");
+ print("Permutations:\n");
+ print_score_permutations($_);
+ print("\n");
+}
+
+
+exit(0);
+################################################################################
+# End of main execution; subroutines follow
+################################################################################
+
+
+
+################################################################################
+# Calculate all the permutations of baskets (free throws worth one point,
+# regular baskets worth two, three-pointers worth, er, three) that add up to a
+# particular specified basketball score, and write each basket sequence to
+# STDOUT
+# Takes one argument:
+# * The score to examine
+# Writes output to STDOUT
+# Returns no meaningful value
+################################################################################
+sub print_score_permutations{
+ my $score = shift();
+
+ my $shot_stack = [];
+
+ _score_recursor($score, $shot_stack);
+
+}
+
+
+
+################################################################################
+# The recursive core of the score permutation calculator
+# Takes two arguments:
+# * The number of points not yet accounted for
+# * A reference to a list of shots already examined, which will be treated as a
+# stack by the various recursive calls to this function; when the contents of
+# the stack add up to the total score specified in the call to
+# print_score_permutations(), the contents will be written to STDOUT
+# Writes output to STDOUT
+# Returns no meaningful value
+# NOTE: This should ONLY be called by score_permutations(), which does some
+# initial setup
+################################################################################
+sub _score_recursor{
+ my $score = shift();
+ my $shot_stack = shift();
+
+ my $shot;
+
+ foreach $shot (1, 2, 3){
+ # No need to proceed if the value of
+ # the current shot exceeds the number
+ # of points remaining
+ last if($shot > $score);
+
+ # Push the current shot onto the shot
+ # stack
+ push(@{$shot_stack}, $shot);
+
+ if($score - $shot){
+ # Subtracting this basket, the score
+ # is nonzero- go a level deeper with
+ # the new score
+ _score_recursor($score - $shot, $shot_stack);
+ } else{
+ # The subtracted score is zero-
+ # print out a copy of the current
+ # shot stack
+ print(" ", join(" ", @{$shot_stack}), "\n");
+ }
+
+ # Pop the shot stack
+ pop(@{$shot_stack});
+ }
+
+}
+
+
+