aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-07 22:48:33 +0100
committerGitHub <noreply@github.com>2025-09-07 22:48:33 +0100
commit8030d9cca65d98d8f3d68877660636586bfba864 (patch)
tree8919152d3995b24bba7a2eaf75ad4e684f6fdde7
parent1e4fab6aa587989775539cd65045b2cefbc2407e (diff)
parent2d3042f644dc69633a9f07dc0352b44d4d3973c0 (diff)
downloadperlweeklychallenge-club-8030d9cca65d98d8f3d68877660636586bfba864.tar.gz
perlweeklychallenge-club-8030d9cca65d98d8f3d68877660636586bfba864.tar.bz2
perlweeklychallenge-club-8030d9cca65d98d8f3d68877660636586bfba864.zip
Merge pull request #12634 from wanderdoc/master
PWC 337 (wanderdoc)
-rw-r--r--challenge-337/wanderdoc/perl/ch-1.pl84
-rw-r--r--challenge-337/wanderdoc/perl/ch-2.pl269
2 files changed, 353 insertions, 0 deletions
diff --git a/challenge-337/wanderdoc/perl/ch-1.pl b/challenge-337/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..135d0cd3e9
--- /dev/null
+++ b/challenge-337/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,84 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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].
+
+Example 1
+
+Input: @num1 = (6, 5, 4, 8)
+Output: (2, 1, 0, 3)
+
+index 0: numbers <= 6 are 5, 4 => 2
+index 1: numbers <= 5 are 4 => 1
+index 2: numbers <= 4, none => 0
+index 3: numbers <= 8 are 6, 5, 4 => 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)
+
+=cut
+
+
+
+
+
+
+use List::Util qw(max);
+use Test2::V0 -no_srand => 1;
+
+is([smaller_or_equal_than_current(6, 5, 4, 8)], [2, 1, 0, 3], 'Example 1');
+is([smaller_or_equal_than_current(7, 7, 7, 7)], [3, 3, 3, 3], 'Example 2');
+is([smaller_or_equal_than_current(5, 4, 3, 2, 1)], [4, 3, 2, 1, 0], 'Example 3');
+is([smaller_or_equal_than_current(-1, 0, 3, -2, 1)], [1, 2, 4, 0, 3], 'Example 4');
+is([smaller_or_equal_than_current(0, 1, 1, 2, 0)], [1, 3, 3, 4, 1], 'Example 5');
+
+
+done_testing();
+
+sub smaller_or_equal_than_current
+{
+ my @arr = @_;
+ my %count;
+ my $level = scalar(@arr) - 1;
+ my $previous;
+ my $equals = 0;
+ for my $num ( sort {$b <=> $a} @arr ) # keys %idx )
+ {
+ if ( (defined $previous) and ($num < $previous))
+ {
+ $level -= max(1, $equals);
+ $equals = 1; # 0
+ }
+ else
+ {
+ $equals++;
+ }
+ $count{$num} = $level;
+ $previous = $num;
+ }
+ return @count{@arr};
+}
diff --git a/challenge-337/wanderdoc/perl/ch-2.pl b/challenge-337/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..b4ceca7eaf
--- /dev/null
+++ b/challenge-337/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,269 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+
+
+Example 1
+
+Input: $row = 2, $col = 3, @locations = ([0,1],[1,1])
+Output: 6
+
+Initial:
+[ 0 0 0 ]
+[ 0 0 0 ]
+
+Apply [0,1]:
+Increment row 0:
+Before After
+[ 0 0 0 ] [ 1 1 1 ]
+[ 0 0 0 ] [ 0 0 0 ]
+Increment col 1:
+Before After
+[ 1 1 1 ] [ 1 2 1 ]
+[ 0 0 0 ] [ 0 1 0 ]
+
+Apply [1,1]:
+Increment row 1:
+Before After
+[ 1 2 1 ] [ 1 2 1 ]
+[ 0 1 0 ] [ 1 2 1 ]
+Increment col 1:
+Before After
+[ 1 2 1 ] [ 1 3 1 ]
+[ 1 2 1 ] [ 1 3 1 ]
+
+Final:
+[ 1 3 1 ]
+[ 1 3 1 ]
+
+
+Example 2
+
+Input: $row = 2, $col = 2, @locations = ([1,1],[0,0])
+Output: 0
+
+Initial:
+[ 0 0 ]
+[ 0 0 ]
+
+Apply [1,1]:
+Increment row 1:
+Before After
+[ 0 0 ] [ 0 0 ]
+[ 0 0 ] [ 1 1 ]
+Increment col 1:
+Before After
+[ 0 0 ] [ 0 1 ]
+[ 1 1 ] [ 1 2 ]
+
+Apply [0,0]:
+Increment row 0:
+Before After
+[ 0 1 ] [ 1 2 ]
+[ 1 2 ] [ 1 2 ]
+Increment col 0:
+Before After
+[ 1 2 ] [ 2 2 ]
+[ 1 2 ] [ 2 2 ]
+
+Final:
+[ 2 2 ]
+[ 2 2 ]
+
+
+Example 3
+
+Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1])
+Output: 0
+
+Initial:
+[ 0 0 0 ]
+[ 0 0 0 ]
+[ 0 0 0 ]
+
+Apply [0,0]:
+Increment row 0:
+Before After
+[ 0 0 0 ] [ 1 1 1 ]
+[ 0 0 0 ] [ 0 0 0 ]
+[ 0 0 0 ] [ 0 0 0 ]
+Increment col 0:
+Before After
+[ 1 1 1 ] [ 2 1 1 ]
+[ 0 0 0 ] [ 1 0 0 ]
+[ 0 0 0 ] [ 1 0 0 ]
+
+Apply [1,2]:
+Increment row 1:
+Before After
+[ 2 1 1 ] [ 2 1 1 ]
+[ 1 0 0 ] [ 2 1 1 ]
+[ 1 0 0 ] [ 1 0 0 ]
+Increment col 2:
+Before After
+[ 2 1 1 ] [ 2 1 2 ]
+[ 2 1 1 ] [ 2 1 2 ]
+[ 1 0 0 ] [ 1 0 1 ]
+
+Apply [2,1]:
+Increment row 2:
+Before After
+[ 2 1 2 ] [ 2 1 2 ]
+[ 2 1 2 ] [ 2 1 2 ]
+[ 1 0 1 ] [ 2 1 2 ]
+Increment col 1:
+Before After
+[ 2 1 2 ] [ 2 2 2 ]
+[ 2 1 2 ] [ 2 2 2 ]
+[ 2 1 2 ] [ 2 2 2 ]
+
+Final:
+[ 2 2 2 ]
+[ 2 2 2 ]
+[ 2 2 2 ]
+
+
+Example 4
+
+Input: $row = 1, $col = 5, @locations = ([0,2],[0,4])
+Output: 2
+
+Initial:
+[ 0 0 0 0 0 ]
+
+Apply [0,2]:
+Increment row 0:
+Before After
+[ 0 0 0 0 0 ] [ 1 1 1 1 1 ]
+Increment col 2:
+Before After
+[ 1 1 1 1 1 ] [ 1 1 2 1 1 ]
+
+Apply [0,4]:
+Increment row 0:
+Before After
+[ 1 1 2 1 1 ] [ 2 2 3 2 2 ]
+Increment col 4:
+Before After
+[ 2 2 3 2 2 ] [ 2 2 3 2 3 ]
+
+Final:
+[ 2 2 3 2 3 ]
+
+
+Example 5
+
+Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1])
+Output: 8
+
+Initial:
+[ 0 0 ]
+[ 0 0 ]
+[ 0 0 ]
+[ 0 0 ]
+
+Apply [1,0]:
+Increment row 1:
+Before After
+[ 0 0 ] [ 0 0 ]
+[ 0 0 ] [ 1 1 ]
+[ 0 0 ] [ 0 0 ]
+[ 0 0 ] [ 0 0 ]
+Increment col 0:
+Before After
+[ 0 0 ] [ 1 0 ]
+[ 1 1 ] [ 2 1 ]
+[ 0 0 ] [ 1 0 ]
+[ 0 0 ] [ 1 0 ]
+
+Apply [3,1]:
+Increment row 3:
+Before After
+[ 1 0 ] [ 1 0 ]
+[ 2 1 ] [ 2 1 ]
+[ 1 0 ] [ 1 0 ]
+[ 1 0 ] [ 2 1 ]
+Increment col 1:
+Before After
+[ 1 0 ] [ 1 1 ]
+[ 2 1 ] [ 2 2 ]
+[ 1 0 ] [ 1 1 ]
+[ 2 1 ] [ 2 2 ]
+
+Apply [2,0]:
+Increment row 2:
+Before After
+[ 1 1 ] [ 1 1 ]
+[ 2 2 ] [ 2 2 ]
+[ 1 1 ] [ 2 2 ]
+[ 2 2 ] [ 2 2 ]
+Increment col 0:
+Before After
+[ 1 1 ] [ 2 1 ]
+[ 2 2 ] [ 3 2 ]
+[ 2 2 ] [ 3 2 ]
+[ 2 2 ] [ 3 2 ]
+
+Apply [0,1]:
+Increment row 0:
+Before After
+[ 2 1 ] [ 3 2 ]
+[ 3 2 ] [ 3 2 ]
+[ 3 2 ] [ 3 2 ]
+[ 3 2 ] [ 3 2 ]
+Increment col 1:
+Before After
+[ 3 2 ] [ 3 3 ]
+[ 3 2 ] [ 3 3 ]
+[ 3 2 ] [ 3 3 ]
+[ 3 2 ] [ 3 3 ]
+
+Final:
+[ 3 3 ]
+[ 3 3 ]
+[ 3 3 ]
+[ 3 3 ]
+
+=cut
+
+use PDL; # Run with strawberry-perl-5.40.0.1-64bit-PDL
+use PDL::NiceSlice;
+use PDL::Primitive;
+
+use Test2::V0 -no_srand => 1;
+
+is(odd_matrix(2, 3, [[0,1],[1,1]]), 6, 'Example 1' );
+is(odd_matrix(2, 2, [[1,1],[0,0]]), 0, 'Example 2' );
+is(odd_matrix(3, 3, [[0,0],[1,2],[2,1]]), 0, 'Example 3' );
+is(odd_matrix(1, 5, [[0,2],[0,4]]), 2, 'Example 4' );
+is(odd_matrix(4, 2, [[1,0],[3,1],[2,0],[0,1]]), 8, 'Example 2' );
+done_testing();
+
+sub odd_matrix
+{
+ my ($row, $col, $locations) = @_;
+ my $pdl = zeroes($col, $row);
+
+ for my $elm ( @$locations)
+ {
+ my $row = $elm->[0];
+ my $slice = $pdl(, $row);
+ $slice++;
+ my $col = $elm->[1];
+ $slice = $pdl($col,);
+ $slice++;
+ }
+ # print $pdl;
+ my ($even, $odd) = which_both( ($pdl % 2) == 0); # $idx is now 1D
+ # look at whichND_both too.
+ return $odd->getdim(0);
+}