diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-23 10:30:07 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-01-23 10:30:07 -0500 |
| commit | c5a1407142c7fc1129db48fc9003ff1e8947e678 (patch) | |
| tree | 5ff374db7228102e7857b89364fb7e5e70fd995a | |
| parent | 27b88f614b9bb53872ef0da19a56087505836db0 (diff) | |
| download | perlweeklychallenge-club-c5a1407142c7fc1129db48fc9003ff1e8947e678.tar.gz perlweeklychallenge-club-c5a1407142c7fc1129db48fc9003ff1e8947e678.tar.bz2 perlweeklychallenge-club-c5a1407142c7fc1129db48fc9003ff1e8947e678.zip | |
#201 DAJ
| -rw-r--r-- | challenge-201/dave-jacoby/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-201/dave-jacoby/perl/ch-2.pl | 39 |
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-201/dave-jacoby/perl/ch-1.pl b/challenge-201/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..6e23d10049 --- /dev/null +++ b/challenge-201/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ 0, 1, 3 ], + [ 0, 1 ], + [ 1, 3, 5, 7, 9 ] + +); + +for my $e (@examples) { + my $list = join ',', $e->@*; + my @out = missing_numbers( $e->@* ); + my $out = join ',', @out; + say <<"END"; + Input: \@array = ($list) + Output: $out +END +} + +sub missing_numbers ( @array ) { + my $n = scalar @array; + my %h = map { $_ => 1 } @array; + my @output; + for my $v ( 0 .. $n ) { + push @output, $v unless $h{$v}; + } + return @output; +} diff --git a/challenge-201/dave-jacoby/perl/ch-2.pl b/challenge-201/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..70f8a0da3f --- /dev/null +++ b/challenge-201/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 uniq }; + +my @examples = ( 1, 3, 5, 7 ); + +for my $e (@examples) { + my @piles = penny_piles($e); + @piles = sort { length $b <=> length $a } + uniq sort map { join ',', $_->@* } @piles; + my $count = scalar @piles; + my $list = join "\n\t", '', @piles; + say <<"END"; + Input: \$n = $e + Output: $count + $list +END +} + +sub penny_piles ( $n, @array ) { + my $sum = sum0 @array; + my @copy = sort { $a <=> $b } @array; + if ( $sum > $n ) { return } + if ( $sum == $n ) { + return \@copy; + } + + my @output; + for my $i ( reverse 1 .. $n ) { + my @local = sort { $a <=> $b } $n, @array; + push @output, penny_piles( $n, @array, $i ); + } + return @output; +} + |
