aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-07-04 22:17:19 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-07-04 22:17:19 +0800
commitcdfef3f9404041b4c7936a26532757e63fd4fcef (patch)
tree33edb1bfc6a8b87e53b235dbdaf57ec74ea5da27
parent9373cec8d2777dd4a0f9352738b3649d34e70cb0 (diff)
downloadperlweeklychallenge-club-cdfef3f9404041b4c7936a26532757e63fd4fcef.tar.gz
perlweeklychallenge-club-cdfef3f9404041b4c7936a26532757e63fd4fcef.tar.bz2
perlweeklychallenge-club-cdfef3f9404041b4c7936a26532757e63fd4fcef.zip
wk 119
-rw-r--r--challenge-119/cheok-yin-fung/bash/ch-1.sh18
-rw-r--r--challenge-119/cheok-yin-fung/bash/ch-2.sh34
-rw-r--r--challenge-119/cheok-yin-fung/perl/ch-2.pl34
3 files changed, 73 insertions, 13 deletions
diff --git a/challenge-119/cheok-yin-fung/bash/ch-1.sh b/challenge-119/cheok-yin-fung/bash/ch-1.sh
new file mode 100644
index 0000000000..2d991875cc
--- /dev/null
+++ b/challenge-119/cheok-yin-fung/bash/ch-1.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+# The Weekly Challenge - 119
+# Task 1 Swap Nibbles
+# Usage: $ chmod +x ch-1.sh
+# $ ./ch-1.sh N
+
+N=$1
+
+if [ $N -gt 255 ] || [ $N -lt 1 ] ;
+then
+ echo "Integer within 1 to 255."
+ exit ;
+fi
+
+A=$(($N/16))
+B=$(($N%16))
+
+echo $(($B*16+$A))
diff --git a/challenge-119/cheok-yin-fung/bash/ch-2.sh b/challenge-119/cheok-yin-fung/bash/ch-2.sh
new file mode 100644
index 0000000000..f9db093116
--- /dev/null
+++ b/challenge-119/cheok-yin-fung/bash/ch-2.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+# The Weekly Challenge - 119
+# Task 2 Sequence without 1-on-1
+# Usage: $ chmod +x ch-2.sh
+# $ ./ch-2.sh N
+
+N=$1
+arr=(0 1 2 3)
+
+x=0
+y=0
+while [ ${#arr[@]} -lt $N ]
+do
+ y=${#arr[@]}
+ for ((h=$x+1; $h<=$y; h=$h+1))
+ do
+ if [ $(($[arr[$h]] % 10)) -ne 1 ] ;
+ then arr+=($[arr[$h]]*10+1) ;
+ fi
+ arr+=($[arr[$h]]*10+2)
+ arr+=($[arr[$h]]*10+3)
+ done
+ x=$y
+done
+
+echo $[arr[$N]]
+
+# ref:
+# https://stackoverflow.com/questions/19417015/how-to-copy-an-array-in-bash
+# https://linuxhandbook.com/bash-arrays/
+# http://rosettacode.org/wiki/Loops/For_with_a_specified_step#Bourne_Shell
+# Bash Guide for Beginners
+# Classic Shell Scripting
+
diff --git a/challenge-119/cheok-yin-fung/perl/ch-2.pl b/challenge-119/cheok-yin-fung/perl/ch-2.pl
index 6a0f1a1759..802057f8e0 100644
--- a/challenge-119/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-119/cheok-yin-fung/perl/ch-2.pl
@@ -1,25 +1,33 @@
#!/usr/bin/perl
# The Weekly Challenge - 119
# Task 2 Sequence without 1-on-1
+# Usage: $ ch-2.pl N
use strict;
use warnings;
+use Test::More tests => 3;
-my $N = $ARGV[0];
+my $N = $ARGV[0] || 0;
+print noo($N), "\n";
-my @arr = (0,1,2,3);
+sub noo {
+ my $p = $_[0];
+ my @arr = (0,1,2,3);
-my $x = 0, my $y;
+ my $x = 0, my $y;
-while ($#arr < $N) {
- $y = $#arr;
- for my $here ($x+1..$y) {
- push @arr, $arr[$here]."1"
- if substr($arr[$here], -1, 1) ne "1";
- push @arr, $arr[$here]."2";
- push @arr, $arr[$here]."3";
+ while ($#arr < $p) {
+ $y = $#arr;
+ for my $here ($x+1..$y) {
+ push @arr, $arr[$here]."1"
+ if substr($arr[$here], -1, 1) ne "1";
+ push @arr, $arr[$here]."2";
+ push @arr, $arr[$here]."3";
+ }
+ $x = $y;
}
- $x = $y;
+ return $arr[$p];
}
-
-print $arr[$N], "\n";
+ok (noo(5) == 13, "Example 1");
+ok (noo(10) == 32, "Example 2");
+ok (noo(60) == 2223, "Example 3");