diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-26 08:56:54 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-26 08:56:54 +0100 |
| commit | 13adc821aef522bf170cc1c2aa92fefcb7aefe64 (patch) | |
| tree | 01f1ccd991e82dbaa8c9c1e314adc4981607b260 | |
| parent | 269c344a944224bbcf9526f1b3f3ce37691300f4 (diff) | |
| parent | 02ba57763e7a9c3419f053e7ebc4ba5f3a940565 (diff) | |
| download | perlweeklychallenge-club-13adc821aef522bf170cc1c2aa92fefcb7aefe64.tar.gz perlweeklychallenge-club-13adc821aef522bf170cc1c2aa92fefcb7aefe64.tar.bz2 perlweeklychallenge-club-13adc821aef522bf170cc1c2aa92fefcb7aefe64.zip | |
Merge pull request #4600 from jaredor/master
Belated solutions to Challenge 122
| -rwxr-xr-x | challenge-122/jaredor/perl/ch-1.pl | 160 | ||||
| -rwxr-xr-x | challenge-122/jaredor/perl/ch-2.pl | 206 |
2 files changed, 366 insertions, 0 deletions
diff --git a/challenge-122/jaredor/perl/ch-1.pl b/challenge-122/jaredor/perl/ch-1.pl new file mode 100755 index 0000000000..e9834c4d0c --- /dev/null +++ b/challenge-122/jaredor/perl/ch-1.pl @@ -0,0 +1,160 @@ +#!/usr/bin/env perl + +# TWC 122, TASK #1 : Average of Stream + +use v5.012; +use strict; +use warnings; +use Getopt::Long; +use Pod::Usage; + +# For this challenge +use List::Util qw(all); # To check all the input args +use Scalar::Util qw(looks_like_number); + +# use Data::Dump qw(pp); + +# Validate Input + +Getopt::Long::Configure( 'bundling_values', 'ignorecase_always', + 'pass_through' ); + +GetOptions( + 'help|h!' => \( my $help ), + 'task|t!' => \( my $task ), + 'test' => \( my $test ) +); + +pod2usage(1) if $help; +pod2usage( -exitval => 0, -verbose => 2 ) if $task; + +$test ? test() : run(@ARGV); + +exit; # End of main script. + +sub run { + + # Bundle up fatal input errors to report them all at once. + + my @errors; + + push @errors, + "This script requires at least one number as an argument." + unless @_; + + push @errors, + "All arguments to this script must be numbers." + unless all { looks_like_number($_) } @_; + + + pod2usage( join "\n", map { "ERROR: " . $_ } @errors ) if @errors; + + # Get the solution. + + output_results( main_algo(@_) ); +} + +exit; # End of main script. + +# The main algorithm. + +sub main_algo { + + my @avgs; + my ($count, $total) = (0, 0.0); + for my $num (@_) { + $count += 1; + $total += $num; + push @avgs, $total / $count; + } + return @avgs; + +} + +# Report to STDOUT from user command line input. + +sub output_results { + + say "Output: " . join(', ', @_); + +} + +# Built in test for the algorithm function. + +sub test { + + use Test::More; + my (@input, @output); + + @input = (10, 20, 30, 40, 50, 60, 70, 80, 90, ); + @output = (10, 15, 20, 25, 30, 35, 40, 45, 50, ); + is_deeply( [ main_algo( @input ) ], \@output, "Test description" ); + + done_testing(); +} + +__END__ + +=head1 NAME + +TWC 122, TASK #1 : Average of Stream + +=head1 SYNOPSIS + + ch-1.pl [options] number [number ...] + + Description: Print a running average of a list of numbers. + + Options: + --help Brief help + --task Full description + --test Run embedded test + + Arguments: + A non-empty list of numbers. + +=head1 OPTIONS + +=over 8 + +=item B<--help> + +Brief help message. + +=item B<--task> + +Complete description of task and the script's attempt to satisfy it. + +=item B<--test> + +Run the embedded test suite for this script. + +=back + +=head1 DESCRIPTION + +B<L<The Weekly Challenge, TASK #1 E<gt> Average of Stream|https://theweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK1>> + +I<Submitted by: Mohammad S Anwar> + +You are given a stream of numbers, @N. + +Write a script to print the average of the stream at every point. + +=head2 Example + + Input: @N = (10, 20, 30, 40, 50, 60, 70, 80, 90, ...) + Output: 10, 15, 20, 25, 30, 35, 40, 45, 50, ... + + Average of first number is 10. + Average of first 2 numbers (10+20)/2 = 15 + Average of first 3 numbers (10+20+30)/3 = 20 + Average of first 4 numbers (10+20+30+40)/4 = 25 and so on. + +=head1 INTERPRETATION + +Despite the "Average of first (<count>) number(s)..." statements being in the Example section, I'm going to take them as explanatory of the output and not part of the output. + +Also, for input I'm just going to take in whatever numbers are given on the command line, whitespace delimited. + +=cut diff --git a/challenge-122/jaredor/perl/ch-2.pl b/challenge-122/jaredor/perl/ch-2.pl new file mode 100755 index 0000000000..f2f6c8023a --- /dev/null +++ b/challenge-122/jaredor/perl/ch-2.pl @@ -0,0 +1,206 @@ +#!/usr/bin/env perl + +# TWC 122, TASK #2 : Basketball Points + +use v5.012; +use strict; +use warnings; +use Getopt::Long; +use Pod::Usage; + +# For this challenge + +use Data::Dump qw(pp); + +# Validate Input + +Getopt::Long::Configure( 'bundling_values', 'ignorecase_always', + 'pass_through' ); + +GetOptions( + 'help|h!' => \( my $help ), + 'task|t!' => \( my $task ), + 'test' => \( my $test ) +); + +pod2usage(1) if $help; +pod2usage( -exitval => 0, -verbose => 2 ) if $task; + +$test ? test() : run(@ARGV); + +exit; # End of main script. + +sub run { + + # Bundle up fatal input errors to report them all at once. + + my @errors; + + push @errors, + "This script requires exactly one positive integer as an argument." + unless 1 == @_ and $_[0] =~ /\d+/ and $_[0] > 0; + + + pod2usage( join "\n", map { "ERROR: " . $_ } @errors ) if @errors; + + # Get the solution. + + output_results( main_algo(@_) ); +} + +exit; # End of main script. + +# The main algorithm. + +sub main_algo { + + my @bb_points = (1, 2, 3); + + my $target = $_[0]; + my @scores = (); + if ($target > 0) { + for my $bb_pnt (@bb_points) { + if ($bb_pnt < $target) { + for my $partial (@{&main_algo($target - $bb_pnt)}) { + push @scores, [ $bb_pnt, @{$partial} ]; + } + } elsif ($bb_pnt == $target) { + push @scores, [ $bb_pnt, ]; + } + # Could use an "else last" here if @bb_points is assumed to + # always be ascending sort and you want to save a cycle or two. + } + } + return \@scores; +} + +# Report to STDOUT from user command line input. + +sub output_results { + + my $leader = "Output: "; + for my $scores (@{$_[0]}) { + say $leader . join(' ', @{$scores}); + $leader = " "; + } + +} + +# Built in test for the algorithm function. + +sub test { + + use Test::More; + my ($input, $output); + + $input = 4; + $output = [ [1, 1, 1, 1], + [1, 1, 2], + [1, 2, 1], + [1, 3], + [2, 1, 1], + [2, 2], + [3, 1], ]; + is_deeply( main_algo( $input ), $output, "Ways to score 4 in basketball." ); + + $input = 5; + $output = [ [1, 1, 1, 1, 1], + [1, 1, 1, 2], + [1, 1, 2, 1], + [1, 1, 3], + [1, 2, 1, 1], + [1, 2, 2], + [1, 3, 1], + [2, 1, 1, 1], + [2, 1, 2], + [2, 2, 1], + [2, 3], + [3, 1, 1], + [3, 2], + ]; + is_deeply( main_algo( $input ), $output, "Ways to score 5 in basketball." ); + + done_testing(); +} + +__END__ + +=head1 NAME + +TWC 122, TASK #2 : Basketball Points + +=head1 SYNOPSIS + + ch-1.pl [options] <score> + + Description: Produce every combination of basketball goals to achieve input score. + + Options: + --help Brief help + --task Full description + --test Run embedded test + + Arguments: + A positive integer to represent the basketball score. + +=head1 OPTIONS + +=over 8 + +=item B<--help> + +Brief help message. + +=item B<--task> + +Complete description of task and the script's attempt to satisfy it. + +=item B<--test> + +Run the embedded test suite for this script. + +=back + +=head1 DESCRIPTION + +B<L<The Weekly Challenge, TASK #1 E<gt> Basketball Points|https://theweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK2>> + +I<Submitted by: Mohammad S Anwar> + + You are given a score $S. + + You can win basketball points e.g. 1 point, 2 points and 3 points. + + Write a script to find out the different ways you can score $S. + +=head2 Example + +Input: $S = 4 +Output: 1 1 1 1 + 1 1 2 + 1 2 1 + 1 3 + 2 1 1 + 2 2 + 3 1 + +Input: $S = 5 +Output: 1 1 1 1 1 + 1 1 1 2 + 1 1 2 1 + 1 1 3 + 1 2 1 1 + 1 2 2 + 1 3 1 + 2 1 1 1 + 2 1 2 + 2 2 1 + 2 3 + 3 1 1 + 3 2 + +=head1 INTERPRETATION + +Forbid non-positive integer scores. + +=cut |
