From cda29f36a8a42a7dad3b67cbad7f965113b0db1c Mon Sep 17 00:00:00 2001 From: Conor Hoekstra Date: Mon, 1 Sep 2025 16:57:24 -0400 Subject: :sparkles: Week 337 in BQN --- challenge-337/conor-hoekstra/ch-1.bqn | 13 +++++++++++++ challenge-337/conor-hoekstra/ch-2.bqn | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 challenge-337/conor-hoekstra/ch-1.bqn create mode 100644 challenge-337/conor-hoekstra/ch-2.bqn diff --git a/challenge-337/conor-hoekstra/ch-1.bqn b/challenge-337/conor-hoekstra/ch-1.bqn new file mode 100644 index 0000000000..3072130e2f --- /dev/null +++ b/challenge-337/conor-hoekstra/ch-1.bqn @@ -0,0 +1,13 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/337-1.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +SmallerThanCurrent ← ¯1+·+´˘≥⌜˜ + +# Tests +u.UnitTest (SmallerThanCurrent ⟨6, 5, 4, 8⟩) ≡ ⟨2, 1, 0, 3⟩ +u.UnitTest (SmallerThanCurrent ⟨7, 7, 7, 7⟩) ≡ ⟨3, 3, 3, 3⟩ +u.UnitTest (SmallerThanCurrent ⟨5, 4, 3, 2, 1⟩) ≡ ⟨4, 3, 2, 1, 0⟩ +u.UnitTest (SmallerThanCurrent ⟨¯1, 0, 3, ¯2, 1⟩) ≡ ⟨1, 2, 4, 0, 3⟩ +u.UnitTest (SmallerThanCurrent ⟨0, 1, 1, 2, 0⟩) ≡ ⟨1, 3, 3, 4, 1⟩ diff --git a/challenge-337/conor-hoekstra/ch-2.bqn b/challenge-337/conor-hoekstra/ch-2.bqn new file mode 100644 index 0000000000..b0e2ef0014 --- /dev/null +++ b/challenge-337/conor-hoekstra/ch-2.bqn @@ -0,0 +1,16 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/337-2.bqn + +u ⇐ •Import "/home/cph/bqn-test/test.bqn" + +Row ← { 1⊸+⌾(𝕨⊸⊏)𝕩 } +Col ← ⍉Row⟜⍉ +RowCol ← { r‿c𝕊𝕩: r Row c Col 𝕩 } +OddMatrix ← { +´2|⥊(𝕨⥊0) RowCol´ 𝕩 } + +# Tests +u.UnitTest (2‿3 OddMatrix ⟨⟨0,1⟩,⟨1,1⟩⟩) ≡ 6 +u.UnitTest (2‿2 OddMatrix ⟨⟨1,1⟩,⟨0,0⟩⟩) ≡ 0 +u.UnitTest (3‿3 OddMatrix ⟨⟨0,0⟩,⟨1,2⟩,⟨2,1⟩⟩) ≡ 0 +u.UnitTest (1‿5 OddMatrix ⟨⟨0,2⟩,⟨0,4⟩⟩) ≡ 2 +u.UnitTest (4‿2 OddMatrix ⟨⟨1,0⟩,⟨3,1⟩,⟨2,0⟩,⟨0,1⟩⟩) ≡ 8 -- cgit From e27044230c74d7769afbb6423d15a2c37c787e9b Mon Sep 17 00:00:00 2001 From: Mark Anderson Date: Mon, 1 Sep 2025 22:05:41 +0000 Subject: ch-1.raku touch-up --- challenge-337/mark-anderson/raku/ch-1.raku | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-337/mark-anderson/raku/ch-1.raku b/challenge-337/mark-anderson/raku/ch-1.raku index c10832763b..087445c1c1 100644 --- a/challenge-337/mark-anderson/raku/ch-1.raku +++ b/challenge-337/mark-anderson/raku/ch-1.raku @@ -9,7 +9,7 @@ is-deeply stc(0,1,1,2,0), (0,2,2,4,0); sub stc(+@nums) { - my %bag is Bag = @nums.Bag; + my %bag is Bag = @nums; my @keys = %bag.keys.sort; my %map is Map = @keys Z=> [\+] 0, |%bag{@keys}; %map{@nums} -- cgit From d0aed856af9fde3fbbd9930691bef19acb845988 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 2 Sep 2025 10:04:23 +0200 Subject: feat(challenge-337): add solutions for week 337 --- challenge-337/lubos-kolouch/perl/ch-1.pl | 51 ++++++++++++++++++++ challenge-337/lubos-kolouch/perl/ch-2.pl | 70 ++++++++++++++++++++++++++++ challenge-337/lubos-kolouch/python/ch-1.py | 75 ++++++++++++++++++++++++++++++ challenge-337/lubos-kolouch/python/ch-2.py | 66 ++++++++++++++++++++++++++ 4 files changed, 262 insertions(+) create mode 100644 challenge-337/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-337/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-337/lubos-kolouch/python/ch-1.py create mode 100644 challenge-337/lubos-kolouch/python/ch-2.py 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() -- cgit From c226f0e71a8bbc1be72821a0327a7cc5c4b0e662 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 2 Sep 2025 10:05:41 +0200 Subject: docs(challenge-337): add README for solutions --- challenge-337/lubos-kolouch/README | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-337/lubos-kolouch/README diff --git a/challenge-337/lubos-kolouch/README b/challenge-337/lubos-kolouch/README new file mode 100644 index 0000000000..921b2d9f4a --- /dev/null +++ b/challenge-337/lubos-kolouch/README @@ -0,0 +1 @@ +Solutions by Lubos Kolouch. -- cgit From df4de4c0477df8718c5da9d094bde6ad30a43066 Mon Sep 17 00:00:00 2001 From: Pok Date: Tue, 2 Sep 2025 15:14:11 +0700 Subject: pwc337 solution in python --- challenge-337/pokgopun/python/ch-1.py | 67 +++++++++ challenge-337/pokgopun/python/ch-2.py | 273 ++++++++++++++++++++++++++++++++++ 2 files changed, 340 insertions(+) create mode 100644 challenge-337/pokgopun/python/ch-1.py create mode 100644 challenge-337/pokgopun/python/ch-2.py diff --git a/challenge-337/pokgopun/python/ch-1.py b/challenge-337/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..eb7b906a2d --- /dev/null +++ b/challenge-337/pokgopun/python/ch-1.py @@ -0,0 +1,67 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +""" + +Task 1: Smaller Than Current + +Submitted by: [40]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]. + +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) + +Task 2: Odd Matrix +""" +### solution by pokgopun@gmail.com + +def stc(ints: tuple[int]) -> int: + l = len(ints) + return tuple( + sum(1 for i in range(l) if i != j and ints[i] <= ints[j]) for j in range(l) + ) + +import unittest + +class TestStc(unittest.TestCase): + def test(self): + for inpt, otpt in { + (6, 5, 4, 8): (2, 1, 0, 3), + (7, 7, 7, 7): (3, 3, 3, 3), + (5, 4, 3, 2, 1): (4, 3, 2, 1, 0), + (-1, 0, 3, -2, 1): (1, 2, 4, 0, 3), + (0, 1, 1, 2, 0): (1, 3, 3, 4, 1), + }.items(): + self.assertEqual(stc(inpt),otpt) + +unittest.main() diff --git a/challenge-337/pokgopun/python/ch-2.py b/challenge-337/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..1aaef66a6d --- /dev/null +++ b/challenge-337/pokgopun/python/ch-2.py @@ -0,0 +1,273 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +""" + +Task 2: Odd Matrix + +Submitted by: [41]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. + +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 ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 7th September + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from dataclasses import dataclass + +@dataclass +class Position: + r: int + c: int + +def om(row: int, col: int, positions: tuple[Position]) -> int: + p2c: dict[int,int] = {} + for p in positions: + p2c[p.r] = p2c.get(p.r,0) + 1 + p2c[-1-p.c] = p2c.get(-1-p.c,0) + 1 + #print(p2c) + cnto = 0 + for r in range(row): + for c in range(col): + if (p2c.get(r,0)+p2c.get(-1-c,0)) % 2 == 1: + cnto += 1 + return cnto + +import unittest + +class TestOm(unittest.TestCase): + def test(self): + for (row, col, locations), otpt in { + (2, 3, ((0,1),(1,1))): 6, + (2, 2, ((1,1),(0,0))): 0, + (3, 3, ((0,0),(1,2),(2,1))): 0, + (1, 5, ((0,2),(0,4))): 2, + (4, 2, ((1,0),(3,1),(2,0),(0,1))): 8, + }.items(): + #print(row, col, locations, otpt) + self.assertEqual(om(row, col, tuple(Position(e[0],e[1]) for e in locations)), otpt) + +unittest.main() -- cgit From 9138c349f4bf1fb4c1cce0a58ef17e33a2cd900f Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Tue, 2 Sep 2025 10:22:07 +0200 Subject: feat(challenge-336): Add README for lubos-kolouch solutions --- challenge-336/README.md | 25 +++++++++++++++++++++++++ challenge-336/lubos-kolouch/README.md | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 challenge-336/README.md create mode 100644 challenge-336/lubos-kolouch/README.md 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. -- cgit From 926bc4fc0727638b16384a11905db0c9fe6d6893 Mon Sep 17 00:00:00 2001 From: Pok Date: Tue, 2 Sep 2025 17:22:04 +0700 Subject: pwc337 solution in go --- challenge-337/pokgopun/go/ch-1.go | 86 +++++++++++ challenge-337/pokgopun/go/ch-2.go | 294 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 380 insertions(+) create mode 100644 challenge-337/pokgopun/go/ch-1.go create mode 100644 challenge-337/pokgopun/go/ch-2.go diff --git a/challenge-337/pokgopun/go/ch-1.go b/challenge-337/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..352fa976cd --- /dev/null +++ b/challenge-337/pokgopun/go/ch-1.go @@ -0,0 +1,86 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +/*# + +Task 1: Smaller Than Current + +Submitted by: [40]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]. + +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) + +Task 2: Odd Matrix +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (in ints) process() ints { + l := len(in) + s := make(ints, l) + for i, v := range in { + j := l + for j > 0 { + j-- + if j != i && in[j] <= v { + s[i]++ + } + } + } + return s +} + +func main() { + for _, data := range []struct { + input, output ints + }{ + {ints{6, 5, 4, 8}, ints{2, 1, 0, 3}}, + {ints{7, 7, 7, 7}, ints{3, 3, 3, 3}}, + {ints{5, 4, 3, 2, 1}, ints{4, 3, 2, 1, 0}}, + {ints{-1, 0, 3, -2, 1}, ints{1, 2, 4, 0, 3}}, + {ints{0, 1, 1, 2, 0}, ints{1, 3, 3, 4, 1}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-337/pokgopun/go/ch-2.go b/challenge-337/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..e30b1a06bb --- /dev/null +++ b/challenge-337/pokgopun/go/ch-2.go @@ -0,0 +1,294 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +/*# + +Task 2: Odd Matrix + +Submitted by: [41]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. + +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 ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 7th September + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type pos struct { + r, c int +} + +type locations []pos + +type Input struct { + row, col int + locations locations +} + +func (in Input) process() int { + m := make(map[int]int) + for _, v := range in.locations { + m[v.r]++ + m[-1-v.c]++ + } + cnto := 0 + for r := 0; r < in.row; r++ { + for c := 0; c < in.col; c++ { + if (m[r]+m[-1-c])%2 == 1 { + cnto++ + } + } + } + return cnto +} + +func main() { + for _, data := range []struct { + input Input + output int + }{ + {Input{2, 3, locations{pos{0, 1}, pos{1, 1}}}, 6}, + {Input{2, 2, locations{pos{1, 1}, pos{0, 0}}}, 0}, + {Input{3, 3, locations{pos{0, 0}, pos{1, 2}, pos{2, 1}}}, 0}, + {Input{1, 5, locations{pos{0, 2}, pos{0, 4}}}, 2}, + {Input{4, 2, locations{pos{1, 0}, pos{3, 1}, pos{2, 0}, pos{0, 1}}}, 8}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} -- cgit From d9c3e9bfe9042e96da151b666faa28e08a2bb5dc Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Tue, 2 Sep 2025 12:19:33 +0100 Subject: - Added solutions by Conor Hoekstra. - Added solutions by Mark Anderson. - Added solutions by Lubos Kolouch. - Added solutions by PokGoPun. --- stats/pwc-current.json | 17 ++++++++++++++++- stats/pwc-language-breakdown-2019.json | 2 +- stats/pwc-language-breakdown-2020.json | 2 +- stats/pwc-language-breakdown-2021.json | 2 +- stats/pwc-language-breakdown-2022.json | 2 +- stats/pwc-language-breakdown-2023.json | 2 +- stats/pwc-language-breakdown-2024.json | 2 +- stats/pwc-language-breakdown-2025.json | 6 +++--- stats/pwc-language-breakdown-summary.json | 4 ++-- stats/pwc-leaders.json | 6 +++--- stats/pwc-summary-1-30.json | 2 +- stats/pwc-summary-121-150.json | 2 +- stats/pwc-summary-151-180.json | 4 ++-- stats/pwc-summary-181-210.json | 2 +- stats/pwc-summary-211-240.json | 2 +- stats/pwc-summary-241-270.json | 2 +- stats/pwc-summary-271-300.json | 2 +- stats/pwc-summary-301-330.json | 2 +- stats/pwc-summary-31-60.json | 2 +- stats/pwc-summary-61-90.json | 2 +- stats/pwc-summary-91-120.json | 2 +- stats/pwc-summary.json | 4 ++-- stats/pwc-yearly-language-summary.json | 6 +++--- 23 files changed, 47 insertions(+), 32 deletions(-) diff --git a/stats/pwc-current.json b/stats/pwc-current.json index 5735aace70..c7024806ec 100644 --- a/stats/pwc-current.json +++ b/stats/pwc-current.json @@ -34,6 +34,16 @@ "id" : "Feng Chang", "name" : "Feng Chang" }, + { + "data" : [ + [ + "Perl", + 2 + ] + ], + "id" : "Lubos Kolouch", + "name" : "Lubos Kolouch" + }, { "data" : [ [ @@ -163,6 +173,11 @@ "name" : "Feng Chang", "y" : 2 }, + { + "drilldown" : "Lubos Kolouch", + "name" : "Lubos Kolouch", + "y" : 2 + }, { "drilldown" : "Mark Anderson", "name" : "Mark Anderson", @@ -208,7 +223,7 @@ } ], "subtitle" : { - "text" : "[Champions: 11] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 12] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge - 337" diff --git a/stats/pwc-language-breakdown-2019.json b/stats/pwc-language-breakdown-2019.json index 977ed42c13..21f977aad5 100644 --- a/stats/pwc-language-breakdown-2019.json +++ b/stats/pwc-language-breakdown-2019.json @@ -970,7 +970,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2020.json b/stats/pwc-language-breakdown-2020.json index 9074cf7670..e0d15f3919 100644 --- a/stats/pwc-language-breakdown-2020.json +++ b/stats/pwc-language-breakdown-2020.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2021.json b/stats/pwc-language-breakdown-2021.json index 3f7ef44117..9ea9ba5353 100644 --- a/stats/pwc-language-breakdown-2021.json +++ b/stats/pwc-language-breakdown-2021.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2022.json b/stats/pwc-language-breakdown-2022.json index 375c98566d..fe4f06b812 100644 --- a/stats/pwc-language-breakdown-2022.json +++ b/stats/pwc-language-breakdown-2022.json @@ -1223,7 +1223,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2023.json b/stats/pwc-language-breakdown-2023.json index 88d16016c2..8ad25a9894 100644 --- a/stats/pwc-language-breakdown-2023.json +++ b/stats/pwc-language-breakdown-2023.json @@ -1200,7 +1200,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2024.json b/stats/pwc-language-breakdown-2024.json index 04de394ea1..48ffd56f52 100644 --- a/stats/pwc-language-breakdown-2024.json +++ b/stats/pwc-language-breakdown-2024.json @@ -1246,7 +1246,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-2025.json b/stats/pwc-language-breakdown-2025.json index 6559bedd3f..e747b8a104 100644 --- a/stats/pwc-language-breakdown-2025.json +++ b/stats/pwc-language-breakdown-2025.json @@ -8,7 +8,7 @@ "data" : [ [ "Perl", - 16 + 18 ], [ "Raku", @@ -655,7 +655,7 @@ { "drilldown" : "337", "name" : "337", - "y" : 27 + "y" : 29 }, { "drilldown" : "336", @@ -832,7 +832,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" diff --git a/stats/pwc-language-breakdown-summary.json b/stats/pwc-language-breakdown-summary.json index 30c46230ea..28af8892dd 100644 --- a/stats/pwc-language-breakdown-summary.json +++ b/stats/pwc-language-breakdown-summary.json @@ -10,7 +10,7 @@ "data" : [ [ "Perl", - 17356 + 17358 ], [ "Raku", @@ -37,7 +37,7 @@ } ], "subtitle" : { - "text" : "Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Contributions [2019 - 2025]" diff --git a/stats/pwc-leaders.json b/stats/pwc-leaders.json index ddf19e67c8..a66502b50c 100644 --- a/stats/pwc-leaders.json +++ b/stats/pwc-leaders.json @@ -244,7 +244,7 @@ "data" : [ [ "Perl", - 559 + 561 ], [ "Raku", @@ -867,7 +867,7 @@ { "drilldown" : "Lubos Kolouch", "name" : "15: Lubos Kolouch", - "y" : 1288 + "y" : 1292 }, { "drilldown" : "Simon Green", @@ -1049,7 +1049,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the score breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the score breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "Team Leaders (TOP 50)" diff --git a/stats/pwc-summary-1-30.json b/stats/pwc-summary-1-30.json index a307031bf2..7d8166f7e4 100644 --- a/stats/pwc-summary-1-30.json +++ b/stats/pwc-summary-1-30.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-121-150.json b/stats/pwc-summary-121-150.json index 730d70b407..ff38724817 100644 --- a/stats/pwc-summary-121-150.json +++ b/stats/pwc-summary-121-150.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-151-180.json b/stats/pwc-summary-151-180.json index 628a04407d..6085cd344a 100644 --- a/stats/pwc-summary-151-180.json +++ b/stats/pwc-summary-151-180.json @@ -27,7 +27,7 @@ 17, 2, 0, - 559, + 561, 0, 0, 0, @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-181-210.json b/stats/pwc-summary-181-210.json index 15fa5671f9..3ca9645ee0 100644 --- a/stats/pwc-summary-181-210.json +++ b/stats/pwc-summary-181-210.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-211-240.json b/stats/pwc-summary-211-240.json index bbeb450bac..bc4bebb567 100644 --- a/stats/pwc-summary-211-240.json +++ b/stats/pwc-summary-211-240.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-241-270.json b/stats/pwc-summary-241-270.json index 8d1b52e77e..3b2e7d03ed 100644 --- a/stats/pwc-summary-241-270.json +++ b/stats/pwc-summary-241-270.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-271-300.json b/stats/pwc-summary-271-300.json index 7dc7128917..0b8804e26b 100644 --- a/stats/pwc-summary-271-300.json +++ b/stats/pwc-summary-271-300.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-301-330.json b/stats/pwc-summary-301-330.json index 5a4091e5f0..5c23e47f95 100644 --- a/stats/pwc-summary-301-330.json +++ b/stats/pwc-summary-301-330.json @@ -109,7 +109,7 @@ } ], "subtitle" : { - "text" : "[Champions: 28] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 28] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-31-60.json b/stats/pwc-summary-31-60.json index 28803a3d4c..6978bd22db 100644 --- a/stats/pwc-summary-31-60.json +++ b/stats/pwc-summary-31-60.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-61-90.json b/stats/pwc-summary-61-90.json index d39987153b..09aa27c151 100644 --- a/stats/pwc-summary-61-90.json +++ b/stats/pwc-summary-61-90.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary-91-120.json b/stats/pwc-summary-91-120.json index 4e64a6dd06..c995dc4237 100644 --- a/stats/pwc-summary-91-120.json +++ b/stats/pwc-summary-91-120.json @@ -115,7 +115,7 @@ } ], "subtitle" : { - "text" : "[Champions: 30] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 30] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-summary.json b/stats/pwc-summary.json index f480c339e6..79eb516e44 100644 --- a/stats/pwc-summary.json +++ b/stats/pwc-summary.json @@ -177,7 +177,7 @@ 16, 1, 0, - 287, + 288, 0, 0, 0, @@ -1009,7 +1009,7 @@ } ], "subtitle" : { - "text" : "[Champions: 328] Last updated at 2025-09-01 19:25:35 GMT" + "text" : "[Champions: 328] Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge [2019 - 2025]" diff --git a/stats/pwc-yearly-language-summary.json b/stats/pwc-yearly-language-summary.json index eaf4927379..bd7d270a5a 100644 --- a/stats/pwc-yearly-language-summary.json +++ b/stats/pwc-yearly-language-summary.json @@ -8,7 +8,7 @@ "data" : [ [ "Perl", - 1585 + 1587 ], [ "Raku", @@ -151,7 +151,7 @@ { "drilldown" : "2025", "name" : "2025", - "y" : 3024 + "y" : 3026 }, { "drilldown" : "2024", @@ -188,7 +188,7 @@ } ], "subtitle" : { - "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-01 19:25:35 GMT" + "text" : "Click the columns to drilldown the language breakdown. Last updated at 2025-09-02 11:19:20 GMT" }, "title" : { "text" : "The Weekly Challenge Language" -- cgit From 7a82574a741ed3fa340a24b8ad6cfdb9c93a78a9 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 2 Sep 2025 13:17:15 +0100 Subject: RogerBW solutions for challenge no. 337 --- challenge-337/roger-bell-west/crystal/ch-1.cr | 31 +++ challenge-337/roger-bell-west/crystal/ch-2.cr | 39 ++++ challenge-337/roger-bell-west/javascript/ch-1.js | 73 +++++++ challenge-337/roger-bell-west/javascript/ch-2.js | 52 +++++ challenge-337/roger-bell-west/kotlin/ch-1.kt | 45 ++++ challenge-337/roger-bell-west/kotlin/ch-2.kt | 52 +++++ challenge-337/roger-bell-west/lua/ch-1.lua | 85 ++++++++ challenge-337/roger-bell-west/lua/ch-2.lua | 65 ++++++ challenge-337/roger-bell-west/perl/ch-1.pl | 24 ++ challenge-337/roger-bell-west/perl/ch-2.pl | 32 +++ challenge-337/roger-bell-west/postscript/ch-1.ps | 267 +++++++++++++++++++++++ challenge-337/roger-bell-west/postscript/ch-2.ps | 73 +++++++ challenge-337/roger-bell-west/python/ch-1.py | 31 +++ challenge-337/roger-bell-west/python/ch-2.py | 36 +++ challenge-337/roger-bell-west/raku/ch-1.p6 | 22 ++ challenge-337/roger-bell-west/raku/ch-2.p6 | 29 +++ challenge-337/roger-bell-west/ruby/ch-1.rb | 38 ++++ challenge-337/roger-bell-west/ruby/ch-2.rb | 68 ++++++ challenge-337/roger-bell-west/rust/ch-1.rs | 41 ++++ challenge-337/roger-bell-west/rust/ch-2.rs | 50 +++++ challenge-337/roger-bell-west/scala/ch-1.scala | 47 ++++ challenge-337/roger-bell-west/scala/ch-2.scala | 55 +++++ challenge-337/roger-bell-west/tests.json | 73 +++++++ challenge-337/roger-bell-west/typst/ch-1.typ | 35 +++ challenge-337/roger-bell-west/typst/ch-2.typ | 43 ++++ 25 files changed, 1406 insertions(+) create mode 100755 challenge-337/roger-bell-west/crystal/ch-1.cr create mode 100755 challenge-337/roger-bell-west/crystal/ch-2.cr create mode 100755 challenge-337/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-337/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-337/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-337/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-337/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-337/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-337/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-337/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-337/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-337/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-337/roger-bell-west/python/ch-1.py create mode 100755 challenge-337/roger-bell-west/python/ch-2.py create mode 100755 challenge-337/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-337/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-337/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-337/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-337/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-337/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-337/roger-bell-west/scala/ch-1.scala create mode 100644 challenge-337/roger-bell-west/scala/ch-2.scala create mode 100644 challenge-337/roger-bell-west/tests.json create mode 100644 challenge-337/roger-bell-west/typst/ch-1.typ create mode 100644 challenge-337/roger-bell-west/typst/ch-2.typ diff --git a/challenge-337/roger-bell-west/crystal/ch-1.cr b/challenge-337/roger-bell-west/crystal/ch-1.cr new file mode 100755 index 0000000000..28347ee415 --- /dev/null +++ b/challenge-337/roger-bell-west/crystal/ch-1.cr @@ -0,0 +1,31 @@ +#! /usr/bin/crystal + +def smallerthancurrent(a) + b = a.sort() + m = Hash(Int32, Int32).new + b.each_with_index do |v, i| + if !m.has_key?(v) + m[v] = i + end + end + a.map{|x| m[x]} +end + +require "spec" +describe "smallerthancurrent" do + it "test_ex1" do + smallerthancurrent([6, 5, 4, 8]).should eq [2, 1, 0, 3] + end + it "test_ex2" do + smallerthancurrent([7, 7, 7, 7]).should eq [0, 0, 0, 0] + end + it "test_ex3" do + smallerthancurrent([5, 4, 3, 2, 1]).should eq [4, 3, 2, 1, 0] + end + it "test_ex4" do + smallerthancurrent([-1, 0, 3, -2, 1]).should eq [1, 2, 4, 0, 3] + end + it "test_ex5" do + smallerthancurrent([0, 1, 1, 2, 0]).should eq [0, 2, 2, 4, 0] + end +end diff --git a/challenge-337/roger-bell-west/crystal/ch-2.cr b/challenge-337/roger-bell-west/crystal/ch-2.cr new file mode 100755 index 0000000000..92301cc1ab --- /dev/null +++ b/challenge-337/roger-bell-west/crystal/ch-2.cr @@ -0,0 +1,39 @@ +#! /usr/bin/crystal + +def oddmatrix(rows, cols, points) + rm = Set(Int32).new + cm = Set(Int32).new + points.each do |p| + if rm.includes?(p[0]) + rm.delete(p[0]) + else + rm.add(p[0]) + end + if cm.includes?(p[1]) + cm.delete(p[1]) + else + cm.add(p[1]) + end + end + rm.size * (cols - cm.size) + + cm.size * (rows - rm.size) +end + +require "spec" +describe "oddmatrix" do + it "test_ex1" do + oddmatrix(2, 3, [[0, 1], [1, 1]]).should eq 6 + end + it "test_ex2" do + oddmatrix(2, 2, [[1, 1], [0, 0]]).should eq 0 + end + it "test_ex3" do + oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]]).should eq 0 + end + it "test_ex4" do + oddmatrix(1, 5, [[0, 2], [0, 4]]).should eq 2 + end + it "test_ex5" do + oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]).should eq 8 + end +end diff --git a/challenge-337/roger-bell-west/javascript/ch-1.js b/challenge-337/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..9c51c05c65 --- /dev/null +++ b/challenge-337/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,73 @@ +#! /usr/bin/node + +"use strict" + +function smallerthancurrent(a) { + let b = [...a]; + b.sort(function(a, b) {return a-b}); + let m = new Map; + b.forEach((v, i) => { + if (!m.has(v)) { + m.set(v, i); + } + }); + return a.map( x => m.get(x) ); +} + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +if (deepEqual(smallerthancurrent([6, 5, 4, 8]), [2, 1, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([7, 7, 7, 7]), [0, 0, 0, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([-1, 0, 3, -2, 1]), [1, 2, 4, 0, 3])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(smallerthancurrent([0, 1, 1, 2, 0]), [0, 2, 2, 4, 0])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-337/roger-bell-west/javascript/ch-2.js b/challenge-337/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..d1f0677096 --- /dev/null +++ b/challenge-337/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,52 @@ +#! /usr/bin/node + +"use strict" + +function oddmatrix(rows, cols, points) { + let rm = new Set; + let cm = new Set; + for (let p of points) { + if (rm.has(p[0])) { + rm.delete(p[0]); + } else { + rm.add(p[0]); + } + if (cm.has(p[1])) { + cm.delete(p[1]); + } else { + cm.add(p[1]); + } + } + return rm.size * (cols - cm.size) + cm.size * (rows - rm.size); +} + +if (oddmatrix(2, 3, [[0, 1], [1, 1]]) == 6) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(2, 2, [[1, 1], [0, 0]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(3, 3, [[0, 0], [1, 2], [2, 1]]) == 0) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(1, 5, [[0, 2], [0, 4]]) == 2) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (oddmatrix(4, 2, [[1, 0], [3, 1], [2, 0], [0, 1]]) == 8) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-337/roger-bell-west/kotlin/ch-1.kt b/challenge-337/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..2341884f7e --- /dev/null +++ b/challenge-337/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,45 @@ +fun smallerthancurrent(a: List): List { + val b = a.sorted() + var m = mutableMapOf() + for ((i, v) in b.withIndex()) { + if (!m.contains(v)) { + m.put(v, i) + } + } + return a.map{ m.getValue(it) }.toList() +} + +fun main() { + + if (smallerthancurrent(listOf(6, 5, 4, 8)) == listOf(2, 1, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(7, 7, 7, 7)) == listOf(0, 0, 0, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(5, 4, 3, 2, 1)) == listOf(4, 3, 2, 1, 0)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(-1, 0, 3, -2, 1)) == listOf(1, 2, 4, 0, 3)) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (smallerthancurrent(listOf(0, 1, 1, 2, 0)) == listOf(0, 2, 2, 4, 0)) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-337/roger-bell-west/kotlin/ch-2.kt b/challenge-337/roger-bell-west/kotlin/ch-2.kt new file mode 100644 index 0000000000..76cc20a5f1 --- /dev/null +++ b/challenge-337/roger-bell-west/kotlin/ch-2.kt @@ -0,0 +1,52 @@ +fun oddmatrix(rows: Int, cols: Int, points: List>): Int { + var rm = mutableSetOf() + var cm = mutableSetOf() + for (p in points) { + if (rm.contains(p[0])) { + rm.remove(p[0]) + } else { + rm.add(p[0]) + } + if (cm.contains(p[1])) { + cm.remove(p[1]) + } else { + cm.add(p[1]) + } + } + return rm.size * (cols - cm.size) + cm.size * (rows - rm.size) +} + +fun main() { + + if (oddmatrix(2, 3, listOf(listOf(0, 1), listOf(1, 1))) == 6) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(2, 2, listOf(listOf(1, 1), listOf(0, 0))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(3, 3, listOf(listOf(0, 0), listOf(1, 2), listOf(2, 1))) == 0) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(1, 5, listOf(listOf(0, 2), listOf(0, 4))) == 2) { + print("Pass") + } else { + print("Fail") + } + print(" ") + if (oddmatrix(4, 2, listOf(listOf(1, 0), listOf(3, 1), listOf(2, 0), listOf(0, 1))) == 8) { + print("Pass") + } else { + print("Fail") + } + println("") + +} diff --git a/challenge-337/roger-bell-west/lua/ch-1.lua b/challenge-337/roger-bell-west/lua/ch-1.lua new file mode 100755 index 0000000000..c0c8541010 --- /dev/null +++ b/challenge-337/roger-bell-west/lua/ch-1.lua @@ -0,0 +1,85 @@ +#! /usr/bin/lua + +function smallerthancurrent(a) + local b = {} + for _, x in ipairs(a) do + table.insert(b, x) + end + table.sort(b) + local m = {} + for i, v in ipairs(b) do + if m[v] == nil then + m[v] = i - 1 + end + end + local out = {} + for _, x in ipairs(a) do + table.insert(out, m[x]) + end + return out +end + +-- by Michael Anderson at +-- https://stackoverflow.com/questions/8722620/comparing-two-index-tables-by-index-value-in-lua +-- modified by Roger +function recursive_compare(t1,t2) + -- Use usual comparison first. + if t1==t2 then return true end + -- We only support non-default behavior for tables + if (type(t1)~="table") then return false end + -- They better have the same metatables + local mt1 = getmetatable(t1) + local mt2 = getmetatable(t2) + if( not recursive_compare(mt1,mt2) ) then return false end + -- Build list of all keys + local kk = {} + for k1, _ in pairs(t1) do + kk[k1] = true + end + for k2, _ in pairs(t2) do + kk[k2] = true + end + -- Check each key that exists in at least one table + for _, k in ipairs(kk) do + if (not recursive_compare(t1[k], t2[k])) then + return false + end + end + return true +end + +if recursive_compare(smallerthancurrent({6, 5, 4, 8}), {2, 1, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({7, 7, 7, 7}), {0, 0, 0, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({5, 4, 3, 2, 1}), {4, 3, 2, 1, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({-1, 0, 3, -2, 1}), {1, 2, 4, 0, 3}) then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if recursive_compare(smallerthancurrent({0, 1, 1, 2, 0}), {0, 2, 2, 4, 0}) then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-337/roger-bell-west/lua/ch-2.lua b/challenge-337/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..946d52e7e8 --- /dev/null +++ b/challenge-337/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,65 @@ +#! /usr/bin/lua + +function oddmatrix(rows, cols, points) + local rm = {} + local cm = {} + for _, p in ipairs(points) do + if rm[p[1]] == nil then + rm[p[1]] = true + else + rm[p[1]] = nil + end + if cm[p[2]] == nil then + cm[p[2]] = true + else + cm[p[2]] = nil + end + end + local cml = length(cm) + local rml = length(rm) + return rml * (cols - cml) + cml * (rows -rml) +end + +function length(t) + local l = 0 + for k, v in pairs(t) do + l = l + 1 + end + return l +end + +if oddmatrix(2, 3, {{0, 1}, {1, 1}}) == 6 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(2, 2, {{1, 1}, {0, 0}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(3, 3, {{0, 0}, {1, 2}, {2, 1}}) == 0 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(1, 5, {{0, 2}, {0, 4}}) == 2 then + io.write("Pass") +else + io.write("FAIL") +end +io.write(" ") + +if oddmatrix(4, 2, {{1, 0}, {3, 1}, {2, 0}, {0, 1}}) == 8 then + io.write("Pass") +else + io.write("FAIL") +end +print("") + diff --git a/challenge-337/roger-bell-west/perl/ch-1.pl b/challenge-337/roger-bell-west/perl/ch-1.pl new file mode 100755 index 0000000000..4283c29a12 --- /dev/null +++ b/challenge-337/roger-bell-west/perl/ch-1.pl @@ -0,0 +1,24 @@ +#! /usr/bin/perl + +use strict; +use warnings; +use experimental 'signatures'; + +use Test::More tests => 5; + +is_deeply(smallerthancurrent([6, 5, 4, 8]), [2, 1, 0, 3], 'example 1'); +is_deeply(smallerthancurrent([7, 7, 7, 7]), [0,