aboutsummaryrefslogtreecommitdiff
path: root/challenge-112
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-13 11:46:35 +0100
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-05-13 11:46:35 +0100
commit15502017ba3d59e96e00453936f6ea9d075f00ae (patch)
tree1d8a72f00f51b79536c2e8ad6d9a75db04da2346 /challenge-112
parent1ac9b132897be0a8ccaaee70b607585fefb95a0d (diff)
downloadperlweeklychallenge-club-15502017ba3d59e96e00453936f6ea9d075f00ae.tar.gz
perlweeklychallenge-club-15502017ba3d59e96e00453936f6ea9d075f00ae.tar.bz2
perlweeklychallenge-club-15502017ba3d59e96e00453936f6ea9d075f00ae.zip
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-112')
-rw-r--r--challenge-112/laurent-rosenfeld/blog.txt1
-rw-r--r--challenge-112/laurent-rosenfeld/perl/ch-1.pl21
-rw-r--r--challenge-112/laurent-rosenfeld/perl/ch-2.pl39
-rw-r--r--challenge-112/laurent-rosenfeld/raku/ch-1.raku19
-rw-r--r--challenge-112/laurent-rosenfeld/raku/ch-2.raku35
5 files changed, 115 insertions, 0 deletions
diff --git a/challenge-112/laurent-rosenfeld/blog.txt b/challenge-112/laurent-rosenfeld/blog.txt
new file mode 100644
index 0000000000..df8edd3267
--- /dev/null
+++ b/challenge-112/laurent-rosenfeld/blog.txt
@@ -0,0 +1 @@
+http://blogs.perl.org/users/laurent_r/2021/05/perl-weekly-challenge-112-canonical-path-and-climb-stairs.html
diff --git a/challenge-112/laurent-rosenfeld/perl/ch-1.pl b/challenge-112/laurent-rosenfeld/perl/ch-1.pl
new file mode 100644
index 0000000000..b118f809dd
--- /dev/null
+++ b/challenge-112/laurent-rosenfeld/perl/ch-1.pl
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+use feature "say";
+
+my @tests = ('/a/', '/a/b//c/', '/a/b/c/../..', '/a/../../b/', '/a/././b/');
+TEST: for my $path (@tests) {
+ my $p = $path;
+ $path =~ s|\/\/+|/|g;
+ $path =~ s!^\/|\/$!!g;
+ my @path_items;
+ for my $item (split /\/+/, $path) {
+ next if $item eq '.';
+ if ($item eq '..') {
+ warn "Invalid path $p" and next TEST unless @path_items;
+ pop @path_items;
+ } else {
+ push @path_items, $item;
+ }
+ };
+ say "$p => /", join '/', @path_items;
+}
diff --git a/challenge-112/laurent-rosenfeld/perl/ch-2.pl b/challenge-112/laurent-rosenfeld/perl/ch-2.pl
new file mode 100644
index 0000000000..c429de82e3
--- /dev/null
+++ b/challenge-112/laurent-rosenfeld/perl/ch-2.pl
@@ -0,0 +1,39 @@
+use strict;
+use warnings;
+use feature "say";
+
+my @result;
+
+sub print_result {
+ my $count = 0;
+ for my $solution (@result) {
+ print "\tOption ", ++$count, ": ";
+ my @step_list;
+ push @step_list, "$_ " . ($_ =~ /1/ ? "step " : "steps") for @$solution;
+ say join " + ", @step_list;
+ }
+ say "";
+}
+
+sub try_steps {
+ my ($nb_steps, $sum, @curr) = @_;
+ for my $new_step (1, 2) {
+ my $new_sum = $sum + $new_step;
+ next if $new_sum > $nb_steps;
+ my @new_cur = (@curr, $new_step);
+ if ($new_sum == $nb_steps) {
+ push @result, \@new_cur;
+ last;
+ } else {
+ try_steps($nb_steps, $new_sum, @new_cur);
+ }
+ }
+}
+
+for my $target (3, 4, 5) {
+ @result = ();
+ try_steps $target, 0, ();
+ say 'Input: $n = ', $target;
+ say "Output: ", scalar @result;
+ print_result;
+}
diff --git a/challenge-112/laurent-rosenfeld/raku/ch-1.raku b/challenge-112/laurent-rosenfeld/raku/ch-1.raku
new file mode 100644
index 0000000000..f479cae6dd
--- /dev/null
+++ b/challenge-112/laurent-rosenfeld/raku/ch-1.raku
@@ -0,0 +1,19 @@
+use v6
+
+my @tests = </a/ /a/b//c/ /a/b/c/../.. /a/../../b/>;
+for @tests <-> $path {
+ my $p = $path;
+ $path ~~ s:g|'//'+|/|;
+ $path ~~ s:g!^'/' | '/'$!!;
+ my @path-items;
+ for split /'/'+/, $path -> $item {
+ next if $item eq '.';
+ if $item eq '..' {
+ die "Invalid path $p" unless @path-items;
+ pop @path-items;
+ } else {
+ push @path-items, $item;
+ }
+ };
+ say "$p => /", @path-items.join('/');
+}
diff --git a/challenge-112/laurent-rosenfeld/raku/ch-2.raku b/challenge-112/laurent-rosenfeld/raku/ch-2.raku
new file mode 100644
index 0000000000..e9cd8c5203
--- /dev/null
+++ b/challenge-112/laurent-rosenfeld/raku/ch-2.raku
@@ -0,0 +1,35 @@
+use v6;
+
+sub print-result {
+ my $count = 0;
+ for @*result -> @solution {
+ print "\tOption ", ++$count, ": ";
+ my @step_list;
+ push @step_list, "$_ " ~ ($_ ~~ /1/ ?? "step " !! "steps") for @solution;
+ say join " + ", @step_list;
+ }
+ say "";
+}
+
+sub try-steps ($nb-steps, @curr) {
+ for 1, 2 -> $new-step {
+ my @new-cur = (|@curr, $new-step);
+ my $sum = [+] @new-cur;
+ next if $sum > $nb-steps;
+ if $sum == $nb-steps {
+ push @*result, @new-cur;
+ last;
+ } else {
+ try-steps $nb-steps, @new-cur;
+ }
+ }
+}
+
+for 3, 4, 5 -> $target {
+ my @*result;
+ try-steps $target, [];
+ say 'Input: $n = ', $target;
+ say "Output: ", @*result.elems;
+ # say @*result;
+ print-result;
+}