From 3ba59e2c4ac168b4a6983cb22300568b9b73fac7 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 10 May 2021 18:33:06 +0800 Subject: challenge 112, raku solutions --- challenge-112/feng-chang/raku/ch-1.raku | 23 +++++++++++++++++++++++ challenge-112/feng-chang/raku/ch-2.raku | 15 +++++++++++++++ 2 files changed, 38 insertions(+) create mode 100755 challenge-112/feng-chang/raku/ch-1.raku create mode 100755 challenge-112/feng-chang/raku/ch-2.raku diff --git a/challenge-112/feng-chang/raku/ch-1.raku b/challenge-112/feng-chang/raku/ch-1.raku new file mode 100755 index 0000000000..5175350730 --- /dev/null +++ b/challenge-112/feng-chang/raku/ch-1.raku @@ -0,0 +1,23 @@ +#!/bin/env raku + +sub MAIN(Str:D $path where * ~~ rx{^'/'}) { + my @dirs = $path.split(rx{'/'+}).tail(*-1); + @dirs.pop unless @dirs[*-1]; + my $i = 0; + try { + while $i < @dirs.elems { + given @dirs[$i] { + when '.' { @dirs.splice($i, 1) } + when '..' { @dirs.splice($i-1, 2); --$i; } + default { ++$i } + } + } + + CATCH { + $*ERR.put: 'cannot walk beyond root'; + exit; + } + } + + put '/', @dirs.join('/'); +} diff --git a/challenge-112/feng-chang/raku/ch-2.raku b/challenge-112/feng-chang/raku/ch-2.raku new file mode 100755 index 0000000000..f0ea1a093b --- /dev/null +++ b/challenge-112/feng-chang/raku/ch-2.raku @@ -0,0 +1,15 @@ +#!/bin/env raku + +proto walk(UInt:D --> UInt:D) {*} + +multi walk(0) { 1 } +multi walk(1) { 1 } +multi walk(2) { 2 } + +multi walk($n) { + state @memo; + @memo[$n] = walk($n-1) + walk($n-2) unless @memo[$n].defined; + @memo[$n]; +} + +sub MAIN(UInt:D $n) { put walk($n) } -- cgit From 144a4fa80fbfa1d1356d05d1da1a1e7ff4cd6ba9 Mon Sep 17 00:00:00 2001 From: 冯昶 Date: Mon, 10 May 2021 18:44:40 +0800 Subject: add comments --- challenge-112/feng-chang/raku/ch-1.raku | 10 +++++----- challenge-112/feng-chang/raku/ch-2.raku | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/challenge-112/feng-chang/raku/ch-1.raku b/challenge-112/feng-chang/raku/ch-1.raku index 5175350730..cf4cf08e00 100755 --- a/challenge-112/feng-chang/raku/ch-1.raku +++ b/challenge-112/feng-chang/raku/ch-1.raku @@ -1,20 +1,20 @@ #!/bin/env raku sub MAIN(Str:D $path where * ~~ rx{^'/'}) { - my @dirs = $path.split(rx{'/'+}).tail(*-1); - @dirs.pop unless @dirs[*-1]; + my @dirs = $path.split(rx{'/'+}).tail(*-1); # put all directory names into an array, omit first empty element + @dirs.pop unless @dirs[*-1]; # omit last element if it is empty my $i = 0; try { while $i < @dirs.elems { given @dirs[$i] { - when '.' { @dirs.splice($i, 1) } - when '..' { @dirs.splice($i-1, 2); --$i; } + when '.' { @dirs.splice($i, 1) } # omit '.' + when '..' { @dirs.splice($i-1, 2); --$i; } # omit '..' and its upper directory default { ++$i } } } CATCH { - $*ERR.put: 'cannot walk beyond root'; + $*ERR.put: 'cannot walk beyond root'; # guard against excessive '..'s exit; } } diff --git a/challenge-112/feng-chang/raku/ch-2.raku b/challenge-112/feng-chang/raku/ch-2.raku index f0ea1a093b..b674d7b518 100755 --- a/challenge-112/feng-chang/raku/ch-2.raku +++ b/challenge-112/feng-chang/raku/ch-2.raku @@ -1,13 +1,13 @@ #!/bin/env raku -proto walk(UInt:D --> UInt:D) {*} +proto walk(UInt:D --> UInt:D) {*} # restrict dispatch pattern multi walk(0) { 1 } multi walk(1) { 1 } multi walk(2) { 2 } multi walk($n) { - state @memo; + state @memo; # use memory to speed up counting @memo[$n] = walk($n-1) + walk($n-2) unless @memo[$n].defined; @memo[$n]; } -- cgit