From c5a1407142c7fc1129db48fc9003ff1e8947e678 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 23 Jan 2023 10:30:07 -0500 Subject: #201 DAJ --- challenge-201/dave-jacoby/perl/ch-1.pl | 33 ++++++++++++++++++++++++++++ challenge-201/dave-jacoby/perl/ch-2.pl | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 challenge-201/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-201/dave-jacoby/perl/ch-2.pl 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; +} + -- cgit