From faa90e31db32bf327a43f0d1761bdc0b7e18edae Mon Sep 17 00:00:00 2001 From: Kang-min Liu Date: Tue, 24 Nov 2020 10:10:18 +0900 Subject: @gugod's solution to pwc 088.2 --- challenge-088/gugod/raku/ch-2.raku | 77 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 challenge-088/gugod/raku/ch-2.raku diff --git a/challenge-088/gugod/raku/ch-2.raku b/challenge-088/gugod/raku/ch-2.raku new file mode 100644 index 0000000000..48f999db52 --- /dev/null +++ b/challenge-088/gugod/raku/ch-2.raku @@ -0,0 +1,77 @@ +my @directions = [ + [ 1, 0], # → + [ 0, 1], # ↓ + [-1, 0], # ← + [ 0,-1], # ↑ +]; + +sub turn ($curdir is rw, @boundary) of Nil { # ⤵ + @boundary[$curdir] += ($curdir == 0|3) ?? 1 !! -1; + $curdir = ($curdir + 1) % 4; + return; +} + +sub next-step (@cursor, $curdir) { + @cursor >>+<< @directions[$curdir]; +} + +sub inside-boundary(@cursor, @boundary) of Bool { + return ((@boundary[3] < @cursor[0] < @boundary[1]) + and (@boundary[0] < @cursor[1] < @boundary[2])); +} + +sub spiral-matrix (@M) { + my $width = @M[0].elems; + my $height = @M.elems; + my @cursor = [0,0]; + + # The index here is arranged in thee way to make it easier to implement turn(). + my $curdir = 0; + my @boundary = [ + -1, # ↑ + $width, # → + $height, # ↓ + -1, # ← + ]; + + my @spiral; + while inside-boundary(@cursor, @boundary) { + @spiral.push(@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; + } + + return @spiral; +} + +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],], +); + +for @examples -> @M { + say "----\nInput: "; + print-matrix(@M); + say "Output: " ~ spiral-matrix(@M).gist; +} -- cgit From bbf75140ed797dd1a263413258ce376022ae952a Mon Sep 17 00:00:00 2001 From: Kang-min Liu Date: Tue, 24 Nov 2020 18:09:15 +0900 Subject: add a test case --- challenge-088/gugod/raku/ch-2.raku | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/challenge-088/gugod/raku/ch-2.raku b/challenge-088/gugod/raku/ch-2.raku index 48f999db52..b630611493 100644 --- a/challenge-088/gugod/raku/ch-2.raku +++ b/challenge-088/gugod/raku/ch-2.raku @@ -64,6 +64,10 @@ my @examples = ( [[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], -- cgit From 5389f71ab4b021f54bebce65de9002c403b81cad Mon Sep 17 00:00:00 2001 From: Kang-min Liu Date: Tue, 24 Nov 2020 18:19:50 +0900 Subject: add a naive solution for 088.1 --- challenge-088/gugod/raku/ch-1.raku | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 challenge-088/gugod/raku/ch-1.raku diff --git a/challenge-088/gugod/raku/ch-1.raku b/challenge-088/gugod/raku/ch-1.raku new file mode 100644 index 0000000000..a5723ffb40 --- /dev/null +++ b/challenge-088/gugod/raku/ch-1.raku @@ -0,0 +1,16 @@ +sub array-of-product (@N) { + my $p = [*] @N; + return @N.map(-> $n { $p / $n }); +} + +my @examples = [ + [5, 2, 1, 4, 3], + [2, 1, 4, 3], + [1, 3, 3, 7], +]; + +for @examples -> @N { + say "Input @N = { @N.gist }"; + say "Output @M = " ~ array-of-product(@N).gist; + say "----"; +} -- cgit From cb8e9359613d4b55fe20222f3ca1975d3c362e1e Mon Sep 17 00:00:00 2001 From: Kang-min Liu Date: Tue, 24 Nov 2020 19:01:43 +0900 Subject: put everything in the same subroutine. --- challenge-088/gugod/raku/ch-2.raku | 72 ++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/challenge-088/gugod/raku/ch-2.raku b/challenge-088/gugod/raku/ch-2.raku index b630611493..e5a21065b7 100644 --- a/challenge-088/gugod/raku/ch-2.raku +++ b/challenge-088/gugod/raku/ch-2.raku @@ -1,32 +1,20 @@ -my @directions = [ - [ 1, 0], # → - [ 0, 1], # ↓ - [-1, 0], # ← - [ 0,-1], # ↑ -]; - -sub turn ($curdir is rw, @boundary) of Nil { # ⤵ - @boundary[$curdir] += ($curdir == 0|3) ?? 1 !! -1; - $curdir = ($curdir + 1) % 4; - return; -} - -sub next-step (@cursor, $curdir) { - @cursor >>+<< @directions[$curdir]; -} - -sub inside-boundary(@cursor, @boundary) of Bool { - return ((@boundary[3] < @cursor[0] < @boundary[1]) - and (@boundary[0] < @cursor[1] < @boundary[2])); -} sub spiral-matrix (@M) { - my $width = @M[0].elems; - my $height = @M.elems; - my @cursor = [0,0]; + # Things don'tt change + my $width := @M[0].elems; + my $height := @M.elems; + my @directions := [ + [ 1, 0], # → + [ 0, 1], # ↓ + [-1, 0], # ← + [ 0,-1], # ↑ + ]; - # The index here is arranged in thee way to make it easier to implement turn(). + # 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, # → @@ -34,20 +22,34 @@ sub spiral-matrix (@M) { -1, # ← ]; - my @spiral; - while inside-boundary(@cursor, @boundary) { - @spiral.push(@M[@cursor[1]][@cursor[0]]); + my sub turn ($curdir is rw, @boundary) of Nil { # ⤵ + @boundary[$curdir] += ($curdir == 0|3) ?? 1 !! -1; + $curdir = ($curdir + 1) % 4; + return; + } - my @next = next-step(@cursor, $curdir); - unless inside-boundary(@next, @boundary) { - turn($curdir, @boundary); - @next = next-step(@cursor, $curdir); - } + my sub next-step (@cursor, $curdir) { + @cursor >>+<< @directions[$curdir]; + } - @cursor = @next; + my sub inside-boundary(@cursor, @boundary) of Bool { + return ((@boundary[3] < @cursor[0] < @boundary[1]) + and (@boundary[0] < @cursor[1] < @boundary[2])); } - return @spiral; + 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) { -- cgit From 37e3de628deb7c89e06cb1a0d66d566cca636bda Mon Sep 17 00:00:00 2001 From: Kang-min Liu Date: Wed, 25 Nov 2020 08:32:44 +0900 Subject: links to blog posts. --- challenge-088/gugod/README | 6 +++--- 1 file changed, 3 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 -- cgit From 551f1b6fa8dd268f4fecbd81a96663ba0a632a17 Mon Sep 17 00:00:00 2001 From: Kang-min Liu Date: Sun, 29 Nov 2020 18:23:35 +0900 Subject: links to blog posts. --- challenge-088/gugod/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-088/gugod/blog.txt 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/ -- cgit From 1aaeb59f9d845cf891e81e9e96ba414b8fedbc8f Mon Sep 17 00:00:00 2001 From: Kang-min Liu Date: Sun, 29 Nov 2020 19:13:59 +0900 Subject: task 1: handle the case when there are zeros. --- challenge-088/gugod/raku/ch-1.raku | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/challenge-088/gugod/raku/ch-1.raku b/challenge-088/gugod/raku/ch-1.raku index a5723ffb40..43e8b650fd 100644 --- a/challenge-088/gugod/raku/ch-1.raku +++ b/challenge-088/gugod/raku/ch-1.raku @@ -1,12 +1,27 @@ sub array-of-product (@N) { - my $p = [*] @N; - return @N.map(-> $n { $p / $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 { -- cgit