diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-05-10 18:24:45 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-05-10 18:24:45 -0400 |
| commit | d88aed1b2621a83900ba60649f359624952d8a00 (patch) | |
| tree | 0c6dcccf3f50827a075e8afcef074e41c3a3eea2 | |
| parent | 765d55c53dabebcd6b5832e3396e6d4fdcbb56e1 (diff) | |
| download | perlweeklychallenge-club-d88aed1b2621a83900ba60649f359624952d8a00.tar.gz perlweeklychallenge-club-d88aed1b2621a83900ba60649f359624952d8a00.tar.bz2 perlweeklychallenge-club-d88aed1b2621a83900ba60649f359624952d8a00.zip | |
Task 1 Task 2 and Blog URL
| -rw-r--r-- | challenge-112/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-112/dave-jacoby/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-112/dave-jacoby/perl/ch-2.pl | 35 |
3 files changed, 72 insertions, 0 deletions
diff --git a/challenge-112/dave-jacoby/blog.txt b/challenge-112/dave-jacoby/blog.txt new file mode 100644 index 0000000000..084652708e --- /dev/null +++ b/challenge-112/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/05/10/one-step-beyond-perl-weekly-challenge-112.html
\ No newline at end of file diff --git a/challenge-112/dave-jacoby/perl/ch-1.pl b/challenge-112/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..2de240a1c5 --- /dev/null +++ b/challenge-112/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +my @paths; +push @paths, "/a/"; +push @paths, "/a/b/./c/"; +push @paths, "/a/b//c/"; +push @paths, "/a/b/c/../d/.."; +push @paths, "/a/b/c/../.."; +push @paths, "/a/b/c/"; + +for my $path (@paths) { + my $cpath = canonical_path($path); + say <<"END"; + path: $path + canonical: $cpath +END +} + +sub canonical_path ($path) { + while ($path =~ m{/\w+/\.\.}mix + || $path =~ m{//}mix + || $path =~ m{/\./}mix ) + { + $path =~ s{/\w+/\.\.}{/}mix; + $path =~ s{//}{/}mix; + $path =~ s{/\./}{/}mix; + } + $path =~ s{/$}{}mix; + $path = qq{/$path} unless $path =~ m{^/}mix; + return $path; +} diff --git a/challenge-112/dave-jacoby/perl/ch-2.pl b/challenge-112/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..b5c5e024e6 --- /dev/null +++ b/challenge-112/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +my @values = ( 3, 4, 5 ); +@values = @ARGV if @ARGV; + +for my $v (@values) { + my $c = 1; + say '-' x 20; + my @steps = climb_stairs($v); + say qq{INPUT: $v}; + say qq{OUTPUT: } . scalar @steps; + for my $opt (@steps) { + say qq{\tOption $c: $opt}; + $c++; + } +} + +sub climb_stairs ( $v, $max_steps = 2 ) { + my @output; + for my $n ( 1 .. $max_steps ) { + my $step = $n < 2 ? '1 step' : "$n steps"; + my $w = $v - $n; + if ( $w > 0 ) { + push @output, + map { $step . ' + ' . $_ } climb_stairs( $w, $max_steps ); + } + elsif ( $w == 0 ) { push @output, $step; } + } + return @output; +} |
