aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLubos Kolouch <lubos@kolouch.net>2025-09-02 10:04:23 +0200
committerLubos Kolouch <lubos@kolouch.net>2025-09-02 10:04:23 +0200
commitd0aed856af9fde3fbbd9930691bef19acb845988 (patch)
tree85e3f5954dd15696fe48cc091b7840c7c57ad9be
parent99d8fa43930abb471fac2b94b68c0785619b37fc (diff)
downloadperlweeklychallenge-club-d0aed856af9fde3fbbd9930691bef19acb845988.tar.gz
perlweeklychallenge-club-d0aed856af9fde3fbbd9930691bef19acb845988.tar.bz2
perlweeklychallenge-club-d0aed856af9fde3fbbd9930691bef19acb845988.zip
feat(challenge-337): add solutions for week 337
-rw-r--r--challenge-337/lubos-kolouch/perl/ch-1.pl51
-rw-r--r--challenge-337/lubos-kolouch/perl/ch-2.pl70
-rw-r--r--challenge-337/lubos-kolouch/python/ch-1.py75
-rw-r--r--challenge-337/lubos-kolouch/python/ch-2.py66
4 files changed, 262 insertions, 0 deletions
diff --git a/challenge-337/lubos-kolouch/perl/ch-1.pl b/challenge-337/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..c60a33ebe5
--- /dev/null
+++ b/challenge-337/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Task 1: Smaller Than Current
+#
+# 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] in the array, excluding the number itself.
+#
+# 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)
+
+sub smaller_than_current {
+ my (@nums) = @_;
+ my @result;
+
+ for my $i ( 0 .. $#nums ) {
+ my $count = 0;
+ for my $j ( 0 .. $#nums ) {
+ if ( $i == $j ) {
+ next;
+ }
+ if ( $nums[$j] <= $nums[$i] ) {
+ $count++;
+ }
+ }
+ push @result, $count;
+ }
+
+ return @result;
+}
+
+use Test::More;
+
+is_deeply( [ smaller_than_current( 6, 5, 4, 8 ) ], [ 2, 1, 0, 3 ], 'Example 1' );
+is_deeply( [ smaller_than_current( 7, 7, 7, 7 ) ], [ 3, 3, 3, 3 ], 'Example 2' );
+is_deeply( [ smaller_than_current( 5, 4, 3, 2, 1 ) ], [ 4, 3, 2, 1, 0 ], 'Example 3' );
+is_deeply( [ smaller_than_current( -1, 0, 3, -2, 1 ) ], [ 1, 2, 4, 0, 3 ], 'Example 4' );
+is_deeply( [ smaller_than_current( 0, 1, 1, 2, 0 ) ], [ 1, 3, 3, 4, 1 ], 'Example 5' );
+
+done_testing();
diff --git a/challenge-337/lubos-kolouch/perl/ch-2.pl b/challenge-337/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..6254b8cbdb
--- /dev/null
+++ b/challenge-337/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Task 2: Odd Matrix
+#
+# 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
+#
+# Example 2
+# Input: $row = 2, $col = 2, @locations = ([1,1],[0,0])
+# Output: 0
+#
+# Example 3
+# Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1])
+# Output: 0
+
+sub odd_matrix {
+ my ( $row, $col, $locations ) = @_;
+ my @matrix;
+
+ # Initialize matrix
+ for my $i ( 0 .. $row - 1 ) {
+ for my $j ( 0 .. $col - 1 ) {
+ $matrix[$i][$j] = 0;
+ }
+ }
+
+ # Increment rows and columns
+ for my $loc (@$locations) {
+ my ( $r, $c ) = @$loc;
+ for my $i ( 0 .. $col - 1 ) {
+ $matrix[$r][$i]++;
+ }
+ for my $i ( 0 .. $row - 1 ) {
+ $matrix[$i][$c]++;
+ }
+ }
+
+ # Count odd cells
+ my $odd_cells = 0;
+ for my $i ( 0 .. $row - 1 ) {
+ for my $j ( 0 .. $col - 1 ) {
+ if ( $matrix[$i][$j] % 2 != 0 ) {
+ $odd_cells++;
+ }
+ }
+ }
+
+ return $odd_cells;
+}
+
+use Test::More;
+
+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 5' );
+
+done_testing();
diff --git a/challenge-337/lubos-kolouch/python/ch-1.py b/challenge-337/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..4b277e6e3c
--- /dev/null
+++ b/challenge-337/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,75 @@
+"""
+This script solves the Perl Weekly Challenge 337, Task 1.
+
+Task 1: Smaller Than Current
+
+You are given an array of numbers, `@num1`.
+
+Write a script to return an array, `@num2`, where `$num2[i]` is the count of all # noqa: E501
+numbers less than or equal to `$num1[i]` in the array,
+excluding the number itself.
+
+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)`
+"""
+
+import unittest
+
+
+def smaller_than_current(nums: list[int]) -> list[int]:
+ """
+ For each number in a list, count how many other numbers in the list are
+ smaller or equal.
+ """
+ result = []
+ for i, num in enumerate(nums):
+ count = 0
+ for j, other_num in enumerate(nums):
+ if i == j:
+ continue
+ if other_num <= num:
+ count += 1
+ result.append(count)
+ return result
+
+
+class TestSmallerThanCurrent(unittest.TestCase):
+
+ def test_example1(self):
+ self.assertEqual(smaller_than_current([6, 5, 4, 8]), [2, 1, 0, 3])
+
+ def test_example2(self):
+ self.assertEqual(smaller_than_current([7, 7, 7, 7]), [3, 3, 3, 3])
+
+ def test_example3(self):
+ self.assertEqual(smaller_than_current([5, 4, 3, 2, 1]),
+ [4, 3, 2, 1, 0])
+
+ def test_example4(self):
+ self.assertEqual(smaller_than_current([-1, 0, 3, -2, 1]),
+ [1, 2, 4, 0, 3])
+
+ def test_example5(self):
+ self.assertEqual(smaller_than_current([0, 1, 1, 2, 0]),
+ [1, 3, 3, 4, 1])
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/challenge-337/lubos-kolouch/python/ch-2.py b/challenge-337/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..c4a1e4babb
--- /dev/null
+++ b/challenge-337/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,66 @@
+"""
+This script solves the Perl Weekly Challenge 337, Task 2.
+
+Task 2: Odd Matrix
+
+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
+
+Example 2
+Input: `$row = 2, $col = 2, @locations = ([1,1],[0,0])`
+Output: 0
+
+Example 3
+Input: `$row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1])`
+Output: 0
+"""
+
+import unittest
+
+
+def odd_matrix(row: int, col: int, locations: list[list[int]]) -> int:
+ """
+ Given a matrix of size row x col, and a list of locations, increment the
+ row and column for each location and count the odd cells.
+ """
+ matrix = [[0 for _ in range(col)] for _ in range(row)]
+
+ for r, c in locations:
+ for i in range(col):
+ matrix[r][i] += 1
+ for i in range(row):
+ matrix[i][c] += 1
+
+ odd_cells = 0
+ for r in range(row):
+ for c in range(col):
+ if matrix[r][c] % 2 != 0:
+ odd_cells += 1
+
+ return odd_cells
+
+
+class TestOddMatrix(unittest.TestCase):
+
+ def test_example1(self):
+ self.assertEqual(odd_matrix(2, 3, [[0, 1], [1, 1]]), 6)
+
+ def test_example2(self):
+ self.assertEqual(odd_matrix(2, 2, [[1, 1], [0, 0]]), 0)
+
+ def test_example3(self):
+ self.assertEqual(odd_matrix(3, 3, [[0, 0], [1, 2], [2, 1]]), 0)
+
+
+if __name__ == '__main__':
+ unittest.main()