diff options
| -rw-r--r-- | challenge-337/robbie-hatley/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-337/robbie-hatley/perl/ch-1.pl | 84 | ||||
| -rwxr-xr-x | challenge-337/robbie-hatley/perl/ch-2.pl | 108 |
3 files changed, 193 insertions, 0 deletions
diff --git a/challenge-337/robbie-hatley/blog.txt b/challenge-337/robbie-hatley/blog.txt new file mode 100644 index 0000000000..32ed292c37 --- /dev/null +++ b/challenge-337/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2025/09/robbie-hatleys-solutions-in-perl-for.html diff --git a/challenge-337/robbie-hatley/perl/ch-1.pl b/challenge-337/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..7720a641bd --- /dev/null +++ b/challenge-337/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,84 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 337-1, +written by Robbie Hatley on Wed Sep 03, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 337-1: Count LE Others +Submitted by: Mohammad Sajid Anwar +You are given an array of numbers, @num1. Write a script to +return an array, @num2, where $num2[$i] is the count of all +numbers in @num1 other than $num1[$i] which are less than or +equal to $num1[$i]. + +Example #1: +Input: @num1 = (6, 5, 4, 8) +Output: (2, 1, 0, 3) + +Example #2: +Input: @num1 = (7, 7, 7, 7) +Output: (3, 3, 3, 3) + +Example #3: +Input: @num1 = (5, 4, 3, 2, 1) +Output: (4, 3, 2, 1, 0) + +Example #4: +Input: @num1 = (-1, 0, 3, -2, 1) +Output: (1, 2, 4, 0, 3) + +Example #5: +Input: @num1 = (0, 1, 1, 2, 0) +Output: (1, 3, 3, 4, 1) + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: + +For each element of @num1, I'll use "grep" to find all elements of @num1 which are less-than-or-equal-to +the current element, then use "scalar" to count that list, then subtract 1 to disqualify the current element, +then push the resulting number to @num2. I'll then return @num2. + +-------------------------------------------------------------------------------------------------------------- +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 numbers, in proper Perl syntax, like so: + +./ch-1.pl '([-17.4, 6, 14, 0, -0.017, 3], [2, 4, 6, 8])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + # For each element of a referred-to array of numbers, return + # the count of elements, other than the current element itself, + # which are less-than-or-equal-to the current element: + sub count_le_others ($aref) { + my @num2; + for my $current (@$aref) { + push @num2, scalar(grep {$_<=$current} @$aref)-1} + @num2} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) + : ([6, 5, 4, 8], [7, 7, 7, 7], [5, 4, 3, 2, 1], [-1, 0, 3, -2, 1], [0, 1, 1, 2, 0]); +# Expected outputs : (2, 1, 0, 3), (3, 3, 3, 3), (4, 3, 2, 1, 0), ( 1, 2, 4, 0, 3), (1, 3, 3, 4, 1) + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + say ''; + say "Array = (@$aref)"; + my @counts = count_le_others($aref); + say "Counts of LE others = (@counts)"; +} diff --git a/challenge-337/robbie-hatley/perl/ch-2.pl b/challenge-337/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..60c02932bc --- /dev/null +++ b/challenge-337/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,108 @@ +#!/usr/bin/env perl + +=pod + +-------------------------------------------------------------------------------------------------------------- +TITLE AND ATTRIBUTION: +Solutions in Perl for The Weekly Challenge 337-2, +written by Robbie Hatley on Wed Sep 03, 2025. + +-------------------------------------------------------------------------------------------------------------- +PROBLEM DESCRIPTION: +Task 337-2: Count Odds +Submitted by: Mohammad Sajid Anwar +You are given the height h and width w of a matrix of zeros +and a list of 0-indexed positions (r,c) within the matrix. +Write a script which performs the following two action for +each given location [r,c]: +a) Increment by 1 all the cells on row r. +b) Increment by 1 all the cells on column c. +Then return the count of odd integers in the matrix. + +Example #1: +Input: $h = 2, $w = 3, @locations = ([0,1],[1,1]) +Output: 6 + +Example #2: +Input: $h = 2, $w = 2, @locations = ([1,1],[0,0]) +Output: 0 + +Example #3: +Input: $h = 3, $w = 3, @locations = ([0,0],[1,2],[2,1]) +Output: 0 + +Example #4: +Input: $h = 1, $w = 5, @locations = ([0,2],[0,4]) +Output: 2 + +Example #5: +Input: $h = 4, $w = 2, @locations = ([1,0],[3,1],[2,0],[0,1]) +Output: 8 + +-------------------------------------------------------------------------------------------------------------- +PROBLEM NOTES: +For a change, Task 2 in this challenge is much easier than Task 1, in that we simply need to do the things +mentioned in the program description: +1. Make a matrix of zeros of the given dimensions. +2. For each coordinate pair (r,c), increment row r and column c. +3. Count odds. + +-------------------------------------------------------------------------------------------------------------- +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, with each inner array consisting of a pair of positive integers followed by +an array of [x,y] coordinates, in proper Perl syntax, like so: + +./ch-2.pl '([3,7,[[0,0,],[1,1,],[2,2]]],[5,8,[[2,4],[5,7]]])' + +Output is to STDOUT and will be each input followed by the corresponding output. + +=cut + +# ------------------------------------------------------------------------------------------------------------ +# PRAGMAS, MODULES, AND SUBS: + + use v5.36; + # Perform given operations on given matrix of zeros, + # then return number of odd numbers: + sub count_odds ($h,$w,@p) { + my @matrix; + for(0..$h-1){ + push @matrix, [(0)x$w]} + for my $p (@p) { + # Increment row $p->[0]: + for (0..$w-1) { + ++$matrix[$p->[0]]->[$_]} + # Increment col $p->[1]: + for (0..$h-1) { + ++$matrix[$_]->[$p->[1]]}} + # Count odds: + my $co = 0; + for my $y (0..$h-1) { + for my $x (0..$w-1) { + ++$co if 0 != $matrix[$y]->[$x] % 2}} + # Return result: + $co} + +# ------------------------------------------------------------------------------------------------------------ +# INPUTS: +my @arrays = @ARGV ? eval($ARGV[0]) : +( + [2,3,[[0,1],[1,1] ]], # Expected output: 6 + [2,2,[[1,1],[0,0] ]], # Expected output: 0 + [3,3,[[0,0],[1,2],[2,1] ]], # Expected output: 0 + [1,5,[[0,2],[0,4] ]], # Expected output: 2 + [4,2,[[1,0],[3,1],[2,0],[0,1] ]], # Expected output: 8 +); + +# ------------------------------------------------------------------------------------------------------------ +# MAIN BODY OF PROGRAM: +$"=', '; +for my $aref (@arrays) { + my $h = $aref->[0]; + my $w = $aref->[1]; + my @p = @{$aref->[2]}; + my @s = map {"[@$_]"} @p; + my $c = count_odds($h,$w,@p); + say "h=$h, w=$w, positions=(@s), count-of-odds=$c"; +} |
