diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-05 20:28:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-05 20:28:41 +0100 |
| commit | 5a7f25b9ae4756ca27e3f72a4d42d437c28ba5d9 (patch) | |
| tree | f1a6075075ceb12b2ee123f8d156b3bddaa82b76 | |
| parent | e3a3db8e940a41a2bb87b9d3654aed33f94da7fd (diff) | |
| parent | 0f138c1640910cd77fc3018a27c8081bbfa46b39 (diff) | |
| download | perlweeklychallenge-club-5a7f25b9ae4756ca27e3f72a4d42d437c28ba5d9.tar.gz perlweeklychallenge-club-5a7f25b9ae4756ca27e3f72a4d42d437c28ba5d9.tar.bz2 perlweeklychallenge-club-5a7f25b9ae4756ca27e3f72a4d42d437c28ba5d9.zip | |
Merge pull request #10775 from pme/challenge-205
challenge-205
| -rwxr-xr-x | challenge-205/peter-meszaros/perl/ch-1.pl | 61 | ||||
| -rwxr-xr-x | challenge-205/peter-meszaros/perl/ch-2.pl | 65 |
2 files changed, 126 insertions, 0 deletions
diff --git a/challenge-205/peter-meszaros/perl/ch-1.pl b/challenge-205/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..c97f3f6864 --- /dev/null +++ b/challenge-205/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @array = (5,3,4) + Output: 3 + + First highest is 5. Second highest is 4. Third highest is 3. + +=head2 Example 2 + + Input: @array = (5,6) + Output: 6 + + First highest is 6. Second highest is 5. Third highest is missing, so maximum is returned. + +=head2 Example 3 + + Input: @array = (5,4,4,3) + Output: 3 + + First highest is 5. Second highest is 4. Third highest is 3. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[5, 3, 4], 3, 'Example 1'], + [[5, 6], 6, 'Example 2'], + [[5, 4, 4, 3], 3, 'Example 3'], +]; + +sub third_highest +{ + my $l = shift; + + my %l = map {$_ => 1} @$l; + my @l = sort {$b <=> $a} keys %l; + + return $l[2] // $l[0]; +} + +for (@$cases) { + is(third_highest($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-205/peter-meszaros/perl/ch-2.pl b/challenge-205/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..f85cca21da --- /dev/null +++ b/challenge-205/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/env perl +# +=head1 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. + +=head2 Example 1 + + Input: @array = (1,2,3,4,5,6,7) + Output: 7 + + The maximum result of 1 xor 6 = 7. + +=head2 Example 2 + + Input: @array = (2,4,1,3) + Output: 7 + + The maximum result of 4 xor 3 = 7. + +=head2 Example 3 + + Input: @array = (10,5,7,12,8) + Output: 15 + + The maximum result of 10 xor 5 = 15. + +=cut + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use Algorithm::Combinatorics qw/combinations/; + +my $cases = [ + [[ 1, 2, 3, 4, 5, 6, 7], 7, 'Example 1'], + [[ 2, 4, 1, 3], 7, 'Example 2'], + [[10, 5, 7, 12, 8], 15, 'Example 3'], +]; + +sub maximum_xor +{ + my $l = shift; + + my $max = 0; + my $iter = combinations($l, 2); + while (my $c = $iter->next) { + my $v = $c->[0] ^ $c->[1]; + $max = $v if $v > $max; + } + return $max; +} + +for (@$cases) { + is(maximum_xor($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; |
