diff options
| author | Joelle Maslak <jmaslak@antelope.net> | 2019-07-07 20:35:17 -0600 |
|---|---|---|
| committer | Joelle Maslak <jmaslak@antelope.net> | 2019-07-07 20:35:17 -0600 |
| commit | 469c31ffc11e0be5e1cdfa4b100366dfd5bb4cbf (patch) | |
| tree | 680bf3538f07356abb82ea9f2b654bf9bd06f01c | |
| parent | 40a931fcd3b997fdb090752c8560976e3be37bb2 (diff) | |
| download | perlweeklychallenge-club-469c31ffc11e0be5e1cdfa4b100366dfd5bb4cbf.tar.gz perlweeklychallenge-club-469c31ffc11e0be5e1cdfa4b100366dfd5bb4cbf.tar.bz2 perlweeklychallenge-club-469c31ffc11e0be5e1cdfa4b100366dfd5bb4cbf.zip | |
Solution to 16.1 in P6 & P5
| -rwxr-xr-x | challenge-016/joelle-maslak/perl5/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-016/joelle-maslak/perl6/ch-1.p6 | 17 |
2 files changed, 45 insertions, 0 deletions
diff --git a/challenge-016/joelle-maslak/perl5/ch-1.pl b/challenge-016/joelle-maslak/perl5/ch-1.pl new file mode 100755 index 0000000000..e113b715e2 --- /dev/null +++ b/challenge-016/joelle-maslak/perl5/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl +use v5.22; +use strict; +use warnings; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use autodie; + +use bignum; +use List::Util qw(max sum); + +my @slices; +for my $i (1..100) { + push @slices, [ $i, (1.0 - (sum(seconds(@slices)) // 0)) * $i / 100.0 ]; +} + +print "Largest piece goes to guest # "; +my $max = max seconds(@slices); +say join " ", firsts(grep { $_->[1] == $max } @slices); + +printf("Slice size: %.2f%% of the pie\n", $max * 100.0); + +sub firsts(@array) { return map { $_->[0] } @array } +sub seconds(@array) { return map { $_->[1] } @array } + diff --git a/challenge-016/joelle-maslak/perl6/ch-1.p6 b/challenge-016/joelle-maslak/perl6/ch-1.p6 new file mode 100755 index 0000000000..02acc8f100 --- /dev/null +++ b/challenge-016/joelle-maslak/perl6/ch-1.p6 @@ -0,0 +1,17 @@ +#!/usr/bin/env perl6 +use v6; + +my @slices; +for 1..100 -> $i { + @slices.push: @( $i, ( FatRat(1) - seconds(@slices).sum ) * $i / 100 ); +} + +print "Largest piece goes to guest # "; +my $max = seconds(@slices).max; +say firsts( @slices.grep( { $_[1] == $max } ) ).join(" "); + +say "Slice size: { ($max * 100).fmt('%.2f') }% of the pie"; + +sub firsts(@array) { return @array.map( { $_[0] } ) } +sub seconds(@array) { return @array.map( { $_[1] } ) } + |
