aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-06-16 21:13:06 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-06-16 21:13:06 +0800
commit1dd7eedea237292a3fb563ecff37d004c3bbc772 (patch)
tree9ea4163ab7be90ac90f8c76f0312e3cdca9e7046
parent8d70c5ccf601afe31d60120a3b8e1e317a097e52 (diff)
downloadperlweeklychallenge-club-1dd7eedea237292a3fb563ecff37d004c3bbc772.tar.gz
perlweeklychallenge-club-1dd7eedea237292a3fb563ecff37d004c3bbc772.tar.bz2
perlweeklychallenge-club-1dd7eedea237292a3fb563ecff37d004c3bbc772.zip
week 117
-rw-r--r--challenge-117/cheok-yin-fung/awk/ch-1.awk22
-rw-r--r--challenge-117/cheok-yin-fung/perl/ch-1.pl1
-rw-r--r--challenge-117/cheok-yin-fung/perl/ch-2.pl63
3 files changed, 86 insertions, 0 deletions
diff --git a/challenge-117/cheok-yin-fung/awk/ch-1.awk b/challenge-117/cheok-yin-fung/awk/ch-1.awk
new file mode 100644
index 0000000000..9d6264cbda
--- /dev/null
+++ b/challenge-117/cheok-yin-fung/awk/ch-1.awk
@@ -0,0 +1,22 @@
+# The Weekly Challenge 117
+# Task 1 Missing Row
+# Usage: awk -f 'ch-1.awk' < [text file]
+
+BEGIN {
+ max = 15
+ separator = ","
+ for (i = 1; i <= max; i++)
+ arr[i] = "F"
+}
+
+{
+ y = index($0, separator)
+ x = substr($0, 1, y-1)
+ arr[x] = "T"
+}
+
+END {
+ for (i = 1; i <= max; i++)
+ if (arr[i] == "F")
+ print i
+}
diff --git a/challenge-117/cheok-yin-fung/perl/ch-1.pl b/challenge-117/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..79f84cb296
--- /dev/null
+++ b/challenge-117/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1 @@
+while(<>) {/^\s*(\d+)/; $arr{$1} = 1;} for (1..15) {print ($_, "\n") unless $arr{$_};}
diff --git a/challenge-117/cheok-yin-fung/perl/ch-2.pl b/challenge-117/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..40909a2072
--- /dev/null
+++ b/challenge-117/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl
+# The Weekly Challenge 117
+# Task 2 Find Possible Paths
+# Usage: ch-2.pl [size of triangle]
+use strict;
+use warnings;
+
+my $N = $ARGV[0];
+
+die "Usage: ch-2.pl [size of triangle]\n"
+ unless $ARGV[0] && $ARGV[0] =~ /^\d+$/;
+
+my @newarr = (1);
+
+while (scalar @newarr <= $N) {
+ my @arr = @newarr;
+ @newarr = (1);
+ my $ord = scalar @arr;
+ for my $i (1.. $ord - 1) {
+ push @newarr, $arr[$i-1] + $arr[$i] + $newarr[$i-1];
+ }
+ $newarr[$ord] = $arr[$ord-1] + $newarr[$ord-1];
+}
+
+print "Number of Paths: ", $newarr[-1];
+print "\n";
+
+# * * * * * * *
+sub ways {
+ my $K = $_[0];
+ my $newstp = [ [''] ];
+
+ while (scalar @{$newstp} <= $K) {
+ my $stp = $newstp;
+ my $ord = scalar @{$stp};
+ $newstp = [ [ 'L' x $ord ] ];
+ for my $i (1.. $ord - 1) {
+ push @{$newstp->[$i]}, $stp->[$i-1]->[$_] . 'R'
+ for (0.. scalar $stp->[$i-1]->@* - 1 );
+ push @{$newstp->[$i]}, $stp->[$i]->[$_] . 'L'
+ for (0.. scalar $stp->[$i]->@* - 1 );
+ push @{$newstp->[$i]}, $newstp->[$i-1]->[$_] . 'H'
+ for (0.. scalar $newstp->[$i-1]->@* - 1 );
+ }
+
+ push @{$newstp->[$ord]}, $stp->[$ord-1]->[$_] . 'R'
+ for (0.. scalar @{$stp->[$ord-1]} - 1 );
+ push @{$newstp->[$ord]}, $newstp->[$ord-1]->[$_] . 'H'
+ for (0.. scalar @{$newstp->[$ord-1]} - 1 );
+ }
+
+ return $newstp->[-1];
+}
+
+
+if ($N <= 10) {
+ print join " ", @{ways($N)};
+}
+else {
+ print "...Too many possible paths!";
+}
+print "\n";
+