aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-09-02 12:13:58 +0100
committerGitHub <noreply@github.com>2025-09-02 12:13:58 +0100
commit348b9b48a81764f121698da3c759921bb0755b4b (patch)
treed0ecfbc425641ae0f7d7702d887d00b6bc5bcf3a
parente4d0f9454a6f578b434af64c92379a230d6d9048 (diff)
parent9138c349f4bf1fb4c1cce0a58ef17e33a2cd900f (diff)
downloadperlweeklychallenge-club-348b9b48a81764f121698da3c759921bb0755b4b.tar.gz
perlweeklychallenge-club-348b9b48a81764f121698da3c759921bb0755b4b.tar.bz2
perlweeklychallenge-club-348b9b48a81764f121698da3c759921bb0755b4b.zip
Merge pull request #12618 from LubosKolouch/master
Challenge 337 Solutions by Lubos Kolouch
-rw-r--r--challenge-336/README.md25
-rw-r--r--challenge-336/lubos-kolouch/README.md25
-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
6 files changed, 312 insertions, 0 deletions
diff --git a/challenge-336/README.md b/challenge-336/README.md
new file mode 100644
index 0000000000..8240b50d5a
--- /dev/null
+++ b/challenge-336/README.md
@@ -0,0 +1,25 @@
+# Perl Weekly Challenge 336
+
+## Task 1: Equal Group
+
+You are given an array of integers. Write a script to return true if the given array can be divided into one or more groups. Each group must be of the same size as the others, with at least two members, and all members within a group must have the same value.
+
+### Perl Solution (`ch-1.pl`)
+
+The Perl solution for Task 1, `ch-1.pl`, determines if an array of integers can be divided into equal groups. It first counts the frequency of each number in the input array. It then checks if all frequencies are at least 2. Finally, it calculates the greatest common divisor (GCD) of all frequencies. If the GCD is 2 or greater, it returns true, indicating that the array can be divided into groups of that size.
+
+### Python Solution (`ch-1.py`)
+
+The Python solution for Task 1, `ch-1.py`, mirrors the logic of its Perl counterpart. It uses `collections.Counter` to efficiently count element frequencies. Similar to the Perl solution, it ensures all frequencies are at least 2 and then computes the GCD of these frequencies using `math.gcd`. The function returns true if the calculated GCD is 2 or greater.
+
+## Task 2: Final Score
+
+You are given an array of scores by a team. Write a script to find the total score of the given team. The score can be any integer, '+', 'C', or 'D'. The '+' adds the sum of the previous two scores. The 'C' invalidates the previous score. The 'D' will double the previous score.
+
+### Perl Solution (`ch-2.pl`)
+
+The Perl solution for Task 2, `ch-2.pl`, calculates the final score based on a series of operations. It processes the input scores using a stack-like approach. Integers are pushed onto the stack. 'C' pops the last score, 'D' doubles the last score and pushes it, and '+' adds the last two scores and pushes the sum. The final total is the sum of all elements remaining in the stack.
+
+### Python Solution (`ch-2.py`)
+
+The Python solution for Task 2, `ch-2.py`, implements the same stack-based logic as the Perl version to calculate the final score. It iterates through the list of scores, performing operations based on the score type: appending integers, popping for 'C', doubling and appending for 'D', and summing the last two elements for '+'. The sum of the elements in the stack at the end represents the total score.
diff --git a/challenge-336/lubos-kolouch/README.md b/challenge-336/lubos-kolouch/README.md
new file mode 100644
index 0000000000..8240b50d5a
--- /dev/null
+++ b/challenge-336/lubos-kolouch/README.md
@@ -0,0 +1,25 @@
+# Perl Weekly Challenge 336
+
+## Task 1: Equal Group
+
+You are given an array of integers. Write a script to return true if the given array can be divided into one or more groups. Each group must be of the same size as the others, with at least two members, and all members within a group must have the same value.
+
+### Perl Solution (`ch-1.pl`)
+
+The Perl solution for Task 1, `ch-1.pl`, determines if an array of integers can be divided into equal groups. It first counts the frequency of each number in the input array. It then checks if all frequencies are at least 2. Finally, it calculates the greatest common divisor (GCD) of all frequencies. If the GCD is 2 or greater, it returns true, indicating that the array can be divided into groups of that size.
+
+### Python Solution (`ch-1.py`)
+
+The Python solution for Task 1, `ch-1.py`, mirrors the logic of its Perl counterpart. It uses `collections.Counter` to efficiently count element frequencies. Similar to the Perl solution, it ensures all frequencies are at least 2 and then computes the GCD of these frequencies using `math.gcd`. The function returns true if the calculated GCD is 2 or greater.
+
+## Task 2: Final Score
+
+You are given an array of scores by a team. Write a script to find the total score of the given team. The score can be any integer, '+', 'C', or 'D'. The '+' adds the sum of the previous two scores. The 'C' invalidates the previous score. The 'D' will double the previous score.
+
+### Perl Solution (`ch-2.pl`)
+
+The Perl solution for Task 2, `ch-2.pl`, calculates the final score based on a series of operations. It processes the input scores using a stack-like approach. Integers are pushed onto the stack. 'C' pops the last score, 'D' doubles the last score and pushes it, and '+' adds the last two scores and pushes the sum. The final total is the sum of all elements remaining in the stack.
+
+### Python Solution (`ch-2.py`)
+
+The Python solution for Task 2, `ch-2.py`, implements the same stack-based logic as the Perl version to calculate the final score. It iterates through the list of scores, performing operations based on the score type: appending integers, popping for 'C', doubling and appending for 'D', and summing the last two elements for '+'. The sum of the elements in the stack at the end represents the total score.
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()