diff options
| -rw-r--r-- | challenge-245/dave-jacoby/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-245/dave-jacoby/perl/ch-2.pl | 38 |
2 files changed, 75 insertions, 0 deletions
diff --git a/challenge-245/dave-jacoby/perl/ch-1.pl b/challenge-245/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..956f70f4ca --- /dev/null +++ b/challenge-245/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + { + lang => [ 'perl', 'c', 'python' ], + popularity => [ 2, 1, 3 ], + }, + { + lang => [ 'c++', 'haskell', 'java' ], + popularity => [ 1, 3, 2 ], + }, +); + +for my $e (@examples) { + my @output = sort_language($e); + my $output = join ', ', map { qq{'$_'} } @output; + my $lang = join ', ', map { qq{'$_'} } $e->{lang}->@*; + my $popularity = join ', ', $e->{popularity}->@*; + + say <<~"END"; + Input: \@lang = ($lang) + \@popularity = ($popularity) + Output: ($output) + END +} + +sub sort_language ($input) { + return map { $_->[1] } + sort { $a->[0] <=> $b->[0] } + map { [ $input->{popularity}->[$_], $input->{lang}->[$_] ] } + 0 .. -1 + scalar $input->{lang}->@*; +} diff --git a/challenge-245/dave-jacoby/perl/ch-2.pl b/challenge-245/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..faa02d0afe --- /dev/null +++ b/challenge-245/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,38 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use Algorithm::Combinatorics qw{ variations }; + +my @examples = ( + + [ 8, 1, 9 ], + [ 8, 6, 7, 1, 0 ], + [1], + [ 9, 0, 1, 2, 5 ] +); +for my $e (@examples) { + my $input = join ', ', $e->@*; + my $output = largest_of_three( $e->@* ); + + say <<~"END"; + Input: \$input = ($input) + Output: $output + END +} + +sub largest_of_three (@input) { + my @output; + my $o = -1; + for my $c ( reverse 1 .. scalar @input ) { + my $iter = variations( \@input, $c ); + while ( my $p = $iter->next ) { + my $combo = 0 + join '', $p->@*; + next if $combo % 3 != 0; + $o = $combo if $combo > $o; + } + } + return $o; +} |
