diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-05-16 07:39:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-16 07:39:36 +0100 |
| commit | e0d74ccd60a3f9a88f12eb234d9dea112f1b35ed (patch) | |
| tree | 6793f61af67d32c0acadc933fd0da17fe183ebf9 /challenge-112 | |
| parent | 7f41e41927d72951f1c8e0943770ac841a50c776 (diff) | |
| parent | f1831f1d20aaf1f1fcabfb66983550731913c6dd (diff) | |
| download | perlweeklychallenge-club-e0d74ccd60a3f9a88f12eb234d9dea112f1b35ed.tar.gz perlweeklychallenge-club-e0d74ccd60a3f9a88f12eb234d9dea112f1b35ed.tar.bz2 perlweeklychallenge-club-e0d74ccd60a3f9a88f12eb234d9dea112f1b35ed.zip | |
Merge pull request #4080 from E7-87-83/newt
2 Perl scripts, blogpost
Diffstat (limited to 'challenge-112')
| -rw-r--r-- | challenge-112/cheok-yin-fung/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-112/cheok-yin-fung/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-112/cheok-yin-fung/perl/ch-2.pl | 34 |
3 files changed, 58 insertions, 0 deletions
diff --git a/challenge-112/cheok-yin-fung/blog.txt b/challenge-112/cheok-yin-fung/blog.txt new file mode 100644 index 0000000000..a0a6007fb0 --- /dev/null +++ b/challenge-112/cheok-yin-fung/blog.txt @@ -0,0 +1 @@ +https://e7-87-83.github.io/coding/challenge_112.html diff --git a/challenge-112/cheok-yin-fung/perl/ch-1.pl b/challenge-112/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..3156d06727 --- /dev/null +++ b/challenge-112/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/perl +# The Weekly Challenge 112 +# Task 1 Canonical Path +use strict; +use warnings; + +my $origin = $ARGV[0] || "/a//b/c/../../"; + +my @directories = grep { $_ ne "" && $_ ne "." } split "/", $origin; + +my @new_dirs = (); + +for (@directories) { + if ($_ ne "..") { + push @new_dirs, $_; + } + else { + pop @new_dirs; + } +} + +print ( "/" . (join "/", @new_dirs)); +print "\n"; diff --git a/challenge-112/cheok-yin-fung/perl/ch-2.pl b/challenge-112/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..451a2aaa73 --- /dev/null +++ b/challenge-112/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl +# The Weekly Challenge 112 +# Task 2 Climb Stairs +use strict; +use warnings; +use Algorithm::Combinatorics qw /combinations/; + +my $n = $ARGV[0] || 5; + +if ($n <= 1) { + print "For one step to climb, there is only one option: \n1\n"; +} else { + +# ================== BEGIN: MAIN BODY OF THE SCRIPT ========= +my @all; + +for my $i ($n%2+$n/2 .. $n-1) { + my $iter = combinations([0..$i-1] , ($n-$i) ); + my $str = "1" x $i; + while (my $c = $iter->next) { + my $str_clone = $str; + substr($str_clone, $_, 1) = "2" for (@{$c}); + push @all, $str_clone; + } +} + +push @all , "1" x $n; +print "For $n steps to climb, the number of options is ", scalar @all, ":\n"; +print join "\n", @all; +print "\n"; + +# ==================== END: MAIN BODY OF THE SCRIPT ========= + +} |
