From bebfb9ab1162df5ff6025a1168dcabedbd587990 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Feb 2023 07:13:48 +0000 Subject: Create ch-1.pl --- challenge-205/james-smith/perl/ch-1.pl | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-205/james-smith/perl/ch-1.pl diff --git a/challenge-205/james-smith/perl/ch-1.pl b/challenge-205/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..9cecdc6dee --- /dev/null +++ b/challenge-205/james-smith/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); + +my @TESTS = ( + [ [5,3,4], 3, 3 ], + [ [5,6], 6, 6 ], + [ [5,4,4,3], 3, 4 ], +); + +is( third( @{$_->[0]} ), $_->[2] ) for @TESTS; +is( third_unique( @{$_->[0]} ), $_->[1] ) for @TESTS; + +done_testing(); + +sub third { + my ($i,$j,$k) = sort { $b <=> $a } splice @_,0,3; + return $i unless defined $k; + $_ > $i ? (($i,$j,$k)=($_,$i,$j)) + : $_ > $j ? (($j,$k)=($_,$j)) + : ( $_ > $k ) && ($k=$_) for @_; + $k; +} + +sub third_unique { + my ($i,$j,$k) = shift; + $_ > $i ? (($i,$j,$k)=($_,$i,$j)) + : $_ == $i ? () + : !defined $j || $_ > $j ? (($j,$k)=($_,$j)) + : defined $j && $_ == $j ? () + : ( !defined $k || $_ > $k ) && ($k=$_) for @_; + $k//$i; +} -- cgit From 047077b37bee96cd4950ba0a092aac7dba8fb373 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Feb 2023 07:14:47 +0000 Subject: Create ch-2.pl --- challenge-205/james-smith/perl/ch-2.pl | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-205/james-smith/perl/ch-2.pl diff --git a/challenge-205/james-smith/perl/ch-2.pl b/challenge-205/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..6b3174302b --- /dev/null +++ b/challenge-205/james-smith/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/local/bin/perl + +use strict; +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); + +my @TESTS = ( + [ [1..7], 7 ], + [ [2,4,1,3], 7 ], + [ [10,5,7,12,8], 15 ], +); + +is( max_xor( @{$_->[0]} ), $_->[1] ) for @TESTS; + +done_testing(); + +sub max_xor { + my $m = 0; + while( @_ ){ + my $x=shift; + ( $x^$_ ) > $m && ( $m = $x^$_ ) for @_; + } + $m; +} -- cgit From f37114b88a6ed3fc014cfe44b9a1aa5a20decfac Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Feb 2023 07:24:49 +0000 Subject: Update README.md --- challenge-205/james-smith/README.md | 100 +++++++++++++++--------------------- 1 file changed, 40 insertions(+), 60 deletions(-) diff --git a/challenge-205/james-smith/README.md b/challenge-205/james-smith/README.md index d72e1d0ee2..be97d5f11b 100644 --- a/challenge-205/james-smith/README.md +++ b/challenge-205/james-smith/README.md @@ -1,7 +1,7 @@ -[< Previous 203](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-203/james-smith) | -[Next 205 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-205/james-smith) +[< Previous 204](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-204/james-smith) | +[Next 206 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-206/james-smith) -# The Weekly Challenge 204 +# The Weekly Challenge 205 You can find more information about this weeks, and previous weeks challenges at: @@ -13,84 +13,64 @@ submit solutions in whichever language you feel comfortable with. You can find the solutions here on github at: -https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-204/james-smith +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-205/james-smith -# Task 1: Monotonic Array +# Task 1: Third Highest -***You are given an array of integers. Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0. An array is Monotonic if it is either monotone increasing or decreasing.*** +***You are given an array of integers. Write a script to find out the Third Highest if found otherwise return the maximum.*** -## Solution +***Note the examples suggest we are looking for third highest unique value - so we will solve both solutions*** -We loop through the array - firstly trying to find if the string is increasing or decreasing. +## Solution -To do so we collect ths sign of the difference between successive numbers UNTIL we find a non-zero value.. +A quick (code wise) solution would be to sort the list `@_` and take the 3rd value or first if the list has length less than 3. But for large lists this would be inefficient. There is a debate here about what the cut off value is and so a simple sort will be quicker for small arrays. -We then see if all subsequence differences have the same sign (or 0).... +This was pass 1 - which sorts irrespective of uniqueness. But fails test 3. -```perl -sub monotonic { - my($p,$d,$t)=$_[0]; - ($t=$p<=>$_)&&($d||=$t)!=$t?(return 0):($p=$_)for@_; - 1 -} -``` +We start by sorting the first three values, then we proceed to check the next values against the three current values, and insert the new value into the correct place in the list (or do nothing); ```perl -sub monotonic { - my($p,$d,$t)=($_[0],0); - ($t=$p<=>$_) ## Get next signum of the pairwise difference (and store in $t) - && ($d||=$t) != $t ## If this new difference is non-zero - ## we update the direction [IF it is previously non-zero] - ## and compare it against the value we have just computed - ? (return 0) ## If the difference is different we return 0 - : ($p=$_) ## Otherwise we update the previous value... - for @_; ## and repeat for all values. - 1; ## We have no differences! +sub third { + my ($i,$j,$k) = sort { $b <=> $a } splice @_,0,3; + return $i unless defined $k; + $_ > $i ? (($i,$j,$k)=($_,$i,$j)) + : $_ > $j ? (($j,$k)=($_,$j)) + : ( $_ > $k ) && ($k=$_) for @_; + $k; } ``` -# Task 2: Reshape Matrix - -***You are given a matrix (m x n) and two integers (r) and (c). Write a script to reshape the given matrix in form (r x c) with the original value in the given matrix. If you can’t reshape print 0.*** - -Our first pass - is simple - we first do a dimension check to see if the matrix is the right size. Then we stitch all the rows together into a single list, and chomp off line-by-line. +Now if we are looking for uniqueness - then the code becomes slighly more complex. If we have the value matching another value we do nothing. Here we can't splice off the first 3 values, instead we have to check for this equality each time. So the code becomes. +Note we could have re-ordered the checks to avoid the two *skips* when checking for equality - but then the code becomes less readable. ```perl -sub reshape { - my($m,$r,$c) = @_; - return 0 unless @{$m}*@{$m->[0]} == $r * $c; - my @t = map { @{$_} } @{$m}; - [ map { [ splice @t, 0, $c ] } 1..$r ]; +sub third_unique { + my ($i,$j,$k) = shift; + $_ > $i ? (($i,$j,$k)=($_,$i,$j)) + : $_ == $i ? () + : !defined $j || $_ > $j ? (($j,$k)=($_,$j)) + : defined $j && $_ == $j ? () + : ( !defined $k || $_ > $k ) && ($k=$_) for @_; + $k//$i; } ``` -Our second pass is a bit more complicated as we try and avoid the stitch everything together in a long line... +# Task 2: Maximum XOR - * Here we start again with the size check then we construct each row of the target array, chunk by chunk, - * We find the part of the of the first row of the source which will fit in the target row. - * If we reach the end of the target row - we push this to our target matrix - * If it does not we repeat again adding bits of the 2nd, 3rd,... rows of the source to the target. - * We repeat for all rows - noting that we may NOT start at the beginning of a source row - but at `$x` are cursor position. +***You are given an array of integers. Write a script to find the highest value obtained by XORing any two distinct members of the array.*** + +## Solution -This method is more efficient memory wise - a consideration if you are reshaping a large matrix. +There is nothing other than brute force to find the solution as we have to check every combination. Just how we do this - using indexes or `shift` and `for{each}`. We go for the latter and the code becomes simple. ```perl -sub reshape2 { - my($m,$r,$c) = @_; - my $R=@{$m}; my $C = @{$m->[0]}; - return 0 unless (my $R=@{$m}) * (my $C=@{$m->[0]}) == $r * $c; - my($x,$y,$res)=(0,0); - for my $i (1..$r) { - my $row = []; - my $to_push = $c; - while( $to_push ) { - my $t = $to_push+$x<$C?$to_push+$x:$C; - push @{$row},@{$m->[$y]}[$x..$t-1]; - $to_push -= $t-$x; - $t==$C ? ($y++,$x=0) : ($x=$t); - } - push @{$res},$row; +sub max_xor { + my $m = 0; + while( @_ ){ + my $x=shift; + ( $x^$_ ) > $m && ( $m = $x^$_ ) for @_ } - $res + $m } ``` +To avoid a set of brackets we use the simple logic of `$a && $b` is the same as `$b if $a`. -- cgit From c12c6fe13582077164bd6d6020ca660479afe69d Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Feb 2023 07:25:40 +0000 Subject: Update blog.txt --- challenge-204/james-smith/blog.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-204/james-smith/blog.txt b/challenge-204/james-smith/blog.txt index c5ba6b2b9e..7d69656ef8 100644 --- a/challenge-204/james-smith/blog.txt +++ b/challenge-204/james-smith/blog.txt @@ -1 +1 @@ -https://github.com/drbaggy/perlweeklychallenge-club/blob/master/challenge-204/james-smith/blog.txt +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-204/james-smith -- cgit From 34cec274829afb29fc8dcc71be6316e81158db69 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Feb 2023 07:26:10 +0000 Subject: Create blog.txt --- challenge-205/james-smith/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-205/james-smith/blog.txt diff --git a/challenge-205/james-smith/blog.txt b/challenge-205/james-smith/blog.txt new file mode 100644 index 0000000000..0bdd535632 --- /dev/null +++ b/challenge-205/james-smith/blog.txt @@ -0,0 +1 @@ +https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-205/james-smith -- cgit From 842aa796349eba0e3956b66eec5c47c84201e3ac Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 20 Feb 2023 07:39:08 +0000 Subject: Challenge 205 Solutions (Raku) --- challenge-205/mark-anderson/raku/ch-1.raku | 22 ++++++++++++++++++++++ challenge-205/mark-anderson/raku/ch-2.raku | 11 +++++++++++ 2 files changed, 33 insertions(+) create mode 100644 challenge-205/mark-anderson/raku/ch-1.raku create mode 100644 challenge-205/mark-anderson/raku/ch-2.raku diff --git a/challenge-205/mark-anderson/raku/ch-1.raku b/challenge-205/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..7121178ef3 --- /dev/null +++ b/challenge-205/mark-anderson/raku/ch-1.raku @@ -0,0 +1,22 @@ +#!/usr/bin/env raku +use Test; + +is third-highest(5,3,4), 3; +is third-highest(5,6), 6; +is third-highest(5,4,4,3), 3; + +multi third-highest(*@a) +{ + third-highest(@a.Set) +} + +multi third-highest(Set $s where $s.elems < 3) +{ + $s.max.key +} + +multi third-highest(Set $s is copy) +{ + $s (-)= $s.max for ^2; + $s.max.key +} diff --git a/challenge-205/mark-anderson/raku/ch-2.raku b/challenge-205/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..c9b84ee11e --- /dev/null +++ b/challenge-205/mark-anderson/raku/ch-2.raku @@ -0,0 +1,11 @@ +#!/usr/bin/env raku +use Test; + +is max-xor(1,2,3,4,5,6,7), 7; +is max-xor(2,4,1,3), 7; +is max-xor(10,5,7,12,8), 15; + +sub max-xor(*@a) +{ + max @a.combinations(2).map({ .[0] +^ .[1] }) +} -- cgit From 353c0d9e762f0835f7e58c94c28a876dc0c880b0 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 20 Feb 2023 08:49:56 +0000 Subject: w205 - Tast 1 & 2 --- challenge-205/perlboy1967/perl/ch1.pl | 36 ++++++++++++++++++++++++++++ challenge-205/perlboy1967/perl/ch2.pl | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100755 challenge-205/perlboy1967/perl/ch1.pl create mode 100755 challenge-205/perlboy1967/perl/ch2.pl diff --git a/challenge-205/perlboy1967/perl/ch1.pl b/challenge-205/perlboy1967/perl/ch1.pl new file mode 100755 index 0000000000..083e321403 --- /dev/null +++ b/challenge-205/perlboy1967/perl/ch1.pl @@ -0,0 +1,36 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 205 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-205 + +Author: Niels 'PerlBoy' van Dijke + +Task 1: Third Highest +Submitted by: Mohammad S Anwar + +You are given an array of integers. + +Write a script to find out the Third Highest if found otherwise return the maximum. + +=cut + +use v5.16; + +use common::sense; + +use Test::More; + +sub nHighest ($@) { + my $n = shift; return (sort {$a <=> $b} @_)[scalar @_ >= $n ? -$n : -1]; +} + +is(3,nHighest(3,5,3,4)); +is(6,nHighest(3,5,6)); +is(4,nHighest(3,5,4,4,3)); +is(0,nHighest(3,0,0,0,0)); +is(9,nHighest(3,9)); +is(undef,nHighest(3)); + +done_testing; diff --git a/challenge-205/perlboy1967/perl/ch2.pl b/challenge-205/perlboy1967/perl/ch2.pl new file mode 100755 index 0000000000..f088d2d334 --- /dev/null +++ b/challenge-205/perlboy1967/perl/ch2.pl @@ -0,0 +1,44 @@ +#!/bin/perl + +=pod + +The Weekly Challenge - 205 +- https://theweeklychallenge.org/blog/perl-weekly-challenge-205 + +Author: Niels 'PerlBoy' van Dijke + +Task 2: Maximum XOR +Submitted by: Mohammad S Anwar + +You are given an array of integers. + +Write a script to find the highest value obtained by XORing any two distinct members of the array. + +=cut + +use v5.16; + +use common::sense; + +use List::MoreUtils qw(uniq); +use Algorithm::Combinatorics qw(combinations); + +use Test::More; + +sub maXor ($@) { + my (@l) = uniq(@_); + my $max; + + for (combinations(\@l,2)) { + my $xor = $$_[0] ^ $$_[1]; + $max = $xor if (!defined $max or $max < $xor); + } + + return $max; +} + +is(7,maXor(1,2,3,4,5,6,7)); +is(7,maXor(2,4,1,3)); +is(15,maXor(10,5,7,12,8)); + +done_testing; -- cgit From 0b962ffb1ed74409c2b92ba7f1142559b8351be8 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 20 Feb 2023 09:13:59 +0000 Subject: Task 2 - A bit more compact using List::Util 'max' --- challenge-205/perlboy1967/perl/ch2.pl | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/challenge-205/perlboy1967/perl/ch2.pl b/challenge-205/perlboy1967/perl/ch2.pl index f088d2d334..b9b0d5a469 100755 --- a/challenge-205/perlboy1967/perl/ch2.pl +++ b/challenge-205/perlboy1967/perl/ch2.pl @@ -20,21 +20,14 @@ use v5.16; use common::sense; +use List::Util qw(max); use List::MoreUtils qw(uniq); use Algorithm::Combinatorics qw(combinations); use Test::More; sub maXor ($@) { - my (@l) = uniq(@_); - my $max; - - for (combinations(\@l,2)) { - my $xor = $$_[0] ^ $$_[1]; - $max = $xor if (!defined $max or $max < $xor); - } - - return $max; + my (@l) = uniq(@_); return max(map { $$_[0] ^ $$_[1] } combinations(\@l,2)); } is(7,maXor(1,2,3,4,5,6,7)); -- cgit From 440ec76f152ce8870ff5c06f7ba5cfac203b2338 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Mon, 20 Feb 2023 09:17:42 +0000 Subject: Task 2 - Remove unneeded '()' --- challenge-205/perlboy1967/perl/ch2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-205/perlboy1967/perl/ch2.pl b/challenge-205/perlboy1967/perl/ch2.pl index b9b0d5a469..988cc75be5 100755 --- a/challenge-205/perlboy1967/perl/ch2.pl +++ b/challenge-205/perlboy1967/perl/ch2.pl @@ -27,7 +27,7 @@ use Algorithm::Combinatorics qw(combinations); use Test::More; sub maXor ($@) { - my (@l) = uniq(@_); return max(map { $$_[0] ^ $$_[1] } combinations(\@l,2)); + my @l = uniq(@_); return max(map { $$_[0] ^ $$_[1] } combinations(\@l,2)); } is(7,maXor(1,2,3,4,5,6,7)); -- cgit From 1dd34f4c99747713393ea1b20afec29a9955be49 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 20 Feb 2023 23:44:07 +1100 Subject: Simon's solution to challenge 205 --- challenge-205/sgreen/README.md | 4 ++-- challenge-205/sgreen/blog.txt | 1 + challenge-205/sgreen/perl/ch-1.pl | 24 ++++++++++++++++++++++++ challenge-205/sgreen/perl/ch-2.pl | 30 ++++++++++++++++++++++++++++++ challenge-205/sgreen/python/ch-1.py | 21 +++++++++++++++++++++ challenge-205/sgreen/python/ch-2.py | 27 +++++++++++++++++++++++++++ 6 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 challenge-205/sgreen/blog.txt create mode 100755 challenge-205/sgreen/perl/ch-1.pl create mode 100755 challenge-205/sgreen/perl/ch-2.pl create mode 100755 challenge-205/sgreen/python/ch-1.py create mode 100755 challenge-205/sgreen/python/ch-2.py diff --git a/challenge-205/sgreen/README.md b/challenge-205/sgreen/README.md index ec08b855cc..b2c76ea65b 100644 --- a/challenge-205/sgreen/README.md +++ b/challenge-205/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 204 +# The Weekly Challenge 205 -Blog: [Weekly Challenge 204](https://dev.to/simongreennet/weekly-challenge-204-256e) +Blog: [Weekly Challenge 205](https://dev.to/simongreennet/weekly-challenge-205-3f3) diff --git a/challenge-205/sgreen/blog.txt b/challenge-205/sgreen/blog.txt new file mode 100644 index 0000000000..d7e139fcdf --- /dev/null +++ b/challenge-205/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-205-3f3 \ No newline at end of file diff --git a/challenge-205/sgreen/perl/ch-1.pl b/challenge-205/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..14acfcee75 --- /dev/null +++ b/challenge-205/sgreen/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'uniq'; + +sub main (@n) { + # Sort the list numerical, removing duplicates + @n = sort {$a <=> $b} ( uniq(@n) ); + + if ( $#n >= 2 ) { + # There are three or more integers, print the third largest + say $n[-3]; + } + else { + # There are less than three integers, print the largest + say $n[-1]; + } +} + +main(@ARGV); \ No newline at end of file diff --git a/challenge-205/sgreen/perl/ch-2.pl b/challenge-205/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..6ff8d04452 --- /dev/null +++ b/challenge-205/sgreen/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use List::Util 'uniq'; + +sub main (@n) { + # Remove non-unique values + @n = map { int($_) } uniq @n; + my $max = $n[0] ^ $n[1]; + + # Work through each combination of two numbers + foreach my $first ( 0 .. $#n - 1 ) { + foreach my $second ( $first + 1 .. $#n ) { + my $xor_value = $n[$first] ^ $n[$second]; + if ( $max < $xor_value ) { + # We have a new maximum value + $max = $xor_value; + } + } + } + + # Print the maximum value found + say $max; +} + +main(@ARGV); \ No newline at end of file diff --git a/challenge-205/sgreen/python/ch-1.py b/challenge-205/sgreen/python/ch-1.py new file mode 100755 index 0000000000..e9d98479cd --- /dev/null +++ b/challenge-205/sgreen/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + # Sort the list numerical, removing duplicates + n = sorted(set(n)) + + if len(n) >= 3: + # There are three or more integers, print the third largest + print(n[-3]) + else: + # There are less than three integers, print the largest + print(n[-1]) + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-205/sgreen/python/ch-2.py b/challenge-205/sgreen/python/ch-2.py new file mode 100755 index 0000000000..d578909fa5 --- /dev/null +++ b/challenge-205/sgreen/python/ch-2.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + # Remove non-unique values + n = list(set(n)) + items = len(n) + max = n[0] ^ n[1] + + # Work through each combination of two numbers + for first in range(items-1): + for second in range(first+1, items): + xor_value = n[first] ^ n[second] + if max < xor_value: + # We have a new maximum value + max = xor_value + + # Print the maximum value found + print(max) + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) -- cgit From 546bec805e08f484096c1154b212a0f12333ea6e Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Feb 2023 16:02:14 +0000 Subject: Update ch-1.pl --- challenge-205/james-smith/perl/ch-1.pl | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/challenge-205/james-smith/perl/ch-1.pl b/challenge-205/james-smith/perl/ch-1.pl index 9cecdc6dee..6948a260b5 100644 --- a/challenge-205/james-smith/perl/ch-1.pl +++ b/challenge-205/james-smith/perl/ch-1.pl @@ -12,8 +12,9 @@ my @TESTS = ( [ [5,4,4,3], 3, 4 ], ); -is( third( @{$_->[0]} ), $_->[2] ) for @TESTS; -is( third_unique( @{$_->[0]} ), $_->[1] ) for @TESTS; +is( third( @{$_->[0]} ), $_->[2] ) for @TESTS; +is( third_unique( @{$_->[0]} ), $_->[1] ) for @TESTS; +is( third_unique_inf( @{$_->[0]} ), $_->[1] ) for @TESTS; done_testing(); @@ -23,7 +24,7 @@ sub third { $_ > $i ? (($i,$j,$k)=($_,$i,$j)) : $_ > $j ? (($j,$k)=($_,$j)) : ( $_ > $k ) && ($k=$_) for @_; - $k; + $k } sub third_unique { @@ -33,5 +34,15 @@ sub third_unique { : !defined $j || $_ > $j ? (($j,$k)=($_,$j)) : defined $j && $_ == $j ? () : ( !defined $k || $_ > $k ) && ($k=$_) for @_; - $k//$i; + $k//$i +} + +sub third_unique_inf { + my ($i,$j,$k) = (shift,'-inf','-inf'); + $_ > $i ? (($i,$j,$k)=($_,$i,$j)) + : $_ == $i ? () + : $_ > $j ? (($j,$k)=($_,$j)) + : $_ == $j ? () + : $_ > $k && ($k=$_) for @_; + $k eq '-inf' ? $i : $k } -- cgit From 6409f622fdf8a713b96b2f30818da47010aa5966 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 20 Feb 2023 16:06:57 +0000 Subject: Update README.md --- challenge-205/james-smith/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/challenge-205/james-smith/README.md b/challenge-205/james-smith/README.md index be97d5f11b..9f095be3e9 100644 --- a/challenge-205/james-smith/README.md +++ b/challenge-205/james-smith/README.md @@ -55,6 +55,23 @@ sub third_unique { } ``` +### A third solution + +Having the extra `defined` queries in the code had seems a little inefficient. We can get round these by using the special variable `'-inf'`. + +Perl does not have a true concept of "infinity". But does have the string `'-inf'` - if you do `$i > '-inf'` it will always be true for all `$i`. Unlike `$i > undef` which is treated as `$i > 0`. + +```perl +sub third_unique_inf { + my ($i,$j,$k) = (shift,'-inf','-inf'); + $_ > $i ? ( ($i,$j,$k) = ($_,$i,$j) ) + : $_ == $i ? ( ) + : $_ > $j ? ( ($j,$k) = ($_,$j) ) + : $_ == $j ? ( ) + : $_ > $k && ( $k = $_ ) for @_; + $k eq '-inf' ? $i : $k +} +``` # Task 2: Maximum XOR ***You are given an array of integers. Write a script to find the highest value obtained by XORing any two distinct members of the array.*** -- cgit From 0c9421c9735b996f10d5136a4603f5010cafc4da Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 20 Feb 2023 10:37:15 -0600 Subject: Solve PWC205 --- challenge-205/wlmb/blog.txt | 2 ++ challenge-205/wlmb/perl/ch-1.pl | 14 ++++++++++++++ challenge-205/wlmb/perl/ch-2.pl | 13 +++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 challenge-205/wlmb/blog.txt create mode 100755 challenge-205/wlmb/perl/ch-1.pl create mode 100755 challenge-205/wlmb/perl/ch-2.pl diff --git a/challenge-205/wlmb/blog.txt b/challenge-205/wlmb/blog.txt new file mode 100644 index 0000000000..3093a2ced6 --- /dev/null +++ b/challenge-205/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/02/20/PWC205/ + diff --git a/challenge-205/wlmb/perl/ch-1.pl b/challenge-205/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..5ca49bfbd8 --- /dev/null +++ b/challenge-205/wlmb/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/env perl +# Perl weekly challenge 205 +# Task 1: Third Highest +# +# See https://wlmb.github.io/2023/02/20/PWC205/#task-1-third-highest +use v5.36; +use List::Util qw(uniqint); +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to find the third highest number from the list N1 N2... + FIN +my @list=(sort {$b<=>$a} uniqint @ARGV); # filter out repetitions and sort descending +push @list, ($list[0]) x 2; # Add two copies of the maximum at the end of the list +say join " ", @ARGV, "->", $list[2]; diff --git a/challenge-205/wlmb/perl/ch-2.pl b/challenge-205/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..58665e6e2e --- /dev/null +++ b/challenge-205/wlmb/perl/ch-2.pl @@ -0,0 +1,13 @@ +#!/usr/bin/env perl +# Perl weekly challenge 205 +# Task 2: Maximum XOR +# +# See https://wlmb.github.io/2023/02/20/PWC205/#task-2-maximum-xor +use v5.36; +use Algorithm::Combinatorics qw(combinations); +use List::Util qw(max); +die <<~"FIN" unless @ARGV >= 2; + Usage: $0 N1 N2 [N3...] + to find the maximum xor of pairs from the list N1 N2 N3... + FIN +say join " ", @ARGV, "->", max map {$_->[0] ^ $_->[1]} combinations([@ARGV],2); -- cgit From a8851f754e8620a8fc8714f8e5bcc93263abd16b Mon Sep 17 00:00:00 2001 From: Shawn Wagner Date: Mon, 20 Feb 2023 08:46:44 -0800 Subject: Challenge 205.1 in Racket --- challenge-205/shawn-wagner/racket/ch-1.rkt | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 challenge-205/shawn-wagner/racket/ch-1.rkt diff --git a/challenge-205/shawn-wagner/racket/ch-1.rkt b/challenge-205/shawn-wagner/racket/ch-1.rkt new file mode 100644 index 0000000000..8cd2fcba41 --- /dev/null +++ b/challenge-205/shawn-wagner/racket/ch-1.rkt @@ -0,0 +1,46 @@ +#lang racket/base + +(require srfi/43) ; For vector-swap! + +(define (vector-partition! vec left right pivot-index list vec)) + (kth-element! (vector-copy vec) 2 >))) + +(define (demo num vec) + (printf "Example ~A: Third highest element of ~S -> ~A~%" num vec (solution vec))) + +(demo 1 #(5 3 4)) +(demo 2 #(5 6)) +(demo 3 #(5 4 4 3)) -- cgit From 3dae1a2b1bbf3762da3599a1d5db9044eace91f2 Mon Sep 17 00:00:00 2001 From: Shawn Wagner Date: Mon, 20 Feb 2023 08:58:30 -0800 Subject: Challenge 205.2 in Racket --- challenge-205/shawn-wagner/racket/ch-2.rkt | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 challenge-205/shawn-wagner/racket/ch-2.rkt diff --git a/challenge-205/shawn-wagner/racket/ch-2.rkt b/challenge-205/shawn-wagner/racket/ch-2.rkt new file mode 100644 index 0000000000..2d8d88e5b8 --- /dev/null +++ b/challenge-205/shawn-wagner/racket/ch-2.rkt @@ -0,0 +1,21 @@ +#lang racket/base + +(require racket/list racket/match) +(define (solution nums) + (for/fold ([num1 -1] + [num2 -1] + [max-xor 0]) + ([pair (in-combinations nums 2)]) + (match-let ([(list p1 p2) pair]) + (let ([xor (bitwise-xor p1 p2)]) + (if (> xor max-xor) + (values p1 p2 xor) + (values num1 num2 max-xor)))))) + +(define (demo n nums) + (let-values ([(n1 n2 xor) (solution nums)]) + (printf "Example ~A: The maximum result of ~A xor ~A = ~A~%" n n1 n2 xor))) + +(demo 1 '(1 2 3 4 5 6 7)) +(demo 2 '(2 4 1 3)) +(demo 3 '(10 5 7 12 8)) -- cgit From 4b8ff9d4b007d5393eaaf16fde8e731f620c7c70 Mon Sep 17 00:00:00 2001 From: Peter Campbell Smith Date: Mon, 20 Feb 2023 16:59:19 +0000 Subject: Week 205 submission --- challenge-205/peter-campbell-smith/blog.txt | 1 + challenge-205/peter-campbell-smith/perl/ch-1.pl | 54 ++++++++++++++++++++ challenge-205/peter-campbell-smith/perl/ch-2.pl | 65 +++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 challenge-205/peter-campbell-smith/blog.txt create mode 100755 challenge-205/peter-campbell-smith/perl/ch-1.pl create mode 100755 challenge-205/peter-campbell-smith/perl/ch-2.pl diff --git a/challenge-205/peter-campbell-smith/blog.txt b/challenge-205/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..29316a7b58 --- /dev/null +++ b/challenge-205/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/challenge/205 diff --git a/challenge-205/peter-campbell-smith/perl/ch-1.pl b/challenge-205/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..a070021203 --- /dev/null +++ b/challenge-205/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-20 + +use v5.28; +use utf8; +use warnings; +use List::Uniq ':all'; + +# Task: You are given an array of integers. Write a script to find the +# third highest value in the array. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/205/1 + +my (@test, $j); + +third_highest(5, 3, 4); +third_highest(5, 6); +third_highest(5, 4, 4, 3); +third_highest(-3, -2, -1); +third_highest(1, 1, 1, 1, 1); +third_highest(); + +# make a longer example - 100 random numbers in (0 .. 999) +for $j (0 .. 99) { + $test[$j] = int(rand(1000)); +} +third_highest(@test); + +sub third_highest { + + # find the 3rd highest value in the argument list + my (@array, $result, $count); + + # get the unique values in the list and sort them largest to smallest + @array = sort {$b <=> $a} uniq(@_); + $count = scalar @array; + + # select appropriate legend + if ($count >= 3) { + $result = qq[First highest is $array[0]. Second highest is $array[1]. Third highest is $array[2].]; + } elsif ($count == 2) { + $result = qq[First highest is $array[0]. Second highest is $array[1]. Third highest is missing so maximum is returned.]; + } elsif ($count == 1) { + $result = qq[First highest is $array[0]. Second and third highest are missing so maximum is returned.]; + } else { + $result = 'List is empty.'; + } + + say qq[\nInput: \@array = (] . join (', ', @_) . ')' . qq[\nOutput: $result]; +} + + + diff --git a/challenge-205/peter-campbell-smith/perl/ch-2.pl b/challenge-205/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..8a9db793b0 --- /dev/null +++ b/challenge-205/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl + +# Peter Campbell Smith - 2023-02-20 + +use v5.28; +use utf8; +use warnings; +use List::Uniq ':all'; + +# Task: You are given an array of integers. Write a script to find the highest value obtained +# by XORing any two distinct members of the array. + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge/205/2 + +my ($j, @test); + +max_xor(1, 2, 3, 4, 5, 6, 7); +max_xor(2, 4, 1, 3); +max_xor(10, 5, 7, 12, 8); +max_xor(21, 25); +max_xor(32, 32, 32, 32); + +for $j (1 .. 20) { + @test[$j - 1] = int(rand(126)) + 1; +} +max_xor(@test); + +sub max_xor { + + my ($count, $i, $j, $max, $xor, $result, @array, $largest, $limit); + + @array = @_; + $count = scalar @array; + + # find the largest number in the array + $largest = 0; + for $j (@array) { + $largest = $j if $j > $largest; + } + + # find the largest possible xor + for $j (1 .. 31) { + next unless 2 ** $j > $largest; + $limit = 2 ** $j - 1; + last; + } + + # now scan for the largest xor + $max = -1; + if ($count >= 2) { + SCAN: for $i (0 .. $count - 2) { + for $j ($i + 1 .. $count - 1) { + $xor = $_[$i] ^ $_[$j]; + if ($xor > $max) { + $result = qq[$array[$i] xor $array[$j] = $xor]; + $max = $xor; + last SCAN if $max == $limit; + } + } + } + say qq[\nInput: \@array = (] . join(', ', @_) . ')' . qq[\nOutput: $result]; + } else { + say 'Array must contan at least 2 elements.' + } +} -- cgit From bbb82248d0bacdef85043e2144311922ccc12df5 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 20 Feb 2023 12:20:13 -0500 Subject: Week 205 --- challenge-205/zapwai/perl/ch-1.pl | 37 +++++++++++++++++++++++++++++++++++++ challenge-205/zapwai/perl/ch-2.pl | 17 +++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 challenge-205/zapwai/perl/ch-1.pl create mode 100644 challenge-205/zapwai/perl/ch-2.pl diff --git a/challenge-205/zapwai/perl/ch-1.pl b/challenge-205/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..e465bf444e --- /dev/null +++ b/challenge-205/zapwai/perl/ch-1.pl @@ -0,0 +1,37 @@ +use v5.30.0; +my @array = (5,3,4,4); +say "Input: \@array = (".join(",",@array).")"; +print "Output: "; +@array = sort @array; +sub third { + my @l = @_; + my $max1 = $l[$#l]; + unless ( defined $max1 ) { + say "Highest value is NULL"; + return; + } + my $max2; + for (0..$#l) { + if ($l[$#l-$_] != $max1) { + $max2 = $l[$#l-$_]; + last; + } + } + unless (defined($max2)) { + say "Highest value is $max1"; + return; + } + my $max3; + for (0..$#l) { + if (($l[$#l-$_] != $max1) and ($l[$#l-$_] != $max2)) { + $max3 = $l[$#l-$_]; + last; + } + } + unless (defined($max3)) { + say "Highest value is $max1, second highest is $max2."; + return; + } + say $max3."\n\t[In order, highest to lowest top 3 values: ($max1, $max2, $max3)]"; +} +third(@array); diff --git a/challenge-205/zapwai/perl/ch-2.pl b/challenge-205/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..a49777461d --- /dev/null +++ b/challenge-205/zapwai/perl/ch-2.pl @@ -0,0 +1,17 @@ +use v5.30.0; +my @array = (1,2,3,4,5,6,7); +say "Input: \@array = (".join(",",@array).")"; +print "Output: "; +my $max = 0; +my ($a,$b); +for my $i (0 .. $#array - 1) { + for my $j ($i+1 .. $#array) { + my $n = $array[$i]^$array[$j]; + if ($max < $n) { + $max = $n; + $a = $array[$i]; + $b = $array[$j]; + } + } +} +say $max . " ($a xor $b = $max)"; -- cgit From a72c906dd4e57106d336c850b8540f683658111a Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 14 Feb 2023 10:47:23 +0100 Subject: Challenge 202 Task1 LK Perl --- challenge-202/lubos-kolouch/perl/ch-1.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-202/lubos-kolouch/perl/ch-1.pl diff --git a/challenge-202/lubos-kolouch/perl/ch-1.pl b/challenge-202/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..27f43e0643 --- /dev/null +++ b/challenge-202/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use Test::More tests => 4; + +sub has_consecutive_odds { +my @array = @_; +my $count = 0; +for (my $i=0; $i<@array-2; $i++) { +if ($array[$i] % 2 == 1 && $array[$i+1] % 2 == 1 && $array[$i+2] % 2 == 1) { +$count++; +} +} +return $count > 0 ? 1 : 0; +} + +ok(has_consecutive_odds(1,5,3,6), 'Example 1'); +ok(!has_consecutive_odds(2,6,3,5), 'Example 2'); +ok(!has_consecutive_odds(1,2,3,4), 'Example 3'); +ok(has_consecutive_odds(2,3,5,7), 'Example 4'); -- cgit From 071b4dcd7d01a3a7e9aebcb60d0103ee427a8707 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 14 Feb 2023 10:48:46 +0100 Subject: Challenge 201 Task1 LK Perl --- challenge-201/lubos-kolouch/perl/ch-1.pl | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 challenge-201/lubos-kolouch/perl/ch-1.pl diff --git a/challenge-201/lubos-kolouch/perl/ch-1.pl b/challenge-201/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..df137a8f7a --- /dev/null +++ b/challenge-201/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,19 @@ +use strict; +use warnings; +use Test::More tests => 2; + +sub find_missing_numbers { + my @array = @_; + my $n = @array; + my %hash = map { $_ => 1 } @array; + my @missing_numbers; + for (my $i=0; $i<=$n; $i++) { + push @missing_numbers, $i unless exists $hash{$i}; + } + return @missing_numbers; +} + +# test cases +is_deeply([find_missing_numbers(0,1,3)], [2], 'Example 1'); +is_deeply([find_missing_numbers(0,1)], [2], 'Example 2'); + -- cgit From e98fa50d9dd47dd02ce01b6db838fd05ec0e1a97 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 14 Feb 2023 11:00:50 +0100 Subject: Challenge 201 Task1 LK Python --- challenge-201/lubos-kolouch/python/ch-1.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 challenge-201/lubos-kolouch/python/ch-1.py diff --git a/challenge-201/lubos-kolouch/python/ch-1.py b/challenge-201/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..dc7229e912 --- /dev/null +++ b/challenge-201/lubos-kolouch/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + + +def find_missing_numbers(arr): + n = len(arr) + s = set(arr) + missing_numbers = [] + for i in range(n + 1): + if i not in s: + missing_numbers.append(i) + return missing_numbers + + +# test cases +class TestMissingNumbers(unittest.TestCase): + + def test_example1(self): + self.assertEqual(find_missing_numbers([0, 1, 3]), [2]) + + def test_example2(self): + self.assertEqual(find_missing_numbers([0, 1]), [2]) + + +if __name__ == '__main__': + unittest.main() -- cgit From 0b0afd9eb7890646aff2c1b5b0a0db87e02dc180 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 14 Feb 2023 11:02:32 +0100 Subject: Challenge 201 Task1 LK Python --- challenge-201/lubos-kolouch/python/ch-1.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/challenge-201/lubos-kolouch/python/ch-1.py b/challenge-201/lubos-kolouch/python/ch-1.py index dc7229e912..4f638a4719 100644 --- a/challenge-201/lubos-kolouch/python/ch-1.py +++ b/challenge-201/lubos-kolouch/python/ch-1.py @@ -1,10 +1,18 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from typing import List import unittest -def find_missing_numbers(arr): +def find_missing_numbers(arr: List[int]) -> List[int]: + """ + Find all missing numbers in the range 0..n where n is the array size. + Args: + arr: A list of unique integers. + Returns: + A list of missing integers in the range 0..n where n is the array size. + """ n = len(arr) s = set(arr) missing_numbers = [] -- cgit From 69b616a5090cbbb2500c09755116c0391bdad11b Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 20 Feb 2023 20:41:19 +0100 Subject: Challenge 205 Task 1 2 LK Perl Python --- challenge-205/lubos-kolouch/perl/ch-1.pl | 35 ++++++++++++++++++++ challenge-205/lubos-kolouch/perl/ch-2.pl | 25 ++++++++++++++ challenge-205/lubos-kolouch/python/ch-1.py | 53 ++++++++++++++++++++++++++++++ challenge-205/lubos-kolouch/python/ch-2.py | 34 +++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 challenge-205/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-205/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-205/lubos-kolouch/python/ch-1.py create mode 100644 challenge-205/lubos-kolouch/python/ch-2.py diff --git a/challenge-205/lubos-kolouch/perl/ch-1.pl b/challenge-205/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..8820145b8b --- /dev/null +++ b/challenge-205/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,35 @@ +use strict; +use warnings; +use Test::More; + +sub find_third_highest { + my @array = @_; + + my @sorted = sort { $b <=> $a } @array; + my $distinct_count = 0; + my $last_number = undef; + + foreach my $number (@sorted) { + if ( !defined($last_number) || $number != $last_number ) { + $distinct_count++; + if ( $distinct_count == 3 ) { + return $number; + } + } + $last_number = $number; + } + + return $sorted[0]; +} + +# Testing +my @array1 = ( 5, 3, 4 ); +is( find_third_highest(@array1), 3, "Example 1" ); + +my @array2 = ( 5, 6 ); +is( find_third_highest(@array2), 6, "Example 2" ); + +my @array3 = ( 5, 4, 4, 3 ); +is( find_third_highest(@array3), 3, "Example 3" ); + +done_testing(); diff --git a/challenge-205/lubos-kolouch/perl/ch-2.pl b/challenge-205/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..2aa25c1ffb --- /dev/null +++ b/challenge-205/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +use Test::More; + +sub find_max_xor { + my @array = @_; + my $max_xor = 0; + for my $i ( 0 .. $#array ) { + for my $j ( $i + 1 .. $#array ) { + my $xor = $array[$i] ^ $array[$j]; + if ( $xor > $max_xor ) { + $max_xor = $xor; + } + } + } + return $max_xor; +} + +# Test cases +is( find_max_xor( 1, 2, 3, 4, 5, 6, 7 ), 7, "Example 1" ); +is( find_max_xor( 2, 4, 1, 3 ), 7, "Example 2" ); +is( find_max_xor( 10, 5, 7, 12, 8 ), 15, "Example 3" ); +is( find_max_xor( 2, 2, 2, 2 ), 0, "All elements the same" ); + +done_testing(); diff --git a/challenge-205/lubos-kolouch/python/ch-1.py b/challenge-205/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..45fbdc1f3e --- /dev/null +++ b/challenge-205/lubos-kolouch/python/ch-1.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest +from typing import List + + +def find_third_highest(array: List[int]) -> int: + """ + Finds the third highest number in an array of integers. + + If the array has fewer than three distinct values, returns the maximum value. + + Args: + array: List of integers to search. + + Returns: + Third highest distinct integer, or maximum integer if there are fewer than three distinct values. + """ + + distinct_numbers = set(array) + if len(distinct_numbers) < 3: + return max(array) + else: + # Sort distinct numbers in descending order + sorted_numbers = sorted(distinct_numbers, reverse=True) + return sorted_numbers[2] + + +class TestFindThirdHighest(unittest.TestCase): + def test_example1(self): + array = [5, 3, 4] + self.assertEqual(find_third_highest(array), 3) + + def test_example2(self): + array = [5, 6] + self.assertEqual(find_third_highest(array), 6) + + def test_example3(self): + array = [5, 4, 4, 3] + self.assertEqual(find_third_highest(array), 3) + + def test_all_same(self): + array = [2, 2, 2, 2] + self.assertEqual(find_third_highest(array), 2) + + def test_negative_numbers(self): + array = [1, -5, 3, -5, 2] + self.assertEqual(find_third_highest(array), 1) + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-205/lubos-kolouch/python/ch-2.py b/challenge-205/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..35ee469eeb --- /dev/null +++ b/challenge-205/lubos-kolouch/python/ch-2.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def find_max_xor(array: List[int]) -> int: + """ + Finds the highest value obtained by XORing any two distinct members of an array of integers. + + Args: + array: List of integers to search. + + Returns: + The highest value obtained by XORing any two distinct members of the input array. + """ + max_xor = 0 + for i in range(len(array)): + for j in range(i + 1, len(array)): + xor = array[i] ^ array[j] + if xor > max_xor: + max_xor = xor + return max_xor + + +# Test cases +def test_find_max_xor(): + assert find_max_xor([1, 2, 3, 4, 5, 6, 7]) == 7 + assert find_max_xor([2, 4, 1, 3]) == 7 + assert find_max_xor([10, 5, 7, 12, 8]) == 15 + assert find_max_xor([2, 2, 2, 2]) == 0 + + +test_find_max_xor() -- cgit From ad33b35727c8cd464b83c523b75d1e081e30403d Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 20 Feb 2023 14:13:50 -0600 Subject: Week 205 Task 1 --- challenge-205/bob-lied/README | 6 +-- challenge-205/bob-lied/perl/ch-1.pl | 87 +++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 challenge-205/bob-lied/perl/ch-1.pl diff --git a/challenge-205/bob-lied/README b/challenge-205/bob-lied/README index 4294e8abc0..2f7d2577c4 100644 --- a/challenge-205/bob-lied/README +++ b/challenge-205/bob-lied/README @@ -1,4 +1,4 @@ -Solutions to weekly challenge 204 by Bob Lied +Solutions to weekly challenge 205 by Bob Lied -https://perlweeklychallenge.org/blog/perl-weekly-challenge-204/ -https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-204/bob-lied +https://perlweeklychallenge.org/blog/perl-weekly-challenge-205/ +https://github.com/boblied/perlweeklychallenge-club/tree/master/challenge-205/bob-lied diff --git a/challenge-205/bob-lied/perl/ch-1.pl b/challenge-205/bob-lied/perl/ch-1.pl new file mode 100644 index 0000000000..50f5de1ea0 --- /dev/null +++ b/challenge-205/bob-lied/perl/ch-1.pl @@ -0,0 +1,87 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-1.pl Perl Weekly Challenge Week 205 Task 1 Third Highest +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers. +# Write a script to find out the Third Highest if found otherwise return +# the maximum. +# Example 1 Input: @array = (5,3,4) Output: 3 +# First highest is 5. Second highest is 4. Third highest is 3. +# Example 2 Input: @array = (5,6) Output: 6 +# First highest is 6. Second highest is 5. Third highest is missing, +# so maximum is returned. +# Example 2 Input: @array = (5,4,4,3) Output: 3 +# First highest is 5. Second highest is 4. Third highest is 3. +#============================================================================= + +use v5.36; + +use List::Util qw/max/; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +say thirdHighest(@ARGV); + +sub thirdHighest(@array) +{ + return 0 if !@array; + + my @top3 = ( shift @array ); # In descending order + + for my $elem ( @array ) + { + # Add the element to the front of the list of possibiliities + unshift @top3, $elem; + + # Bubble the element down to its sorted position + # but we only care about the top 3 positions + ELEM: for ( my $i = 0 ; $i < $#top3 && $i < 3 ; $i++ ) + { + for ( my $j = $i + 1 ; $j < 3 ; $j++ ) + { + if ( $top3[$i] == $top3[$j] ) + { + # Duplicate value, discard + splice(@top3, $i, 1); + next ELEM; + } + elsif ( $top3[$i] < $top3[$j] ) + { + # Swap + @top3[$i, $j] = @top3[$j, $i]; + } + } + } + } + + return max(@array) if ( scalar(@top3) < 3 ); + return $top3[2]; +} + +sub runTest +{ + use Test2::V0; + + is( thirdHighest(5,3,4), 3, "Example 1"); + is( thirdHighest(5,6 ), 6, "Example 2"); + is( thirdHighest(5,4,4,3), 3, "Example 3"); + is( thirdHighest(5,5,5,5), 5, "Only one"); + is( thirdHighest(5,5,5,9), 9, "Only two"); + is( thirdHighest( ), 0, "Test 0"); + is( thirdHighest(5,5,6,7), 5, "Test 1"); + is( thirdHighest(5,6,7,7), 5, "Test 2"); + is( thirdHighest(5,6,6,7,8), 6, "Test 3"); + is( thirdHighest(5,5,6,6,7,7,8,8), 6, "Duplicates"); + is( thirdHighest(2,4,6,8,3,4,6,8), 4, "Duplicates v2"); + is( thirdHighest( 10..99 ), 97, "Longer list"); + + done_testing; +} -- cgit From bb758f01f98669e69cffea9ab78a7e10aa11238c Mon Sep 17 00:00:00 2001 From: boblied Date: Mon, 20 Feb 2023 14:37:15 -0600 Subject: Week 205 Tasks 1 and 2 --- challenge-205/bob-lied/perl/ch-1.pl | 22 +++++------- challenge-205/bob-lied/perl/ch-2.pl | 70 +++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 challenge-205/bob-lied/perl/ch-2.pl diff --git a/challenge-205/bob-lied/perl/ch-1.pl b/challenge-205/bob-lied/perl/ch-1.pl index 50f5de1ea0..579a9c98e0 100644 --- a/challenge-205/bob-lied/perl/ch-1.pl +++ b/challenge-205/bob-lied/perl/ch-1.pl @@ -43,21 +43,17 @@ sub thirdHighest(@array) # Bubble the element down to its sorted position # but we only care about the top 3 positions - ELEM: for ( my $i = 0 ; $i < $#top3 && $i < 3 ; $i++ ) + for ( my $i = 0 ; $i < $#top3 ; $i++ ) { - for ( my $j = $i + 1 ; $j < 3 ; $j++ ) + if ( $top3[$i] == $top3[$i+1] ) { - if ( $top3[$i] == $top3[$j] ) - { - # Duplicate value, discard - splice(@top3, $i, 1); - next ELEM; - } - elsif ( $top3[$i] < $top3[$j] ) - { - # Swap - @top3[$i, $j] = @top3[$j, $i]; - } + # Duplicate value, discard + splice(@top3, $i, 1); + } + elsif ( $top3[$i] < $top3[$i+1] ) + { + # Swap + @top3[$i, $i+1] = @top3[$i+1, $i]; } } } diff --git a/challenge-205/bob-lied/perl/ch-2.pl b/challenge-205/bob-lied/perl/ch-2.pl new file mode 100644 index 0000000000..da5189c027 --- /dev/null +++ b/challenge-205/bob-lied/perl/ch-2.pl @@ -0,0 +1,70 @@ +#!/usr/bin/env perl +# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu: +#============================================================================= +# ch-2.pl Perl Weekly Challenge Task 2 Maximum XOR +#============================================================================= +# Copyright (c) 2023, Bob Lied +#============================================================================= +# You are given an array of integers. +# Write a script to find the highest value obtained by XORing any two +# distinct members of the array. +# Example 1 Input: @array = (1,2,3,4,5,6,7) Output: 7 +# The maximum result of 1 xor 6 = 7. +# Example 2 Input: @array = (2,4,1,3) Output: 7 +# The maximum result of 4 xor 3 = 7. +# Example 3 Input: @array = (10,5,7,12,8) Output: 15 +# The maximum result of 10 xor 5 = 15. +#============================================================================= + +use v5.36; + +use Getopt::Long; +my $Verbose = 0; +my $DoTest = 0; + +GetOptions("test" => \$DoTest, "verbose" => \$Verbose); +exit(!runTest()) if $DoTest; + +sub maxXOR($array) +{ + return 0 if scalar(@$array) == 0; + + my $N = $#{$array}; + + # For a single element, return itself. Debatable because x^x == 0 + return $array->[0] if $N == 0; + + my $max = ( $array->[0] ^ $array->[1] ); + + # Consider evey combination of pairs. Because XOR is commutative, we + # only need to go through one ordering, not the complete NxN set. + + # This loop goes to N-1, so j can go to N + for ( my $i = 0 ; $i < $N ; $i++ ) + { + # j starts after i and goes to the very end + for ( my $j = $i+1; $j <= $N ; $j++ ) + { + my $xor = ( $array->[$i] ^ $array->[$j] ); + $max = $xor if $xor > $max; + } + } + return $max; +} + +sub runTest +{ + use Test2::V0; + + is( maxXOR([1,2,3,4,5,6,7 ]), 7, "Example 1"); + is( maxXOR([2,4,1,3 ]), 7, "Example 2"); + is( maxXOR([10,5,7,12,8 ]), 15, "Example 3"); + is( maxXOR([139 ]), 139, "Singleton"); + is( maxXOR([ ]), 0, "Empty"); + is( maxXOR([0x5, 0x8, 0x2]), 13, "Test 1"); + is( maxXOR([0x6, 0x8, 0x2]), 14, "Test 2"); + is( maxXOR([0x6, 0x8, 0x10]), 24, "Test 3"); + + done_testing; +} + -- cgit From f4d5f5a740d397a019f3d8eb3d40a35e54121b91 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Tue, 21 Feb 2023 00:14:23 +0100 Subject: Add solutions to 205: Third Highest & Maximum XOR by E. Choroba --- challenge-205/e-choroba/perl/ch-1.pl | 19 +++++++++++++++++++ challenge-205/e-choroba/perl/ch-2.pl | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100755 challenge-205/e-choroba/perl/ch-1.pl create mode 100755 challenge-205/e-choroba/perl/ch-2.pl diff --git a/challenge-205/e-choroba/perl/ch-1.pl b/challenge-205/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..65f2829a8e --- /dev/null +++ b/challenge-205/e-choroba/perl/ch-1.pl @@ -0,0 +1,19 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; +use List::Util qw{ uniq }; + +sub third_highest($list) { + my @sorted_uniq = sort { $a <=> $b } uniq(@$list); + return @sorted_uniq[@sorted_uniq > 2 ? -3 : -1]; +} + +use Test::More tests => 3 + 2; + +is third_highest([5, 3, 4]), 3, 'Example 1'; +is third_highest([5, 6]), 6, 'Example 2'; +is third_highest([5, 4, 4, 3]), 3, 'Example 3'; + +is third_highest([2]), 2, 'Single element'; +is third_highest([(1 .. 400) x 10]), 398, 'Long list'; diff --git a/challenge-205/e-choroba/perl/ch-2.pl b/challenge-205/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..760e887c90 --- /dev/null +++ b/challenge-205/e-choroba/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub maximum_xor($list) { + my $max = 0; + for my $x_index (0 .. $#$list - 1) { + for my $y_index ($x_index + 1 .. $#$list) { + my $candidate = $list->[$x_index] ^ $list->[$y_index]; + $max = $candidate if $candidate > $max; + } + } + return $max +} + +use Test::More tests => 3; + +is maximum_xor([1, 2, 3, 4, 5, 6, 7]), 7, 'Example 1'; +is maximum_xor([2, 4, 1, 3]), 7, 'Example 2'; +is maximum_xor([10, 5, 7, 12, 8]), 15, 'Example 3'; -- cgit From 4b4967e256b334e940c700dfc849b750210cec55 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 20 Feb 2023 19:12:11 -0500 Subject: #205 DAJ --- challenge-205/dave-jacoby/perl/ch-1.pl | 29 +++++++++++++++++++++++++++ challenge-205/dave-jacoby/perl/ch-2.pl | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 challenge-205/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-205/dave-jacoby/perl/ch-2.pl diff --git a/challenge-205/dave-jacoby/perl/ch-1.pl b/challenge-205/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..794e99f3d4 --- /dev/null +++ b/challenge-205/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,29 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ uniq max }; + +my @examples = ( + + [ 5, 3, 4 ], + [ 5, 6 ], + [ 5, 4, 4, 3 ], +); + +for my $e (@examples) { + my $list = join ',', $e->@*; + my $out = third_highest( $e->@* ); + say <<"END"; + Input: \@array = ($list) + Output: $out +END +} + +sub third_highest ( @array ) { + @array = uniq sort { $b <=> $a } @array; + return max @array if scalar @array < 3; + return $array[2]; +} diff --git a/challenge-205/dave-jacoby/perl/ch-2.pl b/challenge-205/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..bcf621512c --- /dev/null +++ b/challenge-205/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ max }; + +my @examples = ( + + [ 1, 2, 3, 4, 5, 6, 7 ], + [ 2, 4, 1, 3 ], + [ 10, 5, 7, 12, 8 ], +); + +for my $e (@examples) { + my $o = max_xor( $e->@* ); + my $array = $e->@*; + say <<"END"; + Input: \@array = $array + Output: $o +END +} + +sub max_xor ( @array ) { + my @output; + for my $i ( 0 .. -2 + scalar @array ) { + my $ii = $array[$i]; + for my $j ( $i + 1 .. -1 + scalar @array ) { + my $jj = $array[$j]; + my $x = $ii ^ $jj; + push @output, $x; + } + } + return max @output; +} -- cgit From e80e0016138ef8b58dba1f016af7efd5409539c3 Mon Sep 17 00:00:00 2001 From: rir Date: Mon, 20 Feb 2023 20:48:29 -0500 Subject: 205 --- challenge-205/0rir/raku/ch-1.raku | 51 +++++++++++++++++++++++++++++++++++ challenge-205/0rir/raku/ch-2.raku | 56 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 challenge-205/0rir/raku/ch-1.raku create mode 100644 challenge-205/0rir/raku/ch-2.raku diff --git a/challenge-205/0rir/raku/ch-1.raku b/challenge-205/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..dcc638f53a --- /dev/null +++ b/challenge-205/0rir/raku/ch-1.raku @@ -0,0 +1,51 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ « ␤ » ∴ +use v6.d; +use Test; + +=begin comment +205-1: Third Highest Submitted by: Mohammad S Anwar +Given an array of integers, find the Third Highest if existing otherwise +return the maximum. + +Example 1 +Input: @array = (5,3,4) +Output: 3 + +Example 2 +Input: @array = (5,6) +Output: 6 + +Example 2 +Input: @array = (5,4,4,3) +Output: 3 +=end comment + +my @Test = + [,], (Int), + [10,], 10, + [10,20], 20, + [1,1,1,1,1,20], 20, + [5,3,4], 3, + [5,6], 6, + [5,4,4,3], 3, + [5,4,3,-17,17,10,10,10,5,1,0,10,5], 5, +; + +plan +@Test div 2; + +sub third-highest( @a is copy --> Int ) { + my $triad = @a.sort({$^b <=> $^a}).unique[0..2]; + return $triad[2] // $triad[0] // Int; +} + +for @Test -> $in, $exp { + is third-highest(@$in), $exp, "$in.raku() --> $exp.raku()"; +} +done-testing; + +my @array = @Test[@Test.end][0].flat; + +say "\nInput: @array = @array[]\nOutput: &third-highest(@array)"; + + diff --git a/challenge-205/0rir/raku/ch-2.raku b/challenge-205/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..d4ef70eb6d --- /dev/null +++ b/challenge-205/0rir/raku/ch-2.raku @@ -0,0 +1,56 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅ ≡ ∩ ≢ ∈ « ␤ » ∴ +use v6.d; +use lib $?FILE.IO.parent(2).add("lib"); +use Test; + +=begin comment +205-2: Maximum XOR Submitted by: Mohammad S Anwar +Given an array of integers, find the highest value obtained by XORing any two distinct members of the array. + +Example 1 +Input: @array = (1,2,3,4,5,6,7) +Output: 7 + +The maximum result of 1 xor 6 = 7. +Example 2 +Input: @array = (2,4,1,3) +Output: 7 + +The maximum result of 4 xor 3 = 7. +Example 3 +Input: @array = (10,5,7,12,8) +Output: 7 + +The maximum result of 10 xor 5 = 15. +=end comment + +my @Test = + [( 1,), Int, ], + [( 1,1,), Int, ], + [( 1,2,3,4,5,6,7), 7, ], + [( 2,4,1,3), 7, ], + [( 10,5,7,12,8), 15, ], + [( -11,800), -811, ], + [( 1,11,111,1111,800), 1911, ], + [( 13,13,9,1), 12, ], + [( -100,1), -99, ], + [( -100,1,1), -99, ], +; +plan +@Test; + +multi highest-bitwise-xor-duo( @a where *.elems < 2 ) { Int } +multi highest-bitwise-xor-duo( @a --> Int) { + return Int if @a.unique.elems < 2 ; + max do ( $_[0] +^ $_[1]) for @a.unique.combinations(2) +} +for @Test -> (@in, $exp) { + is highest-bitwise-xor-duo( @in), $exp, "$exp.raku()\t<-- @in[]"; +} +done-testing; + +my @a = -100,1,1; +say "\nInput: @array = @a[]\n", + "Output: &highest-bitwise-xor-duo( @a)"; + + -- cgit From db67f42fe80703bfdb6ef77b96e2e1716bf22126 Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Tue, 21 Feb 2023 00:34:32 -0800 Subject: Robbie Hatley's Perl Solutions for PWCC #205 --- challenge-205/robbie-hatley/blog.txt | 1 + challenge-205/robbie-hatley/perl/ch-1.pl | 40 +++++++++++++++++++++ challenge-205/robbie-hatley/perl/ch-2.pl | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 challenge-205/robbie-hatley/blog.txt create mode 100755 challenge-205/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-205/robbie-hatley/perl/ch-2.pl diff --git a/challenge-205/robbie-hatley/blog.txt b/challenge-205/robbie-hatley/blog.txt new file mode 100644 index 0000000000..304d137101 --- /dev/null +++ b/challenge-205/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/02/robbie-hatleys-perl-solutions-to-weekly_21.html \ No newline at end of file diff --git a/challenge-205/robbie-hatley/perl/ch-1.pl b/challenge-205/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..42491eb2c1 --- /dev/null +++ b/challenge-205/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,40 @@ +#! /usr/bin/perl +# Robbie Hatley's Solution to PWCC 205-1 + +=pod + +Task 1: Third Highest +Submitted by: Mohammad S Anwar +Given an array of integers, write a script to find out the third-highest unique value, if the number of unique values +is at least 3; otherwise, return the maximum unique value. +Example 1: Input: (5,3,4) Output: 3 +Example 2: Input: (5,6) Output: 6 +Example 3: Input: (5,4,4,3) Output: 3 + +=cut + +# IO NOTES: +# NOTE: Input is by either built-in array-of-arrays, or @ARGV. If using @ARGV,the args should be a space-separated +# sequence of integers, which will be interpreted as being a single array. +# NOTE: Output is to STDOUT and will be the third-highest unique value if the number of unique values is at least 3; +# otherwise, the output will be the maximum unique value. + +# PRELIMINARIES: +use v5.36; +use List::Util 'uniqint'; +$"=", "; + +# DEFAULT INPUTS: +my @arrays = ([5,4,3], [5,6], [5,4,4,3]); + +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = ([@ARGV]);} + +# MAIN BODY OF SCRIPT: +for (@arrays){ + say ''; + my @array = @{$_}; + say "array: (@array)"; + my @unique = uniqint reverse sort @array; + if (@unique >= 3) {say "Third-highest unique value = $unique[2]"} + else {say "Maximum unique value = $unique[0]"}} \ No newline at end of file diff --git a/challenge-205/robbie-hatley/perl/ch-2.pl b/challenge-205/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..f1e55931fd --- /dev/null +++ b/challenge-205/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,62 @@ +#! /usr/bin/perl +# Robbie Hatley's Solution to PWCC 205-2 + +=pod + +Task 2: Maximum XOR +Submitted by: Mohammad S Anwar +Given an array of non-negative integers, write a script to find the highest +value obtained by XORing any two distinct members of the array. + +Example 1: Input: (1,2,3,4,5,6,7) Output: 7 +Example 2: Input: (2,4,1,3) Output: 7 +Example 3: Input: (10,5,7,12,8) Output: 15 + +The maximum result of 10 xor 5 = 15. + +=cut + +# IO NOTES: +# +# NOTE: Input is by either built-in array-of-arrays, or @ARGV. +# +# If using @ARGV,the args should be a space-separated sequence of +# integers, which should be the numbers of rows & columns of the +# original matrix, followed by the elements of the matrix in +# left-to-right-within-up-to-down order (like reading a book), +# followed by the numbers of rows & columns desired. For