aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCY Fung <fungcheokyin@gmail.com>2023-02-27 03:57:28 +0800
committerCY Fung <fungcheokyin@gmail.com>2023-02-27 03:57:28 +0800
commiteb4ebb60d76619dd7389a4e66a8a961cf807eb20 (patch)
tree3e1823773a329389611c1e2cd9a9729a697e82c4
parent8bc4d47406b514b6767f84cd6973870938e93e26 (diff)
downloadperlweeklychallenge-club-eb4ebb60d76619dd7389a4e66a8a961cf807eb20.tar.gz
perlweeklychallenge-club-eb4ebb60d76619dd7389a4e66a8a961cf807eb20.tar.bz2
perlweeklychallenge-club-eb4ebb60d76619dd7389a4e66a8a961cf807eb20.zip
Week 205
-rw-r--r--challenge-205/cheok-yin-fung/perl/ch-1.pl29
-rw-r--r--challenge-205/cheok-yin-fung/perl/ch-2.pl21
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-205/cheok-yin-fung/perl/ch-1.pl b/challenge-205/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..5fb7319342
--- /dev/null
+++ b/challenge-205/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,29 @@
+# The Weekly Challenge 205
+# Task 1 Third Highest
+use v5.30.0;
+use warnings;
+use List::Util qw/max uniq/;
+
+sub th {
+ my @arr = @_;
+ @arr = uniq @arr;
+ my $max = $arr[0];
+ my $scmax = $arr[1];
+ return $max if !defined($scmax);
+ my $thmax = $arr[2];
+ ($max, $scmax) = ($scmax, $max) if $scmax > $max;
+ return $max if !defined($thmax);
+ for my $i (2..$#arr) {
+ $thmax = $arr[$i] if $arr[$i] > $thmax;
+ ($thmax, $scmax) = ($scmax, $thmax) if $thmax > $scmax;
+ ($max, $scmax) = ($scmax, $max) if $scmax > $max;
+ }
+ return $thmax;
+}
+
+use Test::More tests=>5;
+ok th(5,3,4) == 3;
+ok th(5,6) == 6;
+ok th(5,4,4,3) == 3;
+ok th(1,1,1,1) == 1;
+ok th(1,2,3,4) == 2;
diff --git a/challenge-205/cheok-yin-fung/perl/ch-2.pl b/challenge-205/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..1f6802ae84
--- /dev/null
+++ b/challenge-205/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,21 @@
+# The Weekly Challenge 205
+# Task 2 Maximum XOR
+use v5.30.0;
+use warnings;
+use List::Util qw/max/;
+
+sub mx {
+ my @arr = @_;
+ my @xor;
+ for my $i (0..$#arr) {
+ for my $j ($i+1..$#arr) {
+ push @xor, $arr[$i] ^ $arr[$j]
+ }
+ }
+ return max @xor;
+}
+
+use Test::More tests=>3;
+ok mx(1,2,3,4,5,6,7) == 7;
+ok mx(2,4,1,3) == 7;
+ok mx(10,5,7,12,8) == 15;