aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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";
+