diff options
| author | Michael Hamlin <1197072+myrrhlin@users.noreply.github.com> | 2019-07-14 23:02:35 -0400 |
|---|---|---|
| committer | Michael Hamlin <1197072+myrrhlin@users.noreply.github.com> | 2019-07-14 23:02:35 -0400 |
| commit | b690a2d14d660bdc88bc1e9fde871ddbbbf71f4b (patch) | |
| tree | c56a2c23ff536f41f783ec01573ac74369512578 /challenge-016/michael-hamlin | |
| parent | 912732e159464fc541dd1b48b50d13221b08b867 (diff) | |
| download | perlweeklychallenge-club-b690a2d14d660bdc88bc1e9fde871ddbbbf71f4b.tar.gz perlweeklychallenge-club-b690a2d14d660bdc88bc1e9fde871ddbbbf71f4b.tar.bz2 perlweeklychallenge-club-b690a2d14d660bdc88bc1e9fde871ddbbbf71f4b.zip | |
replace prior PR
my previous PR had some changes from the original code
you'd seen, mohammad. you tossed that PR because you thought it
was the same. this PR merges those changes back in.
Diffstat (limited to 'challenge-016/michael-hamlin')
| -rw-r--r-- | challenge-016/michael-hamlin/perl5/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-016/michael-hamlin/perl5/task1.txt | 6 |
2 files changed, 39 insertions, 0 deletions
diff --git a/challenge-016/michael-hamlin/perl5/ch-1.pl b/challenge-016/michael-hamlin/perl5/ch-1.pl new file mode 100644 index 0000000000..f172617f68 --- /dev/null +++ b/challenge-016/michael-hamlin/perl5/ch-1.pl @@ -0,0 +1,33 @@ +#! /usr/bin/env perl +# +use 5.18.0; +use List::Util qw<sum>; +use Getopt::Std; + +my %opt; +getopts('v', \%opt); + +# each piece is a percent. the sum must equal 100. +my @pieces; +my $biggest; + +# given the conditions, and the nature of the problem, +# we can surmise the function must have a single maximum. +# with this knowledge, we can skip calculating once we +# pass the largest value. + +my $remaining = 100; +for my $p (1..100) { + my $this = $remaining * $p/100; + printf "piece %3u: %.2f\n", $p, $this if $opt{v}; + push @pieces, $this; + $remaining -= $this; + # once we passed the peak, we can stop + last if $biggest && $this < $pieces[$biggest - 1]; + # otherwise we just found a new maximum: + $biggest = $p; +} + +printf "biggest was #%2u, at %.2f of the pie\n", $biggest, $pieces[$biggest - 1]; +# say 'checking our math, sum is ', List::Util::sum @pieces if $opt{v}; + diff --git a/challenge-016/michael-hamlin/perl5/task1.txt b/challenge-016/michael-hamlin/perl5/task1.txt new file mode 100644 index 0000000000..d2b904c7ed --- /dev/null +++ b/challenge-016/michael-hamlin/perl5/task1.txt @@ -0,0 +1,6 @@ + + Pythagoras Pie Puzzle, proposed by Jo Christian Oterhals. + + At a party a pie is to be shared by 100 guest. The first guest gets 1% of the pie, the second guest gets 2% of the remaining pie, the third gets 3% of the remaining pie, the fourth gets 4% and so on. + + Write a script that figures out which guest gets the largest piece of pie. |
