diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-12 18:06:52 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-12 18:06:52 +0000 |
| commit | bd95c8ed599517bae2c202d4a8e3c491eb4e4c75 (patch) | |
| tree | e94118c4fd7b94858193a4566e53f5448c0de031 /challenge-242 | |
| parent | db98c2730091cae2427593818d9d79afa6f6e2f3 (diff) | |
| parent | 9b7836de448b3b12c2729ed6f117323b9c53dc84 (diff) | |
| download | perlweeklychallenge-club-bd95c8ed599517bae2c202d4a8e3c491eb4e4c75.tar.gz perlweeklychallenge-club-bd95c8ed599517bae2c202d4a8e3c491eb4e4c75.tar.bz2 perlweeklychallenge-club-bd95c8ed599517bae2c202d4a8e3c491eb4e4c75.zip | |
Merge pull request #9038 from LubosKolouch/master
feat(challenge-242/lubos-kolouch/perl,python,raku,blog): Challenge 242 LK Perl Python Raku blog
Diffstat (limited to 'challenge-242')
| -rw-r--r-- | challenge-242/lubos-kolouch/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-242/lubos-kolouch/perl/ch-1.pl | 25 | ||||
| -rw-r--r-- | challenge-242/lubos-kolouch/perl/ch-2.pl | 12 | ||||
| -rw-r--r-- | challenge-242/lubos-kolouch/python/ch-1.py | 26 | ||||
| -rw-r--r-- | challenge-242/lubos-kolouch/python/ch-2.py | 29 | ||||
| -rw-r--r-- | challenge-242/lubos-kolouch/raku/ch-1.raku | 22 | ||||
| -rw-r--r-- | challenge-242/lubos-kolouch/raku/ch-2.raku | 11 |
7 files changed, 126 insertions, 0 deletions
diff --git a/challenge-242/lubos-kolouch/blog.txt b/challenge-242/lubos-kolouch/blog.txt new file mode 100644 index 0000000000..42a7b996e2 --- /dev/null +++ b/challenge-242/lubos-kolouch/blog.txt @@ -0,0 +1 @@ +https://egroup.kolouch.org/nextcloud/sites/lubos/2023-11-06_Weekly_challenge_242 diff --git a/challenge-242/lubos-kolouch/perl/ch-1.pl b/challenge-242/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..16127c5451 --- /dev/null +++ b/challenge-242/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +use Test::More; + +sub find_missing_members { + my ($arr1_ref, $arr2_ref) = @_; + my %count; + + $count{$_}++ for @$arr1_ref; + $count{$_}-- for @$arr2_ref; + + my @missing_in_arr2 = sort { $a <=> $b } grep { $count{$_} > 0 } keys %count; + my @missing_in_arr1 = sort { $a <=> $b } grep { $count{$_} < 0 } keys %count; + + return (\@missing_in_arr2, \@missing_in_arr1); +} + +# Testing +my @test1_arr1 = (1, 2, 3); +my @test1_arr2 = (2, 4, 6); +my ($result1, $result2) = find_missing_members(\@test1_arr1, \@test1_arr2); +is_deeply($result1, [1, 3], 'Test 1 - Missing in arr2'); +is_deeply($result2, [4, 6], 'Test 1 - Missing in arr1'); + +done_testing(); diff --git a/challenge-242/lubos-kolouch/perl/ch-2.pl b/challenge-242/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..2d2a169641 --- /dev/null +++ b/challenge-242/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,12 @@ +use strict; +use warnings; +use Test::More tests => 2; + +sub flip_matrix { + my @matrix = @_; + return map { [ map { 1 - $_ } reverse @$_ ] } @matrix; +} + +# Test cases with the correct structure +is_deeply([flip_matrix([1, 1, 0], [1, 0, 1], [0, 0, 0])], [[1, 0, 0], [0, 1, 0], [1, 1, 1]], 'Example 1'); +is_deeply([flip_matrix([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])], [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], 'Example 2'); diff --git a/challenge-242/lubos-kolouch/python/ch-1.py b/challenge-242/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..62a3bc9993 --- /dev/null +++ b/challenge-242/lubos-kolouch/python/ch-1.py @@ -0,0 +1,26 @@ +from typing import List, Tuple + + +def find_missing_members( + arr1: list[int], arr2: list[int] +) -> tuple[list[int], list[int]]: + # Convert lists to sets for unique elements + set_arr1, set_arr2 = set(arr1), set(arr2) + + # Find unique missing members in each array + missing_in_arr2 = sorted(list(set_arr1 - set_arr2)) + missing_in_arr1 = sorted(list(set_arr2 - set_arr1)) + + return missing_in_arr2, missing_in_arr1 + + +# Testing +assert find_missing_members([1, 2, 3], [2, 4, 6]) == ([1, 3], [4, 6]) +assert find_missing_members([1, 2, 3, 3], [1, 1, 2, 2]) == ([3], []) + +# Running the script +if __name__ == "__main__": + arr1, arr2 = [1, 2, 3, 3], [1, 1, 2, 2] + print(f"Input: arr1 = {arr1}, arr2 = {arr2}") + result = find_missing_members(arr1, arr2) + print(f"Output: {result}") diff --git a/challenge-242/lubos-kolouch/python/ch-2.py b/challenge-242/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..6344ef64c5 --- /dev/null +++ b/challenge-242/lubos-kolouch/python/ch-2.py @@ -0,0 +1,29 @@ +import unittest +from typing import List + + +def flip_matrix(matrix: list[list[int]]) -> list[list[int]]: + """ + Flip the given matrix by reversing each row and inverting each element. + + :param matrix: A list of lists representing the binary matrix. + :return: The flipped matrix. + """ + return [[1 - element for element in reversed(row)] for row in matrix] + + +# Test cases +class TestFlipMatrix(unittest.TestCase): + def test_examples(self): + self.assertEqual( + flip_matrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]), + [[1, 0, 0], [0, 1, 0], [1, 1, 1]], + ) + self.assertEqual( + flip_matrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]), + [[1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]], + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/challenge-242/lubos-kolouch/raku/ch-1.raku b/challenge-242/lubos-kolouch/raku/ch-1.raku new file mode 100644 index 0000000000..c935ae266d --- /dev/null +++ b/challenge-242/lubos-kolouch/raku/ch-1.raku @@ -0,0 +1,22 @@ +use Test; + +sub find-missing-members(@arr1, @arr2) { + my %count; + + %count{$_}++ for @arr1; + %count{$_}-- for @arr2; + + my @missing-in-arr2 = %count.keys.grep({ %count{$_} > 0 }).sort; + my @missing-in-arr1 = %count.keys.grep({ %count{$_} < 0 }).sort; + + return @missing-in-arr2, @missing-in-arr1; +} + +# Testing +my @test1-arr1 = 1, 2, 3; +my @test1-arr2 = 2, 4, 6; +my ($result1, $result2) = find-missing-members(@test1-arr1, @test1-arr2); +is-deeply($result1, ["1", "3"], 'Test 1 - Missing in arr2'); +is-deeply($result2, ["4", "6"], 'Test 1 - Missing in arr1'); + +done-testing(); diff --git a/challenge-242/lubos-kolouch/raku/ch-2.raku b/challenge-242/lubos-kolouch/raku/ch-2.raku new file mode 100644 index 0000000000..3e0559573d --- /dev/null +++ b/challenge-242/lubos-kolouch/raku/ch-2.raku @@ -0,0 +1,11 @@ +use Test; + +sub flip-matrix(@matrix) { + return @matrix.map({ [($_.reverse).map({1 - $_})] }); +} + +# Corrected Test cases +is-deeply flip-matrix(([1, 1, 0], [1, 0, 1], [0, 0, 0])), (([1, 0, 0], [0, 1, 0], [1, 1, 1])), 'Example 1'; +is-deeply flip-matrix(([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])), (([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])), 'Example 2'; + +done-testing; |
