diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-29 12:13:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-29 12:13:25 +0100 |
| commit | 82994b3392979ae39fc31fd5c5895103133b2893 (patch) | |
| tree | 157e24a963e05b2bc5168867a4aa12fd4cf09592 | |
| parent | d43814b051a83eecb0f542c76511b729e5260784 (diff) | |
| parent | 908fe7807a19380ea27e1f995ca1ecf97bbc77ab (diff) | |
| download | perlweeklychallenge-club-82994b3392979ae39fc31fd5c5895103133b2893.tar.gz perlweeklychallenge-club-82994b3392979ae39fc31fd5c5895103133b2893.tar.bz2 perlweeklychallenge-club-82994b3392979ae39fc31fd5c5895103133b2893.zip | |
Merge pull request #10179 from robbie-hatley/rh271
Robbie Hatley's solutions in Perl for The Weekly Challenge #271.
| -rw-r--r-- | challenge-271/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-271/robbie-hatley/perl/ch-1.pl | 121 | ||||
| -rwxr-xr-x | challenge-271/robbie-hatley/perl/ch-2.pl | 107 |
3 files changed, 229 insertions, 0 deletions
diff --git a/challenge-271/robbie-hatley/blog.txt b/challenge-271/robbie-hatley/blog.txt new file mode 100644 index 0000000000..af22311372 --- /dev/null +++ b/challenge-271/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2024/05/robbie-hatleys-solutions-to-weekly_29.html
\ No newline at end of file diff --git a/challenge-271/robbie-hatley/perl/ch-1.pl b/challenge-271/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..d3da7b357f --- /dev/null +++ b/challenge-271/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,121 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 271-1, +written by Robbie Hatley on Tue May 28, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 271-1: Maximum Ones +Submitted by: Mohammad Sajid Anwar +You are given a m x n binary matrix. Write a script to return the +row number containing maximum ones. In case of more than one row, +then return smallest row number. + + # Example 1 input: + [ + [0, 1], + [1, 0], + ], + # Expected output: 1 + (Row 1 and Row 2 have the same number of ones, so return 1.) + + # Example 2 input: + [ + [0, 0, 0], + [1, 0, 1], + ], + # Expected output: 2 + (Row 2 has the maximum ones, so return 2.) + + # Example 3 input: + [ + [0, 0], + [1, 1], + [0, 0], + ], + # Expected output: 2 + (Row 2 have the maximum ones, so return 2.) + + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I'll attack this problem by sorting the row indices in reverse order of row sums, +then returning 1 + 0th element of sorted indices. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of binary matrices, in proper Perl syntax, like so: +./ch-1.pl '([[0,1],"pig"], [[0,1],[1,5]], [[0,0,1],[1,0,0],[0,0,0]], [[0,1,0],[1,0,0],[1,0,1]])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + use List::Util 'sum0'; + $" = ', '; + + # Return the 1-based index of the first row of a binary matrix with maximum number of "1" elements: + sub max_ones ($mref) {(sort {sum0(@{$$mref[$b]}) <=> sum0(@{$$mref[$a]}) || $a <=> $b} 0..$#$mref)[0]+1} + + # Is a given scalar a ref to an m x n binary matrix? + sub is_nonempty_binary_matrix ($matref) { + 'ARRAY' ne ref $matref and return 0; # Is $matref not an array ref? + scalar(@$matref) < 1 and return 0; # Is matrix empty? + for my $rowref (@$matref) { # For each row: + 'ARRAY' ne ref $rowref and return 0; # Is row not an array ref? + scalar(@$rowref) < 1 and return 0;} # Is row empty? + my $n = scalar(@{$$matref[0]}); # Length of first row. + for my $rowref (@$matref) { # For each row: + scalar(@$rowref) != $n and return 0; # Is row not same length as first? + for my $element (@$rowref) { # For each element of row: + '0' ne $element && '1' ne $element and return 0;}} # Is any element not binary? + return 1;} # Matrix is binary and rectangular. + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @matrices = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [ + [0, 1], + [1, 0], + ], + # Expected output: 1 + + # Example 2 input: + [ + [0, 0, 0], + [1, 0, 1], + ], + # Expected output: 2 + + # Example 3 input: + [ + [0, 0], + [1, 1], + [0, 0], + ], + # Expected output: 2 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $matref (@matrices) { + say ''; + !is_nonempty_binary_matrix ($matref) + and say 'Error: Matrix is not a non-empty binary matrix.' + and say 'Moving on to next matrix.' + and next; + say 'Matrix:'; + say "[@$_]" for @$matref; + say 'First row # with max ones = ', max_ones($matref); +} diff --git a/challenge-271/robbie-hatley/perl/ch-2.pl b/challenge-271/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..8585c1c127 --- /dev/null +++ b/challenge-271/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,107 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 271-2, +written by Robbie Hatley on Tue May 28, 2024. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 271-2: Sort by 1 bits +Submitted by: Mohammad Sajid Anwar +Given an array of non-negative integers, write a script to sort +the integers in ascending order by the number of 1 bits in their +binary representation. In case more than one integers have the +same number of 1 bits then sort them in ascending order. + + # Example 1 input: + [0, 1, 2, 3, 4, 5, 6, 7, 8], + # Expected output: (0, 1, 2, 4, 8, 3, 5, 6, 7) + 0 = 0 one bits + 1 = 1 one bits + 2 = 1 one bits + 4 = 1 one bits + 8 = 1 one bits + 3 = 2 one bits + 5 = 2 one bits + 6 = 2 one bits + 7 = 3 one bits + + # Example 2 input: + [1024, 512, 256, 128, 64], + # Expected output: (64, 128, 256, 512, 1024) + All integers in the given array have one 1-bits, + so just sort them in ascending order. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +I'll first make a sub "ones" that counts the number of "1" bits of a non-negative integer. +Then I'll make a sub "sort_by_ones" that sorts an array of non-negative integers primarily by ascending +number-of-ones and secondarily by ascending value. + +-------------------------------------------------------------------------------------------------------------- +IO NOTES: +Input is via either built-in variables or via @ARGV. If using @ARGV, provide one argument which must be a +single-quoted array of arrays of non-negative integers, in proper Perl syntax, like so: +./ch-2.pl '(["pig","cow"],[7,-2,6],[5,1,17,3,27,24],[7,8,9,10,11,12])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.38; + $" = ', '; + + # Return number of "1" digits in the binary + # representation of a non-negative integer: + sub ones ($x) { + my $ones = 0; + while ($x) { + 1 == $x%2 and ++$ones; + $x >>= 1;} + return $ones} + + # Sort an array of non-negative integers, + # primarily by number of ones, + # and secondarily by value: + sub sort_by_ones :prototype(@) (@a) { + sort {ones($a)<=>ones($b) || $a<=>$b} @a} + + sub is_array_of_nonneg_ints ($aref) { + 'ARRAY' ne ref $aref and return 0; + scalar(@$aref) < 1 and return 0; + for my $element (@$aref) { + $element !~ m/^0$|^[1-9]\d*$/ and return 0} + return 1} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + # Example 1 input: + [0, 1, 2, 3, 4, 5, 6, 7, 8], + # Expected output: (0, 1, 2, 4, 8, 3, 5, 6, 7) + + # Example 2 input: + [1024, 512, 256, 128, 64], + # Expected output: (64, 128, 256, 512, 1024) +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +for my $aref (@arrays) { + say ''; + !is_array_of_nonneg_ints($aref) + and say 'Error: Not an array of non-negative integers.' + and say 'Moving on to next array.' + and next; + my @a = @{$aref}; + say "Original array = (@a)"; + my @s = sort_by_ones(@a); + say "Sorted array = (@s)"; +} |
