diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-06-16 15:48:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-16 15:48:04 +0100 |
| commit | 6023beb284ac68e6a8cd67094aa76b55daa20ad9 (patch) | |
| tree | 77cd0ba501ebc5d0c0fcb36764e55c98b4869b3c | |
| parent | 0aa46d7e327eb82bd447279a1891f0d1873c46ff (diff) | |
| parent | 5e631f4b325d6ef41562ec1ef955e49e7da4ab75 (diff) | |
| download | perlweeklychallenge-club-6023beb284ac68e6a8cd67094aa76b55daa20ad9.tar.gz perlweeklychallenge-club-6023beb284ac68e6a8cd67094aa76b55daa20ad9.tar.bz2 perlweeklychallenge-club-6023beb284ac68e6a8cd67094aa76b55daa20ad9.zip | |
Merge pull request #4279 from E7-87-83/newt
week 117
| -rw-r--r-- | challenge-117/cheok-yin-fung/awk/ch-1.awk | 22 | ||||
| -rw-r--r-- | challenge-117/cheok-yin-fung/perl/ch-1.pl | 1 | ||||
| -rw-r--r-- | challenge-117/cheok-yin-fung/perl/ch-2.pl | 63 |
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"; + |
