diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-09-01 20:14:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-01 20:14:25 +0100 |
| commit | e63b64bc5b4e95d7c52eddf829b4121089e61a96 (patch) | |
| tree | 4503506f9af3d7edbd77eb59f56cc89b21bb1e2a | |
| parent | c55807fef3c93f010efc531ed807dcaec4579a4b (diff) | |
| parent | 38e5076c843395910393daf36438813dd98f9b6a (diff) | |
| download | perlweeklychallenge-club-e63b64bc5b4e95d7c52eddf829b4121089e61a96.tar.gz perlweeklychallenge-club-e63b64bc5b4e95d7c52eddf829b4121089e61a96.tar.bz2 perlweeklychallenge-club-e63b64bc5b4e95d7c52eddf829b4121089e61a96.zip | |
Merge pull request #12612 from pjcs00/wk337
Week 337 - One and two dimensions
| -rw-r--r-- | challenge-337/peter-campbell-smith/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-337/peter-campbell-smith/perl/ch-1.pl | 32 | ||||
| -rwxr-xr-x | challenge-337/peter-campbell-smith/perl/ch-2.pl | 46 |
3 files changed, 79 insertions, 0 deletions
diff --git a/challenge-337/peter-campbell-smith/blog.txt b/challenge-337/peter-campbell-smith/blog.txt new file mode 100644 index 0000000000..a230b0fac6 --- /dev/null +++ b/challenge-337/peter-campbell-smith/blog.txt @@ -0,0 +1 @@ +http://ccgi.campbellsmiths.force9.co.uk/pwc/challenge/337 diff --git a/challenge-337/peter-campbell-smith/perl/ch-1.pl b/challenge-337/peter-campbell-smith/perl/ch-1.pl new file mode 100755 index 0000000000..ba20ea7ad5 --- /dev/null +++ b/challenge-337/peter-campbell-smith/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-09-01 +use utf8; # Week 337 - task 1 - Smaller than current +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +smaller_than_current(6, 5, 4, 8); +smaller_than_current(7, 7, 7, 7); +smaller_than_current(5, 4, 3, 2, 1); +smaller_than_current(-1, 0, 3, -2, 1); +smaller_than_current(0, 1, 1, 2, 0); + +sub smaller_than_current { + + my (@input, @output, $j); + + # initialise + @input = @_; + + # count elements smaller than this one + for $j (0 .. $#input) { + $output[$j] = 0; + $output[$j] ++ for grep { $_ < $input[$j] } @input; + } + + say qq[\nInput: (] . join(', ', @input) . ')'; + say qq[Output: (] . join(', ', @output) . ')' +} diff --git a/challenge-337/peter-campbell-smith/perl/ch-2.pl b/challenge-337/peter-campbell-smith/perl/ch-2.pl new file mode 100755 index 0000000000..4980b05321 --- /dev/null +++ b/challenge-337/peter-campbell-smith/perl/ch-2.pl @@ -0,0 +1,46 @@ +#!/usr/bin/perl + +# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge + +use v5.26; # The Weekly Challenge - 2025-09-01 +use utf8; # Week 337 - task 2 - Odd matrix +use warnings; # Peter Campbell Smith +binmode STDOUT, ':utf8'; +use Encode; + +odd_matrix(2, 3, [[0, 1], [1, 1]]); +odd_matrix(2, 2, [[1, 1], [0, 0]]); +odd_matrix(3, 3, [[0, 0], [1, 2], [2, 1]]); +odd_matrix(1, 5, [[0, 2], [0, 4]]); +odd_matrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]); +odd_matrix(5, 5, [[0, 0], [4, 4]]); + +sub odd_matrix { + + my ($rows, $cols, $locations, @cells, $cell, $row, $col, $legend, $input, $odds); + + # initialise + ($rows, $cols, $locations) = @_; + $cells[$_] = 0 for 0 .. $rows * $cols - 1; + + # loop over locations + for $cell (@$locations) { + ($row, $col) = @$cell; + + # increment row and col + $cells[$_ + $row * $cols] ++ for 0 .. $cols - 1; + $cells[$col + $_ * $cols] ++ for 0 .. $rows - 1; + $input .= qq{[$row, $col], }; + } + $odds = scalar(grep { $_ & 1 == 1} @cells); + + # report + say qq{\nInput: \$rows = $rows, \$cols = $cols, \@locations = [} . + substr($input, 0, -2) . ']'; + say qq[Output: $odds odd value] . ($odds == 1 ? '' : 's'); + for $row (0 .. $rows - 1) { + print ' [ '; + print $cells[$_ + $row * $cols] . ' ' for 0 .. $cols - 1; + say ']'; + } +} |
