diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-20 13:41:53 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-20 13:41:53 +0100 |
| commit | e4459f900ced91c5f0c91433e3a4094bb93debec (patch) | |
| tree | f2afd96e4e3057e895f751d9cab09c9d45439116 | |
| parent | 0127d62163ae5054e1c42c640936dec7483b1d24 (diff) | |
| parent | 7a1a1f0d8a60eb693c0fcabfe6152e6a1eec056b (diff) | |
| download | perlweeklychallenge-club-e4459f900ced91c5f0c91433e3a4094bb93debec.tar.gz perlweeklychallenge-club-e4459f900ced91c5f0c91433e3a4094bb93debec.tar.bz2 perlweeklychallenge-club-e4459f900ced91c5f0c91433e3a4094bb93debec.zip | |
Merge pull request #4567 from PerlBoy1967/branch-for-challenge-122
Task 1 & 2
| -rwxr-xr-x | challenge-122/perlboy1967/perl/ch-1.pl | 53 | ||||
| -rwxr-xr-x | challenge-122/perlboy1967/perl/ch-2.pl | 66 |
2 files changed, 119 insertions, 0 deletions
diff --git a/challenge-122/perlboy1967/perl/ch-1.pl b/challenge-122/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..980cff0a82 --- /dev/null +++ b/challenge-122/perlboy1967/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/perl + +# Perl Weekly Challenge - 122 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK1 +# +# Task 1 - Average of Stream +# +# Author: Niels 'PerlBoy' van Dijke + +use v5.16; +use strict; +use warnings; + +use Test::More; + +my $tests = [ + [ [10,10],[20,15],[30,20],[40,25] ], + [ [1,1],[3,2],[5,3],[7,4] ], +]; + +my @as = map {new averageStream} (1 .. scalar @$tests); + +for (my $i = 0; $i < scalar @$tests; $i++) { + foreach my $t1 (@{$tests->[$i]}) { + printf "%d %s\n", $i, join(',', @$t1) unless is($as[$i]->add($t1->[0]),$t1->[1]); + } +} + +done_testing(); + + +package averageStream; + +sub new { + my %this = (sum => 0, count => 0, average => undef); + + bless \%this; +} + +sub add { + my ($this,$n) = @_; + + $this->{sum} += $n; + $this->{average} = $this->{sum} / ++$this->{count}; + + $this->{average}; +} + +sub average { + $_->{average} // die "No points added yet!"; +} + +1; diff --git a/challenge-122/perlboy1967/perl/ch-2.pl b/challenge-122/perlboy1967/perl/ch-2.pl new file mode 100755 index 0000000000..a48862b4c9 --- /dev/null +++ b/challenge-122/perlboy1967/perl/ch-2.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl + +# Perl Weekly Challenge - 122 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-122/#TASK2 +# +# Task 1 - Basketball Points +# +# Author: Niels 'PerlBoy' van Dijke + +use v5.16; +use strict; +use warnings; + +use List::Util qw(sum); + +use Test::More; +use Test::Deep qw(cmp_deeply); + +# Prototype(s) +sub basketballPoints($); +sub _bp($$$); + + +my $tests = [ + [ 3, [1,1,1],[1,2],[2,1],[3] ], + [ 4, [1,1,1,1],[1,1,2],[1,2,1],[1,3],[2,1,1],[2,2],[3,1] ], + [ 5, [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] ], +]; + +foreach my $t (@$tests) { + my $n = shift(@$t); + printf "failed: %d\n", $n unless cmp_deeply(basketballPoints($n),$t); +} + +done_testing(); + + +sub basketballPoints($) { + my ($n) = @_; + + if ($n == 1) { + return [[1]]; + } elsif ($n == 2) { + return [[1,1],[2]]; + } + + my $res = []; + return _bp($n,$res,[]); +} + +sub _bp ($$$) { + my ($n,$arRes,$arTmp) = @_; + + for my $i (1 .. ($n > 3 ? 3 : $n)) { + push(@$arTmp,$i); + my $s = sum(@$arTmp); + if ($s == $n) { + push(@$arRes,[@$arTmp]); + } elsif ($s < $n) { + _bp($n,$arRes,$arTmp); + } + pop(@$arTmp); + } + + return $arRes; +} |
