diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2021-07-19 15:26:59 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2021-07-19 15:26:59 +0000 |
| commit | 7a1a1f0d8a60eb693c0fcabfe6152e6a1eec056b (patch) | |
| tree | c9cd7a90ff380bbcbda1dde39b6cb25e5e2fce3e | |
| parent | 675c4ed9a3b441729b9558c051638027242ba77a (diff) | |
| download | perlweeklychallenge-club-7a1a1f0d8a60eb693c0fcabfe6152e6a1eec056b.tar.gz perlweeklychallenge-club-7a1a1f0d8a60eb693c0fcabfe6152e6a1eec056b.tar.bz2 perlweeklychallenge-club-7a1a1f0d8a60eb693c0fcabfe6152e6a1eec056b.zip | |
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; +} |
