diff options
| author | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-02-21 00:34:32 -0800 |
|---|---|---|
| committer | robbie-hatley <Robbie.Hatley@gmail.com> | 2023-02-21 00:34:32 -0800 |
| commit | db67f42fe80703bfdb6ef77b96e2e1716bf22126 (patch) | |
| tree | b65ffa301bd999b61850a8db692b11c88a6ab811 | |
| parent | a7c004dd7466bb12a849f30ea7c107574be41c4b (diff) | |
| download | perlweeklychallenge-club-db67f42fe80703bfdb6ef77b96e2e1716bf22126.tar.gz perlweeklychallenge-club-db67f42fe80703bfdb6ef77b96e2e1716bf22126.tar.bz2 perlweeklychallenge-club-db67f42fe80703bfdb6ef77b96e2e1716bf22126.zip | |
Robbie Hatley's Perl Solutions for PWCC #205
| -rw-r--r-- | challenge-205/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-205/robbie-hatley/perl/ch-1.pl | 40 | ||||
| -rwxr-xr-x | challenge-205/robbie-hatley/perl/ch-2.pl | 62 |
3 files changed, 103 insertions, 0 deletions
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 example, +# to reshape the matrix ([5,8],[1,4]) from 2-by-2 to 4-by-1: +# ./ch-2.pl 2 2 5 8 1 4 4 1 +# +# NOTE: Output is to STDOUT and will be the original matrix, followed by +# the reshaped matrix (or 0 if the matrix can't be reshaped to +# the given shape). + +# PRELIMINARIES: +use v5.36; +$"=" "; + +# DEFAULT INPUTS: +my @arrays = +( + [1,2,3,4,5,6,7], + [2,4,1,3], + [10,5,7,12,8] +); + +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = ([@ARGV]);} + +# MAIN BODY OF SCRIPT: +ARRAY: for (@arrays){ + say ''; + my @array = @{$_}; + say "Array: (@array)"; + for (@array) {if ($_<0) {say "Error: negative integer found!"; next ARRAY;}} + my $curxor = 0; + my $maxxor = 0; + for ( my $i = 0 ; $i <= $#array - 1 ; ++$i ) { + for ( my $j = $i + 1 ; $i <= $#array - 0 ; ++$i ) { + $curxor = $array[$i] ^ $array[$j]; + if ($curxor > $maxxor) {$maxxor = $curxor}}} + say "max xor = $maxxor"}
\ No newline at end of file |
