diff options
| author | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2023-02-20 16:59:19 +0000 |
|---|---|---|
| committer | Peter Campbell Smith <pj.campbell.smith@gmail.com> | 2023-02-20 16:59:19 +0000 |
| commit | 4b8ff9d4b007d5393eaaf16fde8e731f620c7c70 (patch) | |
| tree | cffe08b0e9fdae57cc70160e7d18c7b453bac14d | |
| parent | a7c004dd7466bb12a849f30ea7c107574be41c4b (diff) | |
| download | perlweeklychallenge-club-4b8ff9d4b007d5393eaaf16fde8e731f620c7c70.tar.gz perlweeklychallenge-club-4b8ff9d4b007d5393eaaf16fde8e731f620c7c70.tar.bz2 perlweeklychallenge-club-4b8ff9d4b007d5393eaaf16fde8e731f620c7c70.zip | |
Week 205 submission
| -rw-r--r-- | challenge-205/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-205/peter-campbell-smith/perl/ch-1.pl | 54 | ||||
| -rwxr-xr-x | challenge-205/peter-campbell-smith/perl/ch-2.pl | 65 |
3 files changed, 120 insertions, 0 deletions
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.' + } +} |
