diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-29 17:36:18 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-29 17:36:18 +0000 |
| commit | 66653895e79e55718db4f542d0529ba9a09e2aa3 (patch) | |
| tree | eb8da4393d1731e9a643b13f2c2f629f73993877 | |
| parent | e5f5309fd45b7e190c3bf574304d3b9b42ce185a (diff) | |
| parent | 1aaeb59f9d845cf891e81e9e96ba414b8fedbc8f (diff) | |
| download | perlweeklychallenge-club-66653895e79e55718db4f542d0529ba9a09e2aa3.tar.gz perlweeklychallenge-club-66653895e79e55718db4f542d0529ba9a09e2aa3.tar.bz2 perlweeklychallenge-club-66653895e79e55718db4f542d0529ba9a09e2aa3.zip | |
Merge pull request #2865 from gugod/gugod-088
gugod's solutions to pwc 088
| -rw-r--r-- | challenge-088/gugod/README | 6 | ||||
| -rw-r--r-- | challenge-088/gugod/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-088/gugod/raku/ch-1.raku | 31 | ||||
| -rw-r--r-- | challenge-088/gugod/raku/ch-2.raku | 83 |
4 files changed, 118 insertions, 3 deletions
diff --git a/challenge-088/gugod/README b/challenge-088/gugod/README index 9a4443f23b..552c5afaa1 100644 --- a/challenge-088/gugod/README +++ b/challenge-088/gugod/README @@ -1,6 +1,6 @@ Solutions by Kang-min Liu. Blog posts: -- https://gugod.org/2020/11/pwc-087/ -- https://gugod.org/2020/11/pwc-087-en/ -- https://dev.to/gugod/solvintg-perl-weekly-challenge-087-1f09 +- https://gugod.org/2020/11/pwc-088/ +- https://gugod.org/2020/11/pwc-088-en/ +- https://dev.to/gugod/solving-perl-weekly-challenge-088-array-of-prodict-vs-spiral-matrix-13ni diff --git a/challenge-088/gugod/blog.txt b/challenge-088/gugod/blog.txt new file mode 100644 index 0000000000..d53d5d2b59 --- /dev/null +++ b/challenge-088/gugod/blog.txt @@ -0,0 +1 @@ +https://gugod.org/2020/11/pwc-088-en/ diff --git a/challenge-088/gugod/raku/ch-1.raku b/challenge-088/gugod/raku/ch-1.raku new file mode 100644 index 0000000000..43e8b650fd --- /dev/null +++ b/challenge-088/gugod/raku/ch-1.raku @@ -0,0 +1,31 @@ +sub array-of-product (@N) { + my $zeros = @N.grep(-> $n { $n == 0 }).elems; + + my $q = [*] @N.grep(-> $n { $n != 0 }); + + my $fun; + + if $zeros == 0 { + $fun = -> $n { $q / $n }; + } elsif $zeros == 1 { + $fun = -> $n { $n == 0 ?? $q !! 0 }; + } else { + $fun = -> $n { 0 }; + } + + return @N.map($fun); +} + +my @examples = [ + [5, 2, 1, 4, 3], + [2, 1, 4, 3], + [1, 3, 3, 7], + [1, 3, 0, 3, 7], + [1, 3, 0, 3, 7, 0], +]; + +for @examples -> @N { + say "Input @N = { @N.gist }"; + say "Output @M = " ~ array-of-product(@N).gist; + say "----"; +} diff --git a/challenge-088/gugod/raku/ch-2.raku b/challenge-088/gugod/raku/ch-2.raku new file mode 100644 index 0000000000..e5a21065b7 --- /dev/null +++ b/challenge-088/gugod/raku/ch-2.raku @@ -0,0 +1,83 @@ + +sub spiral-matrix (@M) { + # Things don'tt change + my $width := @M[0].elems; + my $height := @M.elems; + my @directions := [ + [ 1, 0], # → + [ 0, 1], # ↓ + [-1, 0], # ← + [ 0,-1], # ↑ + ]; + + # Things change. + my @cursor = [0,0]; + my $curdir = 0; + + # The order of boundary is arranged in thee way to make it easier to implement turn(). + my @boundary = [ + -1, # ↑ + $width, # → + $height, # ↓ + -1, # ← + ]; + + my sub turn ($curdir is rw, @boundary) of Nil { # ⤵ + @boundary[$curdir] += ($curdir == 0|3) ?? 1 !! -1; + $curdir = ($curdir + 1) % 4; + return; + } + + my sub next-step (@cursor, $curdir) { + @cursor >>+<< @directions[$curdir]; + } + + my sub inside-boundary(@cursor, @boundary) of Bool { + return ((@boundary[3] < @cursor[0] < @boundary[1]) + and (@boundary[0] < @cursor[1] < @boundary[2])); + } + + return gather { + while inside-boundary(@cursor, @boundary) { + take @M[@cursor[1]][@cursor[0]]; + + my @next = next-step(@cursor, $curdir); + unless inside-boundary(@next, @boundary) { + turn($curdir, @boundary); + @next = next-step(@cursor, $curdir); + } + + @cursor = @next; + } + }; +} + +sub print-matrix(@matrix) { + for ^@matrix -> $y { + say "[" ~ @matrix[$y].map(-> $it { sprintf("%2d", $it) }).join(" ") ~ "]"; + } +} + +my @examples = ( + [[1,2], + [3,4]], + [[1,2,3], + [4,5,6]], + [[1,2,3], + [4,5,6], + [7,8,9]], + [[ 1, 2, 3, 4 ], + [ 5, 6, 7, 8 ], + [ 9, 10, 11, 12 ], + [ 13, 14, 15, 16 ]], + [[1,2,3], + [4,5,6], + [7,8,9], + [10,11,12],], +); + +for @examples -> @M { + say "----\nInput: "; + print-matrix(@M); + say "Output: " ~ spiral-matrix(@M).gist; +} |
