aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2023-12-04 01:33:30 +0800
committerCY Fung <fungcheokyin@gmail.com>2023-12-04 01:33:30 +0800
commit6c0b2d38e32e31d78daed444555fbc80231a6171 (patch)
tree7b9c708b9ff84ac63ab22b3ef40bba36430d7ca9
parentc301e328c95a9956c014c043776e8e81d0c20b47 (diff)
downloadperlweeklychallenge-club-6c0b2d38e32e31d78daed444555fbc80231a6171.tar.gz
perlweeklychallenge-club-6c0b2d38e32e31d78daed444555fbc80231a6171.tar.bz2
perlweeklychallenge-club-6c0b2d38e32e31d78daed444555fbc80231a6171.zip
Week 245
-rw-r--r--challenge-245/cheok-yin-fung/perl/ch-1.pl16
-rw-r--r--challenge-245/cheok-yin-fung/perl/ch-2.pl48
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-245/cheok-yin-fung/perl/ch-1.pl b/challenge-245/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..a1dd2c01ce
--- /dev/null
+++ b/challenge-245/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,16 @@
+# The Weekly Challenge 245
+# Task 1 Sort Language
+use v5.30.0;
+use warnings;
+
+sub sl {
+ my @lang = $_[0]->@*;
+ my @pop = $_[1]->@*;
+ my @nlang = map { $lang[$_] } sort { $pop[$a] <=> $pop[$b] } 0..$#lang;
+ return [@nlang];
+}
+
+use Test::More tests=>2;
+use Test::Deep;
+cmp_deeply sl(['perl','c','python'],[2,1,3]), ['c','perl','python'];
+cmp_deeply sl(['c++','haskell','java'],[1,3,2]), ['c++','java','haskell'];
diff --git a/challenge-245/cheok-yin-fung/perl/ch-2.pl b/challenge-245/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..a5d1b511cf
--- /dev/null
+++ b/challenge-245/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,48 @@
+# The Weekly Challenge 245
+# Task 2 Largest of Three
+use v5.30.0;
+use warnings;
+use List::Util qw/sum min/;
+
+sub lot {
+ my @ints = @_;
+ my $idr = sum(map {$_ % 3} @ints) % 3;
+ if ($idr != 0) {
+ my $unwanted2 = -12;
+ my $unwanted = min grep {$idr == $_ % 3} @ints;
+ my $unwill = 1;
+ if (!$unwanted) {
+ my @unwants = sort {$a<=>$b} grep {($idr+1) % 3 == $_ % 3} @ints;
+ $unwanted = $unwants[0];
+ $unwanted2 = $unwants[1];
+ return -1 if !$unwanted2;
+ }
+ my @nints;
+ for my $z (@ints) {
+ if ($z == $unwanted || $z == $unwanted2) {
+ $unwill--;
+ $unwanted = -11 if $z == $unwanted;
+ $unwanted = -12 if $z == $unwanted2;
+ }
+ else {
+ push @nints, $z;
+ }
+ }
+ return -1 if scalar @nints == 0;
+ return join "", sort {$b<=>$a} @nints;
+ }
+ else {
+ return -1 if scalar @ints == 0;
+ return join "", sort {$b<=>$a} @ints;
+ }
+}
+
+
+use Test::More tests=>7;
+ok lot(8,1,9) == 981;
+ok lot(8,6,7,1,0) == 8760;
+ok lot(1) == -1;
+ok lot(1,2) == 21;
+ok lot(1,2,2) == 21;
+ok lot(2,2) == -1;
+ok lot(2,2,3,6,9) == 963;