aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-05-16 22:43:56 +0100
committerGitHub <noreply@github.com>2021-05-16 22:43:56 +0100
commitbd866cb9cb06a1b3f4ffc3ed51f86d3f742351fa (patch)
tree9b8f4f6371a883944adb09fd02d0f99420bd0e71 /challenge-112
parent06834eb68e5143e7cfae8cf6626b66a74191394f (diff)
parent4974b0ebf410017964822ab6a1c43bff58487f92 (diff)
downloadperlweeklychallenge-club-bd866cb9cb06a1b3f4ffc3ed51f86d3f742351fa.tar.gz
perlweeklychallenge-club-bd866cb9cb06a1b3f4ffc3ed51f86d3f742351fa.tar.bz2
perlweeklychallenge-club-bd866cb9cb06a1b3f4ffc3ed51f86d3f742351fa.zip
Merge pull request #4089 from PerlBoy1967/branch-for-challenge-112
Task 1 & 2
Diffstat (limited to 'challenge-112')
-rwxr-xr-xchallenge-112/perlboy1967/perl/ch-1.pl43
-rwxr-xr-xchallenge-112/perlboy1967/perl/ch-2.pl58
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-112/perlboy1967/perl/ch-1.pl b/challenge-112/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..379636526c
--- /dev/null
+++ b/challenge-112/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,43 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 112
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-112/#TASK1
+#
+# Task 1 - Canonical Path
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+use Test::More;
+
+# Prototype(s)
+sub canonicalPath($);
+
+my %test = (
+ '/a/' => '/a',
+ '/a/.' => '/a',
+ '/a/b//c/' => '/a/b/c',
+ '/a/b/./c/' => '/a/b/c',
+ '/a/./b' => '/a/b',
+ '/a/b/../c/' => '/a/c',
+ '/a/b/c/../..' => '/a',
+ '/a/..' => '/',
+);
+
+foreach my $path (sort keys %test) {
+ is(canonicalPath($path), $test{$path});
+}
+
+done_testing;
+
+sub canonicalPath($) {
+ my ($p) = @_;
+
+ do {
+ $p =~ s#(//|/\./|/\.\Z)#/#g;
+ } while ($p =~ s#[^/]+/\.\.##);
+ return $p =~ s#(?!\A)/$##r;
+}
diff --git a/challenge-112/perlboy1967/perl/ch-2.pl b/challenge-112/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..a5c88ac80e
--- /dev/null
+++ b/challenge-112/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+
+# Perl Weekly Challenge - 112
+# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-112/#TASK2
+#
+# Task 2 - Climb Stairs
+#
+# Author: Niels 'PerlBoy' van Dijke
+
+use v5.16;
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Deep;
+
+use Data::Printer;
+
+# Prototype(s)
+sub climbStairs($\@);
+
+my @stepOptions = (1,2);
+
+cmp_deeply (climbStairs(3,@stepOptions),
+ [[1,1,1],[1,2],[2,1]]);
+cmp_deeply (climbStairs(4,@stepOptions),
+ [[1,1,1,1],[1,1,2],[1,2,1],[2,1,1],[2,2]]);
+
+@stepOptions = (1,2,3);
+cmp_deeply (climbStairs(4,@stepOptions),
+ [[1,1,1,1],[1,1,2],[1,2,1],[1,3],[2,1,1],[2,2],[3,1]]);
+
+done_testing;
+
+sub climbStairs($\@) {
+ my ($height,$arStepOptions) = @_;
+ my ($solutions,$steps) = ([],[]);
+
+ _climb($height,$arStepOptions,$steps,$solutions);
+
+ return $solutions;
+}
+
+sub _climb {
+ my ($height,$arStepOptions,$arSteps,$arSolutions) = @_;
+
+ my @steps = @$arSteps;
+
+ foreach my $step (@$arStepOptions) {
+ if ($height-$step >= 0) {
+ push(@steps,$step);
+ _climb($height-$step,$arStepOptions,\@steps,$arSolutions);
+ push(@$arSolutions,[@steps])
+ if ($height-$step == 0);
+ pop(@steps);
+ }
+ }
+}