diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-12 23:40:00 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-12 23:40:00 +0000 |
| commit | 65d0e5b3aff3ddfda2b0ff8185228f2830ae4ad7 (patch) | |
| tree | 836f12b2e580fa5e02779fba6b1982e765feb5b2 | |
| parent | ea5129e980d775a4066ada748b945361a4f8edd9 (diff) | |
| parent | 0f446f76937773ed0f484d198da70b0741c3ba17 (diff) | |
| download | perlweeklychallenge-club-65d0e5b3aff3ddfda2b0ff8185228f2830ae4ad7.tar.gz perlweeklychallenge-club-65d0e5b3aff3ddfda2b0ff8185228f2830ae4ad7.tar.bz2 perlweeklychallenge-club-65d0e5b3aff3ddfda2b0ff8185228f2830ae4ad7.zip | |
Merge pull request #9050 from 0rir/242
242
| -rw-r--r-- | challenge-242/0rir/ch-1.raku | 73 | ||||
| -rw-r--r-- | challenge-242/0rir/ch-2.raku | 62 |
2 files changed, 135 insertions, 0 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; + |
