diff options
| -rwxr-xr-x | challenge-245/e-choroba/perl/ch-1.pl | 20 | ||||
| -rwxr-xr-x | challenge-245/e-choroba/perl/ch-2.pl | 33 |
2 files changed, 53 insertions, 0 deletions
diff --git a/challenge-245/e-choroba/perl/ch-1.pl b/challenge-245/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..a2db097214 --- /dev/null +++ b/challenge-245/e-choroba/perl/ch-1.pl @@ -0,0 +1,20 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::MoreUtils qw{ nsort_by }; + +sub sort_language($lang, $popularity) { + [@$lang[ nsort_by { $popularity->[$_] } 0 .. $#$popularity ]] +} + +use Test::More tests => 2; + +is_deeply sort_language(['perl', 'c', 'python'], [2, 1, 3]), + ['c', 'perl', 'python'], + 'Example 1'; + +is_deeply sort_language(['c++', 'haskell', 'java'], [1, 3, 2]), + ['c++', 'java', 'haskell'], + 'Example 2'; diff --git a/challenge-245/e-choroba/perl/ch-2.pl b/challenge-245/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..73a789f123 --- /dev/null +++ b/challenge-245/e-choroba/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use Algorithm::Combinatorics qw{ variations }; +use List::Util qw{ sum }; + +sub largest_of_three(@ints) { + my $max = -1; + for my $length (reverse 1 .. @ints) { + for my $var (variations(\@ints, $length)) { + next unless 0 == sum(@$var) % 3; + + my $candidate = 0 + join "", @$var; + $max = $candidate if $candidate > $max; + } + } + return $max +} + +use Test::More tests => 3 + 6; + +is largest_of_three(8, 1, 9), 981, 'Example 1'; +is largest_of_three(8, 6, 7, 1, 0), 8760, 'Example 2'; +is largest_of_three(1), -1, 'Example 3'; + +is largest_of_three(0, 0, 0), 0, 'Zero'; +is largest_of_three(4, 8, 911), 9114, 'n>9'; +is largest_of_three(8, 85, 0), 8850, 'n>9'; +is largest_of_three(8, 89, 2), 8982, 'n>9'; +is largest_of_three(8, 76, 0), 8760, 'n>9'; +is largest_of_three(8, 94, 0), 9480, 'n>9'; |
