diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-29 11:24:37 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-29 11:24:37 +0000 |
| commit | 71d0df1c25084d211592d7e4ca9eb73267023a58 (patch) | |
| tree | 48299b895bd77c5f93fb60aa6692034e8c2a7a79 /challenge-201 | |
| parent | 31d1c584ae3cca11a01ec2b643766ba052765da2 (diff) | |
| parent | b0a72f0e06997be68a7cd9413885e99c21c4ab8e (diff) | |
| download | perlweeklychallenge-club-71d0df1c25084d211592d7e4ca9eb73267023a58.tar.gz perlweeklychallenge-club-71d0df1c25084d211592d7e4ca9eb73267023a58.tar.bz2 perlweeklychallenge-club-71d0df1c25084d211592d7e4ca9eb73267023a58.zip | |
Merge pull request #7477 from pokgopun/pwc201
pwc201/perl
Diffstat (limited to 'challenge-201')
| -rw-r--r-- | challenge-201/pokgopun/perl/ch-1.pl | 11 | ||||
| -rw-r--r-- | challenge-201/pokgopun/perl/ch-2.pl | 23 |
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-201/pokgopun/perl/ch-1.pl b/challenge-201/pokgopun/perl/ch-1.pl new file mode 100644 index 0000000000..aa58f14b86 --- /dev/null +++ b/challenge-201/pokgopun/perl/ch-1.pl @@ -0,0 +1,11 @@ +use strict; +use warnings; + +@ARGV = (0,1,3) unless join("",@ARGV) =~ /^\d+$/; ### default array to example1 if not provided via arguments + +my %seen; +foreach (@ARGV){ + die "not an array of unique numbers" if $seen{$_}++; +} + +printf "Input: array = %s\nOutput: %s\n", join(", ", sort{$a<=>$b} @ARGV), join(", ", grep{!$seen{$_}} 0..$#ARGV+1); diff --git a/challenge-201/pokgopun/perl/ch-2.pl b/challenge-201/pokgopun/perl/ch-2.pl new file mode 100644 index 0000000000..5fa6bfc94f --- /dev/null +++ b/challenge-201/pokgopun/perl/ch-2.pl @@ -0,0 +1,23 @@ +use strict; +use warnings; + +my $n = $ARGV[0] && $ARGV[0] =~ /^\d+$/ ? $ARGV[0] : 5; ### n default to 5 unless a number is provided as an argument +my %seen; ### hash to put "number of ways" as keys + +### function that take n to generate "number of ways" and put them as keys of %seen +sub penny{ + my $p = shift @_; + { + last if $p; + $seen{join(" ",sort {$b<=>$a} @_)}++; ### sort "number of ways" so they can be deduped + return + } + foreach (1..$p) { + penny($p-$_,$_,@_); + } +} + +penny($n); +my $p = keys %seen; +printf "Input: n = %d\nOutput: %d\n\nThere are %d ways of stacking %d pennies in ascending piles:\n\n", $n, $p, $p, $n; +printf("\t%s\n",$_) foreach sort keys %seen; |
