diff options
| author | ntovar <tovar.nelo@gmail.com> | 2023-11-13 10:19:21 -0500 |
|---|---|---|
| committer | ntovar <tovar.nelo@gmail.com> | 2023-11-13 10:19:21 -0500 |
| commit | 4333c415ec1503cffa0618876e2ded4f6eb2ce8d (patch) | |
| tree | 00121627979e3bd168d6e1b14b873e2f6b1df575 /challenge-242 | |
| parent | 04f30726f8970a4ac098e39f897f8761ba6ee8f8 (diff) | |
| parent | aeed5ae2bdafdcf14de24d172392278b8ba0b44f (diff) | |
| download | perlweeklychallenge-club-4333c415ec1503cffa0618876e2ded4f6eb2ce8d.tar.gz perlweeklychallenge-club-4333c415ec1503cffa0618876e2ded4f6eb2ce8d.tar.bz2 perlweeklychallenge-club-4333c415ec1503cffa0618876e2ded4f6eb2ce8d.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-242')
88 files changed, 4137 insertions, 215 deletions
diff --git a/challenge-242/0rir/ch-1.raku b/challenge-242/0rir/ch-1.raku new file mode 100644 index 0000000000..62ca11361e --- /dev/null +++ b/challenge-242/0rir/ch-1.raku @@ -0,0 +1,73 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.d; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +242-1: Missing Members Submitted by: Mohammad S Anwar + +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). +=end comment + +=begin comment +Often, I resist the idea that a string is the desired output. The given +output is ambiguous for example 2, i.e. the @ary1 and @ary2 could be in +@ary2, @ary1 order. + +=end comment + +my @Test = + # @a @b @exp @exp-shortened + (1, 2, 3), (2, 4, 6), ((1, 3), (4, 6)), ((1, 3), (4, 6),), + (1, 2, 3, 3), (1, 1, 2, 2), ((3,),()), ((3,),), + (1, 1, 2, 2), (1, 2, 3, 3), ((), (3,)), ((3,)), + (1, 1, 2, 2), (1, 1, 2, 2), ((),()), (), + (), (1,), ((), 1), (1,), + (1,), (), (1, ()), (1,), + (), (), ((),()), (), + ; + +plan @Test ÷ 2; + +# the logical change +sub l-and-r-oj( @a, @b ) { # oj ~ outer join + ((@a (-) @b).keys.sort.List // () ), # Empty is a Slip ? + ((@b (-) @a).keys.sort.List // () ); +} + +# the presentation +sub delete-empty( @l-and-r is copy --> List) { + if @l-and-r[1] ~~ () { @l-and-r.pop } + if @l-and-r[0] ~~ () { @l-and-r.shift } + @l-and-r; +} + + +for @Test -> @a, @b, @exp, @exp-shortened { + is l-and-r-oj(@a, @b), @exp, 'test'; + is delete-empty(l-and-r-oj(@a, @b)), @exp-shortened, 'test'; +} +done-testing; + +my @arr1 = (1, 2, 3, 3); +my @arr2 = (1, 1, 2, 2); +say "\nInput: @arr1 = @arr1.raku()\n @arr2 = @arr2.raku()\nOutput = ", + delete-empty(l-and-r-oj(@arr1, @arr2)); +exit; diff --git a/challenge-242/0rir/ch-2.raku b/challenge-242/0rir/ch-2.raku new file mode 100644 index 0000000000..ac2a53bbf7 --- /dev/null +++ b/challenge-242/0rir/ch-2.raku @@ -0,0 +1,62 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ « » ∴ +use v6.d; +use lib $?FILE.IO.cleanup.parent(2).add("lib"); +use Test; + +=begin comment +242-2: Flip Matrix Submitted by: Mohammad S Anwar +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]) +=end comment + +my @Test = + [[1, 1, 0], [1, 0, 1], [0, 0, 0]], + [[1, 0, 0], [0, 1, 0], [1, 1, 1]], + [[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]], +; +plan @Test ÷ 2; + +subset Matrix of Array; + +sub reverse-n-not( @a is copy --> Matrix ) { + @a.map( *.map( (!*).Int ).reverse.Array).Array +} + +for @Test -> @in, @exp { + is reverse-n-not(@in), @exp, ""; +} +done-testing; + +my @matrix = [[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]; +say "\nInput @matrix = @matrix.raku()\nOutput: &reverse-n-not(@matrix).raku()"; + + +exit; + diff --git a/challenge-242/adam-russell/blog.txt b/challenge-242/adam-russell/blog.txt new file mode 100644 index 0000000000..7778ddfedd --- /dev/null +++ b/challenge-242/adam-russell/blog.txt @@ -0,0 +1 @@ +http://rabbitfarm.com/cgi-bin/blosxom/perl/2023/11/11
\ No newline at end of file diff --git a/challenge-242/adam-russell/blog1.txt b/challenge-242/adam-russell/blog1.txt new file mode 100644 index 0000000000..6557439598 --- /dev/null +++ b/challenge-242/adam-russell/blog1.txt @@ -0,0 +1 @@ +http://rabbitfarm.com/cgi-bin/blosxom/prolog/2023/11/11
\ No newline at end of file diff --git a/challenge-242/adam-russell/javascript/ch-1.js b/challenge-242/adam-russell/javascript/ch-1.js new file mode 100644 index 0000000000..86f57321a5 --- /dev/null +++ b/challenge-242/adam-russell/javascript/ch-1.js @@ -0,0 +1,33 @@ +class Ch1{ + missingMembers(a1, a2){ + let r = []; + r.push( + a1.filter(function(x){ + return a2.findIndex(function(y){ + return x === y; + }) === -1; + }) + ); + r.push( + a2.filter(function(x){ + return a1.findIndex(function(y){ + return x === y; + }) === -1; + }) + ); + let s = []; + r.forEach(function(x){ + if(x.length > 0){ + s.push(Array.from(new Set(x))); + } + }); + return s; + } +} +let ch1 = new Ch1(); +console.log( + ch1.missingMembers([1, 2, 3], [2, 4, 6]) +); +console.log( + ch1.missingMembers([1, 2, 3, 3], [1, 1, 2, 2]) +);
\ No newline at end of file diff --git a/challenge-242/adam-russell/javascript/ch-2.js b/challenge-242/adam-russell/javascript/ch-2.js new file mode 100644 index 0000000000..4a4641f9e1 --- /dev/null +++ b/challenge-242/adam-russell/javascript/ch-2.js @@ -0,0 +1,14 @@ +class Ch2{ + flipMatrix(m){ + return m.map(function(row){ + return row.reverse().map(x => ~x & 1); + }); + } +} +let ch2 = new Ch2(); +console.log( + ch2.flipMatrix([[1, 1, 0], [1, 0, 1], [0, 0, 0]]) +); +console.log( + ch2.flipMatrix([[1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]]) +);
\ No newline at end of file diff --git a/challenge-242/adam-russell/perl/ch-1.pl b/challenge-242/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..a6ee05cfcf --- /dev/null +++ b/challenge-242/adam-russell/perl/ch-1.pl @@ -0,0 +1,58 @@ +use v5.38; +## +# You are given two arrays of integers. +# Write a script to find out the missing members in each other arrays. +## +use boolean; +use Data::Dump q/pp/; +sub missing_members{ + my @r; + my($a0, $a1) = @_; + my $missing0 = []; + missing_members_r([@{$a0}], [@{$a1}], $missing0); + my $missing1 = []; + missing_members_r([@{$a1}], [@{$a0}], $missing1); + push @r, $missing0 if @{$missing0} > 0; + push @r, $missing1 if @{$missing1} > 0; + return @r; +} + +sub missing_members_r{ + my($a0, $a1, $missing, $seen) = @_; + $seen = [] if !defined($seen); + my $x = shift @{$a0}; + push @{$missing}, $x if missing_r($x, [@{$a1}]) && !seen_r($x, $seen); + push @{$seen}, $x; + missing_members_r($a0, $a1, $missing, $seen) if @{$a0} > 0; +} + +sub missing_r{ + my($x, $a0) = @_; + return true if @{$a0} == 0; + if(@{$a0}){ + my $y = shift @{$a0}; + if($x == $y){ + return false; + } + } + return missing_r($x, $a0); +} + +sub seen_r{ + my($x, $seen) = @_; + return false if @{$seen} == 0; + my $y = shift @{$seen}; + if($x == $y){ + return true; + } + return seen_r($x, $seen); +} + +MAIN:{ + my @array1 = (1, 2, 3); + my @array2 = (2, 4, 6); + say pp missing_members \@array1, \@array2; + @array1 = (1, 2, 3, 3); + @array2 = (1, 1, 2, 2); + say pp missing_members \@array1, \@array2; +}
\ No newline at end of file diff --git a/challenge-242/adam-russell/perl/ch-2.pl b/challenge-242/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..63a70ca188 --- /dev/null +++ b/challenge-242/adam-russell/perl/ch-2.pl @@ -0,0 +1,30 @@ +use v5.38; +## +# 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 +## +use Data::Dump q/pp/; +sub flip_matrix{ + return map { + my $row = $_; + [map {~$_ & 1} reverse @{$row}] + } @_; +} + +MAIN:{ + my @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0]); + say pp flip_matrix @matrix; + @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]); + say pp flip_matrix @matrix; +}
\ No newline at end of file diff --git a/challenge-242/adam-russell/prolog/ch-1.p b/challenge-242/adam-russell/prolog/ch-1.p new file mode 100644 index 0000000000..9691230ace --- /dev/null +++ b/challenge-242/adam-russell/prolog/ch-1.p @@ -0,0 +1,8 @@ +missing(L, E, Member):- + (member(E, L), Member = nil); + (\+ member(E, L), Member = E). +missing_members([List1, List2], [Missing1, Missing2]):- + maplist(missing(List2), List1, Missing1Nil), + delete(Missing1Nil, nil, Missing1), + maplist(missing(List1), List2, Missing2Nil), |
