# Perl Weekly Challenge 205.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 205.

# 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.

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.
``````

I use `uniqint` from `List::Util` to filter duplicates, sort the values in descending order and choose the third value. In case there are less than three distinct values, I copy the maximum at the end of the list a couple of times. This yields a oneliner:

``````perl -MList::Util=uniqint -E '
@l=(sort {\$b<=>\$a} uniqint @ARGV); push @l, (\$l[0])x2; say join " ", @ARGV, "->", \$l[2]' 5 3 4
perl -MList::Util=uniqint -E '
@l=(sort {\$b<=>\$a} uniqint @ARGV); push @l, (\$l[0])x2; say join " ", @ARGV, "->", \$l[2]' 5 6
perl -MList::Util=uniqint -E '
@l=(sort {\$b<=>\$a} uniqint @ARGV); push @l, (\$l[0])x2; say join " ", @ARGV, "->", \$l[2]' 5 4 4 3
``````

Results:

``````5 3 4 -> 3
5 6 -> 6
5 4 4 3 -> 3
``````

Other examples:

``````perl -MList::Util=uniqint -E '
@l=(sort {\$b<=>\$a} uniqint @ARGV); push @l, (\$l[0])x2; say join " ", @ARGV, "->", \$l[2]' 1 2 3 4 5 6
perl -MList::Util=uniqint -E '
@l=(sort {\$b<=>\$a} uniqint @ARGV); push @l, (\$l[0])x2; say join " ", @ARGV, "->", \$l[2]' 5
perl -MList::Util=uniqint -E '
@l=(sort {\$b<=>\$a} uniqint @ARGV); push @l, (\$l[0])x2; say join " ", @ARGV, "->", \$l[2]'
``````

Results:

``````1 2 3 4 5 6 -> 4
5 -> 5
->
``````

The full code is similar, with a couple of checks added.

`````` 1  # Perl weekly challenge 205
2  # Task 1:  Third Highest
3  #
4  # See https://wlmb.github.io/2023/02/20/PWC205/#task-1-third-highest
5  use v5.36;
6  use List::Util qw(uniqint);
7  die <<~"FIN" unless @ARGV;
8      Usage: \$0 N1 [N2...]
9      to find the third highest number from the list N1 N2...
10      FIN
11  my @list=(sort {\$b<=>\$a} uniqint @ARGV); # filter out repetitions and sort descending
12  push @list, (\$list[0]) x 2; # Add two copies of the maximum at the end of the list
13  say join " ", @ARGV, "->", \$list[2];
``````

Example:

``````./ch-1.pl 5 3 4
./ch-1.pl 5 6
./ch-1.pl 5 4 4 3
./ch-1.pl 1 2 3 4 5 6
./ch-1.pl 5
``````

Results:

``````5 3 4 -> 3
5 6 -> 6
5 4 4 3 -> 3
1 2 3 4 5 6 -> 4
5 -> 5
``````

# 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.

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.
``````

I use `combinations` from `Algorithm::Combinatorics` to generate all unordered pairs of elements of the array, I `xor` them and print the maximum. This yields a one liner:

``````perl -MAlgorithm::Combinatorics=combinations -MList::Util=max -E'
say join " ", @ARGV, "->", max map  {\$_->[0] ^ \$_->[1]} combinations([@ARGV],2)' 1 2 3 4 5 6 7
perl -MAlgorithm::Combinatorics=combinations -MList::Util=max -E'
say join " ", @ARGV, "->", max map  {\$_->[0] ^ \$_->[1]} combinations([@ARGV],2)' 2 4 1 3
perl -MAlgorithm::Combinatorics=combinations -MList::Util=max -E'
say join " ", @ARGV, "->", max map  {\$_->[0] ^ \$_->[1]} combinations([@ARGV],2)' 10 5 7 12 8
``````

Results:

``````1 2 3 4 5 6 7 -> 7
2 4 1 3 -> 7
10 5 7 12 8 -> 15
``````

The full code is similar:

`````` 1  # Perl weekly challenge 205
2  # Task 2:  Maximum XOR
3  #
4  # See https://wlmb.github.io/2023/02/20/PWC205/#task-2-maximum-xor
5  use v5.36;
6  use Algorithm::Combinatorics qw(combinations);
7  use List::Util qw(max);
8  die <<~"FIN" unless @ARGV >= 2;
9      Usage: \$0 N1 N2 [N3...]
10      to find the maximum xor of pairs from the list N1 N2 N3...
11      FIN
12  say join " ", @ARGV, "->", max map  {\$_->[0] ^ \$_->[1]} combinations([@ARGV],2);
``````

I don’t have to check that the inputs differ, as `n xor n = 0`. Example:

``````./ch-2.pl  1 2 3 4 5 6 7
./ch-2.pl  2 4 1 3
./ch-2.pl  10 5 7 12 8
``````

Results:

``````1 2 3 4 5 6 7 -> 7
2 4 1 3 -> 7
10 5 7 12 8 -> 15
``````
Written on February 20, 2023