diff options
| author | E. Choroba <choroba@matfyz.cz> | 2023-11-27 21:43:28 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2023-11-27 21:43:28 +0100 |
| commit | 09e358d530f921d49e36bafaa2819fdd6a645962 (patch) | |
| tree | e6328092c2e8792e549517171cd7878464439658 | |
| parent | a91d49494a545d745c5c622afa3a9646bf1ac774 (diff) | |
| download | perlweeklychallenge-club-09e358d530f921d49e36bafaa2819fdd6a645962.tar.gz perlweeklychallenge-club-09e358d530f921d49e36bafaa2819fdd6a645962.tar.bz2 perlweeklychallenge-club-09e358d530f921d49e36bafaa2819fdd6a645962.zip | |
Add solutions to 245: Sort Language & Largest of Three by E. Choroba
| -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'; |
