From eb4ebb60d76619dd7389a4e66a8a961cf807eb20 Mon Sep 17 00:00:00 2001 From: CY Fung Date: Mon, 27 Feb 2023 03:57:28 +0800 Subject: Week 205 --- challenge-205/cheok-yin-fung/perl/ch-1.pl | 29 +++++++++++++++++++++++++++++ challenge-205/cheok-yin-fung/perl/ch-2.pl | 21 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-205/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-205/cheok-yin-fung/perl/ch-2.pl 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; -- cgit