aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2025-09-01 12:57:56 +0000
committerNiels van Dijke <perlboy@cpan.org>2025-09-01 12:57:56 +0000
commitda4388091582a53a506e6b8e3cdee0dfe715946a (patch)
tree0ba2b3cfa612a5ffa10c5382dad53630667df0ca
parent6a75690bb1b4d8222f94c9b37f9644f5a61bd562 (diff)
downloadperlweeklychallenge-club-da4388091582a53a506e6b8e3cdee0dfe715946a.tar.gz
perlweeklychallenge-club-da4388091582a53a506e6b8e3cdee0dfe715946a.tar.bz2
perlweeklychallenge-club-da4388091582a53a506e6b8e3cdee0dfe715946a.zip
w337 - Task 1 & 2
-rwxr-xr-xchallenge-337/perlboy1967/perl/ch1.pl34
-rwxr-xr-xchallenge-337/perlboy1967/perl/ch2.pl47
2 files changed, 81 insertions, 0 deletions
diff --git a/challenge-337/perlboy1967/perl/ch1.pl b/challenge-337/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..24b46dd586
--- /dev/null
+++ b/challenge-337/perlboy1967/perl/ch1.pl
@@ -0,0 +1,34 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-337#TASK1>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Smaller Than Current
+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 less than or equal to $num1[i].
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+sub smallerThanCurrent (@ints) {
+ my %cache;
+ map { my $i = $_; $cache{$i} //= scalar(grep { $_ < $i } @ints); $cache{$i} } @ints;
+}
+
+is([smallerThanCurrent(6,5,4,8)],[2,1,0,3],'Example 1');
+is([smallerThanCurrent(7,7,7,7)],[0,0,0,0],'Example 2');
+is([smallerThanCurrent(5,4,3,2,1)],[4,3,2,1,0],'Example 3');
+is([smallerThanCurrent(-1,0,3,-2,1)],[1,2,4,0,3],'Example 4');
+is([smallerThanCurrent(0,2,2,4,0)],[0,2,2,4,0],'Example 5');
+is([smallerThanCurrent((1) x 10_000)],[(0) x 10_000],'Own example');
+
+done_testing;
diff --git a/challenge-337/perlboy1967/perl/ch2.pl b/challenge-337/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..d514247f4f
--- /dev/null
+++ b/challenge-337/perlboy1967/perl/ch2.pl
@@ -0,0 +1,47 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-337#TASK2>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Odd Matrix
+Submitted by: Mohammad Sajid Anwar
+
+You are given row and col, also a list of positions in the matrix.
+
+Write a script to perform action on each location (0-indexed) as provided in
+the list and find out the total odd valued cells.
+
+For each location (r, c), do both of the following:
+
+a) Increment by 1 all the cells on row r.
+b) Increment by 1 all the cells on column c.
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+use List::Util qw(sum0);
+
+sub oddMatrix ($row,$col,@locations) {
+ my @m = map { [(0) x $col] } (0 .. --$row);
+ $col--;
+
+ for (@locations) {
+ my ($r,$c) = @$_;
+ $m[$r][$_]++ for (0 .. $col);
+ $m[$_][$c]++ for (0 .. $row);
+ }
+
+ sum0 map { scalar grep { $_ % 2 } @$_ } @m;
+}
+
+is(oddMatrix(2,3,[0,1],[1,1]),6,'Example 1');
+is(oddMatrix(2,2,[1,1],[0,0]),0,'Example 2');
+is(oddMatrix(3,3,[0,0],[1,2],[2,1]),0,'Example 3');
+is(oddMatrix(1,5,[0,2],[0,4]),2,'Example 4');
+
+done_testing;