diff options
| author | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-05-15 02:54:32 -0400 |
|---|---|---|
| committer | Matthew Neleigh <matthew.neleigh@gmail.com> | 2024-05-15 02:54:32 -0400 |
| commit | 1f4a110ed532a9dd7b54598ddbdf4de6ea18860f (patch) | |
| tree | 2943b74e07683f00db2227bbe516ade1b2524083 | |
| parent | 64b8c830c57e76dc96d88d66ead474e829ccb220 (diff) | |
| download | perlweeklychallenge-club-1f4a110ed532a9dd7b54598ddbdf4de6ea18860f.tar.gz perlweeklychallenge-club-1f4a110ed532a9dd7b54598ddbdf4de6ea18860f.tar.bz2 perlweeklychallenge-club-1f4a110ed532a9dd7b54598ddbdf4de6ea18860f.zip | |
new file: challenge-269/mattneleigh/perl/ch-1.pl
new file: challenge-269/mattneleigh/perl/ch-2.pl
| -rwxr-xr-x | challenge-269/mattneleigh/perl/ch-1.pl | 75 | ||||
| -rwxr-xr-x | challenge-269/mattneleigh/perl/ch-2.pl | 79 |
2 files changed, 154 insertions, 0 deletions
diff --git a/challenge-269/mattneleigh/perl/ch-1.pl b/challenge-269/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..58609e3028 --- /dev/null +++ b/challenge-269/mattneleigh/perl/ch-1.pl @@ -0,0 +1,75 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 1, 2, 3, 4, 5 ], + [ 2, 3, 8, 16 ], + [ 1, 2, 5, 7, 9 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: %s\n\n", + join(", ", @{$integer_list}), + bitwise_OR_least_sig_bit_possible(@{$integer_list}) ? + "true" + : + "false" + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine whether a list of positive integers contains at least two numbers +# that would, if bitwise-OR'd together, yield an integer with a zero in the +# least significant bit +# Takes one argument: +# * A list of positive integers to exmaine (e.g. ( 1, 2, 3, 4, 5 ) ) +# Returns: +# * 1 if there are at least two integers that, if bit-wise OR'd together, would +# yield a zero in the least-significant bit +# * 0 if there are NOT at least two integers that, if bit-wise OR'd together, +# would yield a zero in the least significant bit +################################################################################ +sub bitwise_OR_least_sig_bit_possible{ + + my $zeros = 0; + + # Loop over each integer- the secret is + # to count the zeros in the ones-place + # bit; if we find two of those we know + # to return true + foreach my $int (@ARG){ + # Add to the count of zeros if this int's + # ones-place bit is zero + $zeros++ + unless($int & 0x01); + + # If we have found more than one such + # int, return true + return(1) + if($zeros > 1) + } + + # Found zero or one ints with a zero in + # the ones-place bit + return(0); + +} + + + diff --git a/challenge-269/mattneleigh/perl/ch-2.pl b/challenge-269/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..8cfcd4c4e1 --- /dev/null +++ b/challenge-269/mattneleigh/perl/ch-2.pl @@ -0,0 +1,79 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @integer_lists = ( + [ 2, 1, 3, 4, 5 ], + [ 3, 2, 4 ], + [ 5, 4, 3 ,8 ] +); + +print("\n"); +foreach my $integer_list (@integer_lists){ + printf( + "Input: \@ints = (%s)\nOutput: (%s)\n\n", + join(", ", @{$integer_list}), + join(", ", redistribute_and_concatenate(@{$integer_list})) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Redistribute the contents of an array accordint to the following rules: +# 1) Place the first element from the given array to a new array, @arr1 +# 2) Place the second element from the given array to a new array, @arr2 +# 3) If the last element of @arr1 is greater than the last element of @arr2, +# place the next element of the given array into @array1 +# - or - +# place the next element of the given array into @array2 +# 4) If there are elements remaining in the given array, return to Rule 3 +# 5) Concatenate @arr1 and @arr2, and return the result +# Takes one argument: +# * The list of integers to process (e.g. ( 2, 1, 3, 4, 5 ) ) +# Returns: +# * A copy of the list, redistributed as described above (e.g. +# ( 2, 3, 4, 5, 1 ) ) +################################################################################ +sub redistribute_and_concatenate{ + + # Grab the first and second element of the + # given array and store them in the new + # arrays + my @arr1 = (shift()); + my @arr2 = (shift()); + + # Loop while we have elements left in the + # given array + while(@ARG){ + # Compare trailing array elements from each + # new array + if($arr1[$#arr1] > $arr2[$#arr2]){ + # Add the next element from the given array + # to array 1 + push(@arr1, shift()); + } else{ + # Add the next element from the given array + # to array 2 + push(@arr2, shift()); + } + } + + # Concatenate new arrays and return + return(@arr1, @arr2); + +} + + + |
