From f1831f1d20aaf1f1fcabfb66983550731913c6dd Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sun, 16 May 2021 13:33:06 +0800 Subject: 2 Perl scripts, blogpost --- challenge-112/cheok-yin-fung/blog.txt | 1 + challenge-112/cheok-yin-fung/perl/ch-1.pl | 23 +++++++++++++++++++++ challenge-112/cheok-yin-fung/perl/ch-2.pl | 34 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 challenge-112/cheok-yin-fung/blog.txt create mode 100644 challenge-112/cheok-yin-fung/perl/ch-1.pl create mode 100644 challenge-112/cheok-yin-fung/perl/ch-2.pl 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 ========= + +} -- cgit