aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-16 07:42:11 +0100
committerGitHub <noreply@github.com>2021-05-16 07:42:11 +0100
commit1cc703718a1bd751176c78aafc5316e6c39434fd (patch)
treec8360b88ba96a9b545ea67c25a516b1562174b6e
parent3817ee04777d28e4fd0bfebc75ec31bb4ec329fe (diff)
parentf3fede6d074f588a630fbfe141c2aa96e55563f6 (diff)
downloadperlweeklychallenge-club-1cc703718a1bd751176c78aafc5316e6c39434fd.tar.gz
perlweeklychallenge-club-1cc703718a1bd751176c78aafc5316e6c39434fd.tar.bz2
perlweeklychallenge-club-1cc703718a1bd751176c78aafc5316e6c39434fd.zip
Merge pull request #4079 from aaronreidsmith/challenge-112
Challenge 112 - Raku
-rw-r--r--challenge-112/aaronreidsmith/blog.txt1
-rw-r--r--challenge-112/aaronreidsmith/raku/ch-1.raku43
-rw-r--r--challenge-112/aaronreidsmith/raku/ch-2.raku38
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-112/aaronreidsmith/blog.txt b/challenge-112/aaronreidsmith/blog.txt
new file mode 100644
index 0000000000..3a9b06e61b
--- /dev/null
+++ b/challenge-112/aaronreidsmith/blog.txt
@@ -0,0 +1 @@
+https://aaronreidsmith.github.io/blog/perl-weekly-challenge-112/
diff --git a/challenge-112/aaronreidsmith/raku/ch-1.raku b/challenge-112/aaronreidsmith/raku/ch-1.raku
new file mode 100644
index 0000000000..748081b826
--- /dev/null
+++ b/challenge-112/aaronreidsmith/raku/ch-1.raku
@@ -0,0 +1,43 @@
+#!/usr/bin/env raku
+
+sub challenge(Str $path is copy) returns Str {
+ die "Must be an absolute path" unless $path.starts-with('/');
+
+ $path = $path.substr(1);
+ $path = $path.subst(/\/ ** 2..*/, '/', :global);
+ $path = $path.substr(0, *-1) if $path.ends-with('/');
+
+ my @output;
+ for $path.split('/') -> $dir {
+ given $dir {
+ when '.' { Nil }
+ when '..' {
+ die "Illegal path" if @output.elems == 0;
+ @output.pop;
+ }
+ default { @output.push($dir) }
+ }
+ }
+ '/' ~ @output.join('/')
+}
+
+multi sub MAIN(Str $path) {
+ say challenge($path);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ ('/a/', '/a'),
+ ('/a/b//c/', '/a/b/c'),
+ ('/a/b/c/../../', '/a'),
+ ('/a/b/./c/./d/', '/a/b/c/d')
+ );
+
+ for @tests -> ($input, $expected) {
+ is(challenge($input), $expected);
+ }
+
+ done-testing;
+}
diff --git a/challenge-112/aaronreidsmith/raku/ch-2.raku b/challenge-112/aaronreidsmith/raku/ch-2.raku
new file mode 100644
index 0000000000..4ef27f4cbf
--- /dev/null
+++ b/challenge-112/aaronreidsmith/raku/ch-2.raku
@@ -0,0 +1,38 @@
+#!/usr/bin/env raku
+
+use experimental :cached;
+
+sub challenge(
+ $max where Int,
+ @paths where Positional = (^$max),
+ $current-step where Int = 0
+) is cached {
+ given $current-step {
+ when * == $max { 1 }
+ when * > $max { 0 }
+ default {
+ challenge($max, @paths, $current-step + 1) +
+ challenge($max, @paths, $current-step + 2)
+ }
+ }
+}
+
+multi sub MAIN(Int $n) {
+ say challenge($n);
+}
+
+multi sub MAIN(Bool :$test) {
+ use Test;
+
+ my @tests = (
+ (3, 3),
+ (4, 5),
+ (10, 89)
+ );
+
+ for @tests -> ($n, $expected) {
+ is(challenge($n), $expected);
+ }
+
+ done-testing;
+}