aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-10 17:41:01 +0100
committerGitHub <noreply@github.com>2021-05-10 17:41:01 +0100
commit6426091d4daab4d77a550263fb81662298bcbfb0 (patch)
treea00fb83ed6d531b3592a2501e03a6b02157236f8
parent5e535c759ec1826d95b3f49c4033db88803975ca (diff)
parentb82a359f9ab72a3160797bf2d0aba8a0b23fe6ed (diff)
downloadperlweeklychallenge-club-6426091d4daab4d77a550263fb81662298bcbfb0.tar.gz
perlweeklychallenge-club-6426091d4daab4d77a550263fb81662298bcbfb0.tar.bz2
perlweeklychallenge-club-6426091d4daab4d77a550263fb81662298bcbfb0.zip
Merge pull request #4054 from seaker/master
challenge 112, Feng Chang's raku solutions
-rwxr-xr-xchallenge-112/feng-chang/raku/ch-1.raku23
-rwxr-xr-xchallenge-112/feng-chang/raku/ch-2.raku15
2 files changed, 38 insertions, 0 deletions
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..cf4cf08e00
--- /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); # 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) } # omit '.'
+ when '..' { @dirs.splice($i-1, 2); --$i; } # omit '..' and its upper directory
+ default { ++$i }
+ }
+ }
+
+ CATCH {
+ $*ERR.put: 'cannot walk beyond root'; # guard against excessive '..'s
+ 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..b674d7b518
--- /dev/null
+++ b/challenge-112/feng-chang/raku/ch-2.raku
@@ -0,0 +1,15 @@
+#!/bin/env raku
+
+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; # use memory to speed up counting
+ @memo[$n] = walk($n-1) + walk($n-2) unless @memo[$n].defined;
+ @memo[$n];
+}
+
+sub MAIN(UInt:D $n) { put walk($n) }