From 469c31ffc11e0be5e1cdfa4b100366dfd5bb4cbf Mon Sep 17 00:00:00 2001 From: Joelle Maslak Date: Sun, 7 Jul 2019 20:35:17 -0600 Subject: Solution to 16.1 in P6 & P5 --- challenge-016/joelle-maslak/perl5/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-016/joelle-maslak/perl6/ch-1.p6 | 17 +++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100755 challenge-016/joelle-maslak/perl5/ch-1.pl create mode 100755 challenge-016/joelle-maslak/perl6/ch-1.p6 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] } ) } + -- cgit