diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-07 14:38:25 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-07 14:38:25 +0000 |
| commit | 5c464fd842b4e53d342602f2cb710c75aa14a435 (patch) | |
| tree | a3fcd4c71fbd71289a663f1b794e4616fbeab0a9 | |
| parent | e68ef367a6f4dac843d25d276b543fb626218042 (diff) | |
| parent | 16a794c74c24c7ca6a9a04ac2a883a643bd41599 (diff) | |
| download | perlweeklychallenge-club-5c464fd842b4e53d342602f2cb710c75aa14a435.tar.gz perlweeklychallenge-club-5c464fd842b4e53d342602f2cb710c75aa14a435.tar.bz2 perlweeklychallenge-club-5c464fd842b4e53d342602f2cb710c75aa14a435.zip | |
Merge pull request #9016 from jeanluc2020/jeanluc-242
Add solution 242
| -rw-r--r-- | challenge-242/jeanluc2020/blog-1.txt | 1 | ||||
| -rw-r--r-- | challenge-242/jeanluc2020/blog-2.txt | 1 | ||||
| -rwxr-xr-x | challenge-242/jeanluc2020/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-242/jeanluc2020/perl/ch-2.pl | 74 | ||||
| -rwxr-xr-x | challenge-242/jeanluc2020/python/ch-1.py | 66 | ||||
| -rwxr-xr-x | challenge-242/jeanluc2020/python/ch-2.py | 71 |
6 files changed, 275 insertions, 0 deletions
diff --git a/challenge-242/jeanluc2020/blog-1.txt b/challenge-242/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..3609be466d --- /dev/null +++ b/challenge-242/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-242-1.html diff --git a/challenge-242/jeanluc2020/blog-2.txt b/challenge-242/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..97547946a0 --- /dev/null +++ b/challenge-242/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-242-2.html diff --git a/challenge-242/jeanluc2020/perl/ch-1.pl b/challenge-242/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..d3b7aaf045 --- /dev/null +++ b/challenge-242/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK1 +# +# Task 1: Missing Members +# ======================= +# +# You are given two arrays of integers. +# +# Write a script to find out the missing members in each other arrays. +# +## Example 1 +## +## Input: @arr1 = (1, 2, 3) +## @arr2 = (2, 4, 6) +## Output: ([1, 3], [4, 6]) +## +## (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +## (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +# +## Example 2 +## +## Input: @arr1 = (1, 2, 3, 3) +## @arr2 = (1, 1, 2, 2) +## Output: ([3]) +## +## (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +## (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). +# +############################################################ +## +## discussion +## +############################################################ +# +# For each element of each array, if it is not in the other array, +# save it for the output. +# Example 2 has a caveat though, by not returning an empty array in +# case of no missing elements it is unclear in which of the arrays +# the element was originally. It's better to return an empty array +# here, so let's just do that instead. + +missing_members( [ 1, 2, 3 ], [ 2, 4, 6 ] ); +missing_members( [ 1, 2, 3, 3 ], [ 1, 1, 2, 2 ] ); + +sub missing_members { + my ($arr1, $arr2) = @_; + print "Input: (" . join(", ", @$arr1) . "), (" . join(", ", @$arr2) . ")\n"; + my (@res1, @res2, %keys1, %keys2); + map { $keys1{$_} = 1; } @$arr1; + map { $keys2{$_} = 1; } @$arr2; + my %seen = (); + foreach my $elem (@$arr1) { + push @res1, $elem unless $keys2{$elem} or $seen{$elem}; + $seen{$elem} = 1; + } + %seen = (); + foreach my $elem (@$arr2) { + push @res2, $elem unless $keys1{$elem} or $seen{$elem}; + $seen{$elem} = 1; + } + print "Output: ([" . join(", ", @res1) . "], [" . join(", ", @res2) . "])\n"; +} diff --git a/challenge-242/jeanluc2020/perl/ch-2.pl b/challenge-242/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ed69f9eab7 --- /dev/null +++ b/challenge-242/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,74 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK2 +# +# Task 2: Flip Matrix +# =================== +# +# You are given n x n binary matrix. +# +# Write a script to flip the given matrix as below. +# +# 1 1 0 +# 0 1 1 +# 0 0 1 +# +# a) Reverse each row +# +# 0 1 1 +# 1 1 0 +# 1 0 0 +# +# b) Invert each member +# +# 1 0 0 +# 0 0 1 +# 0 1 1 +# +# +## Example 1 +## +## Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]) +## Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) +# +## Example 2 +## +## Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]) +## Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) +# +############################################################ +## +## discussion +## +############################################################ +# +# Just reverse and invert. Nothing complicated. + +flip_matrix([1, 1, 0], [1, 0, 1], [0, 0, 0]); +flip_matrix([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]); + +sub flip_matrix { + my @rows = @_; + my @result; + print "Input: ("; + foreach my $row (@rows) { + print "[" . join(", ", @$row) . "],"; + } + print ")\n"; + foreach my $row (@rows) { + push @result, [ inverted( reverse @$row ) ]; + } + print "Ouput: ("; + foreach my $row (@result) { + print "[" . join(", ", @$row) . "],"; + } + print ")\n"; +} + +sub inverted { + my @array = @_; + my @result = (); + foreach my $elem (@array) { + push @result, $elem == 0 ? 1 : 0; + } + return @result; +} diff --git a/challenge-242/jeanluc2020/python/ch-1.py b/challenge-242/jeanluc2020/python/ch-1.py new file mode 100755 index 0000000000..f17e000f6f --- /dev/null +++ b/challenge-242/jeanluc2020/python/ch-1.py @@ -0,0 +1,66 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK1 +# +# Task 1: Missing Members +# ======================= +# +# You are given two arrays of integers. +# +# Write a script to find out the missing members in each other arrays. +# +## Example 1 +## +## Input: @arr1 = (1, 2, 3) +## @arr2 = (2, 4, 6) +## Output: ([1, 3], [4, 6]) +## +## (1, 2, 3) has 2 members (1, 3) missing in the array (2, 4, 6). +## (2, 4, 6) has 2 members (4, 6) missing in the array (1, 2, 3). +# +## Example 2 +## +## Input: @arr1 = (1, 2, 3, 3) +## @arr2 = (1, 1, 2, 2) +## Output: ([3]) +## +## (1, 2, 3, 3) has 2 members (3, 3) missing in the array (1, 1, 2, 2). Since they are same, keep just one. +## (1, 1, 2, 2) has 0 member missing in the array (1, 2, 3, 3). +# +############################################################ +## +## discussion +## +############################################################ +# +# For each element of each array, if it is not in the other array, +# save it for the output. +# Example 2 has a caveat though, by not returning an empty array in +# case of no missing elements it is unclear in which of the arrays +# the element was originally. It's better to return an empty array +# here, so let's just do that instead. + +def missing_members(arr1: list, arr2: list): + seen: dict = {} + res1: list = [] + res2: list = [] + print("Input: ([", ", ".join(str(x) for x in arr1), "], [", ", ".join(str(x) for x in arr2), "])") + for elem in arr1: + if elem in seen: + next + else: + if elem not in arr2: + seen[elem] = 1 + res1.append(elem) + seen = {} + for elem in arr2: + if elem in seen: + next + else: + if elem not in arr1: + seen[elem] = 1 + res2.append(elem) + print("Output: ([", ", ".join(str(x) for x in res1), "], [", ", ".join(str(x) for x in res2), "])") + +missing_members( [ 1, 2, 3 ], [ 2, 4, 6 ] ); +missing_members( [ 1, 2, 3, 3 ], [ 1, 1, 2, 2 ] ); + diff --git a/challenge-242/jeanluc2020/python/ch-2.py b/challenge-242/jeanluc2020/python/ch-2.py new file mode 100755 index 0000000000..df3a533199 --- /dev/null +++ b/challenge-242/jeanluc2020/python/ch-2.py @@ -0,0 +1,71 @@ +#!/usr/bin/python3 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-242/#TASK2 +# +# Task 2: Flip Matrix +# =================== +# +# You are given n x n binary matrix. +# +# Write a script to flip the given matrix as below. +# +# 1 1 0 +# 0 1 1 +# 0 0 1 +# +# a) Reverse each row +# +# 0 1 1 +# 1 1 0 +# 1 0 0 +# +# b) Invert each member +# +# 1 0 0 +# 0 0 1 +# 0 1 1 +# +# +## Example 1 +## +## Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]) +## Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1]) +# +## Example 2 +## +## Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]) +## Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0]) +# +############################################################ +## +## discussion +## +############################################################ +# +# Just reverse and invert. Nothing complicated. + +def invert_reverse(array: list) -> list: + result: list = [] + for x in reversed(array): + if x == 0: + result.append(1) + else: + result.append(0) + return result + +def flip_matrix(rows: list): + print("Input: (", end='') + for row in rows: + print("[", ", ".join(str(x) for x in row), "],", end='', sep='') + print(")") + result: list = [] + for row in rows: + result.append(invert_reverse(row)) + print("Output: (", end='') + for row in result: + print("[", ", ".join(str(x) for x in row), "],", end='', sep='') + print(")") + +flip_matrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]) +flip_matrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]) + + |
