diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-11 11:00:41 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-11 11:00:41 +0100 |
| commit | 085b31294927ff79760c40ca9b59d6016e001646 (patch) | |
| tree | c2ba155c6325bfa30f661d3a3e4202065b80c3fa | |
| parent | fac59f5a38614d75556e7ba31d6f64a160fdc877 (diff) | |
| parent | d88aed1b2621a83900ba60649f359624952d8a00 (diff) | |
| download | perlweeklychallenge-club-085b31294927ff79760c40ca9b59d6016e001646.tar.gz perlweeklychallenge-club-085b31294927ff79760c40ca9b59d6016e001646.tar.bz2 perlweeklychallenge-club-085b31294927ff79760c40ca9b59d6016e001646.zip | |
Merge pull request #4062 from jacoby/master
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; +} |
