diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-28 12:25:51 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2020-08-28 12:25:51 +0100 |
| commit | 15ac63c0f3554fc95fbeb05fa7504a205ab76142 (patch) | |
| tree | 4926baeb6d4e95e9ceceb13734246154dd570965 /challenge-075 | |
| parent | 8af7f38eab75da89d982ec77f31566b568a32b70 (diff) | |
| download | perlweeklychallenge-club-15ac63c0f3554fc95fbeb05fa7504a205ab76142.tar.gz perlweeklychallenge-club-15ac63c0f3554fc95fbeb05fa7504a205ab76142.tar.bz2 perlweeklychallenge-club-15ac63c0f3554fc95fbeb05fa7504a205ab76142.zip | |
- Added solutions by Pete Houston.
Diffstat (limited to 'challenge-075')
| -rw-r--r-- | challenge-075/pete-houston/perl/ch-1.pl | 41 | ||||
| -rw-r--r-- | challenge-075/pete-houston/perl/ch-2.pl | 61 |
2 files changed, 102 insertions, 0 deletions
diff --git a/challenge-075/pete-houston/perl/ch-1.pl b/challenge-075/pete-houston/perl/ch-1.pl new file mode 100644 index 0000000000..dc2b63e0dc --- /dev/null +++ b/challenge-075/pete-houston/perl/ch-1.pl @@ -0,0 +1,41 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 7501.pl +# +# USAGE: ./7501.pl TARGET COIN [ ... ] +# +# DESCRIPTION: Report how many ways to make a total from a coin set +# +# REQUIREMENTS: Algorithm::Knapsack, Lingua::EN::Inflexion +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 24/08/20 +#=============================================================================== + +use strict; +use warnings; +use Algorithm::Knapsack; +use Lingua::EN::Inflexion; + +# Construct a big enough set of coins +my $target = shift; +my @coins = map {($_) x ($target / $_)} @ARGV; + +# Process +my $sack = Algorithm::Knapsack->new ( + capacity => $target, + weights => \@coins, +); +$sack->compute; + +# De-duplicate +my %sets; +$sets {join (',', @coins[@$_]) . "\n"} = 1 for $sack->solutions; + +# Output +my $ways = keys %sets; +print inflect ("There <#d:$ways><V:is> <#nw:$ways> " . + "<N:way> of paying $target:\n\n"), + sort keys %sets; diff --git a/challenge-075/pete-houston/perl/ch-2.pl b/challenge-075/pete-houston/perl/ch-2.pl new file mode 100644 index 0000000000..4325b146f5 --- /dev/null +++ b/challenge-075/pete-houston/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +#=============================================================================== +# +# FILE: 7502.pl +# +# USAGE: ./7502.pl N ... +# +# DESCRIPTION: Report the area of the biggest rectangle in the historgram. +# +# REQUIREMENTS: List::Util, List::MoreUtils +# NOTES: Each N must be a natural number. +# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk +# ORGANIZATION: Openstrike +# VERSION: 1.0 +# CREATED: 24/08/20 +#=============================================================================== + +use strict; +use warnings; +use List::Util 'max'; +use List::MoreUtils 'uniq'; + +histarea (@ARGV); + +sub histarea { + my @in = @_; + my $size = scalar @in; + my $area = $size; + my $axb = "1 x $size"; + my @steps = sort { $a <=> $b } uniq @in; + for my $height (@steps) { + my $contig = 0; + my $max = 0; + for my $col (@in) { + if ($col >= $height) { + $contig++; + $max = max $max, $contig; + } else { + $contig = 0; + } + } + my $rect = $max * $height; + if ($rect > $area) { + $area = max $area, $rect; + $axb = "$max x $height"; + } + } + print "Largest rectangle is $axb with area $area\n"; + &print_histo; +} + +sub print_histo { + my @y = @_; + my $yval = max (@y); + while ($yval) { + printf "\%6i\%s\n", + $yval, join '', map { $_ >= $yval ? ' #' : ' ' } @y; + $yval--; + } + print ' ' x 4, ' _' x (1 + @y), "\n @y\n"; +} |
