From 50fc1b3bb6cd9d6260ef876c1c362b0561d5e2f8 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Mon, 28 Jun 2021 14:21:00 +0800 Subject: something more, something less --- challenge-118/cheok-yin-fung/awk/ch-1.awk | 4 ++-- challenge-118/cheok-yin-fung/perl/ch-2.pl | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-118/cheok-yin-fung/awk/ch-1.awk b/challenge-118/cheok-yin-fung/awk/ch-1.awk index fe7e67c04b..18b20df3cf 100644 --- a/challenge-118/cheok-yin-fung/awk/ch-1.awk +++ b/challenge-118/cheok-yin-fung/awk/ch-1.awk @@ -4,8 +4,8 @@ # or: awk -f 'ch-1.awk' < file_contains_an_integer_on_each_line { - n = $0; - i = 0; + n = $0 + i = 0 while (n > 0) { arr[i] = n % 2 n = int(n / 2) diff --git a/challenge-118/cheok-yin-fung/perl/ch-2.pl b/challenge-118/cheok-yin-fung/perl/ch-2.pl index d74f82c92e..3132a7fd82 100644 --- a/challenge-118/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-118/cheok-yin-fung/perl/ch-2.pl @@ -48,9 +48,9 @@ my @min_paths = (); my $dist_tbl = [[0,3,2,3,2,3,4,5], - [3,2,1,2,3,4,3,4], # >[1][1] = 2 only if the target is not a corner - [2,1,4,3,2,3,4,5], - [3,2,3,2,3,4,3,4], + [3,2,1,2,3,4,3,4], # >[1][1] = 2 only if the target + [2,1,4,3,2,3,4,5], # or the departure position + [3,2,3,2,3,4,3,4], # is not a corner [2,3,2,3,4,3,4,5], [3,4,3,4,3,4,5,4], [4,3,4,3,4,5,4,5], -- cgit From 1b8f559a9cf0f229655543c5458789a4012315a4 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Fri, 2 Jul 2021 17:18:04 +0800 Subject: Week 119 ch-1.pl --- challenge-119/cheok-yin-fung/perl/ch-1.pl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 challenge-119/cheok-yin-fung/perl/ch-1.pl diff --git a/challenge-119/cheok-yin-fung/perl/ch-1.pl b/challenge-119/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..75f9fd33eb --- /dev/null +++ b/challenge-119/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,16 @@ +print oct ("0x". scalar reverse sprintf("%02x", $ARGV[0])) ,"\n"; + +=pod +sub hex2dec { + return oct ("0x".$_[0]); +} + +sub dec2hex { + return sprintf("%02x", $_[0]); +} + +my $N = $ARGV[0]; + +print hex2dec(scalar reverse (dec2hex $N)); +print "\n"; +=cut -- cgit From 9373cec8d2777dd4a0f9352738b3649d34e70cb0 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sat, 3 Jul 2021 08:44:03 +0800 Subject: wk 119 ch-2.pl --- challenge-119/cheok-yin-fung/perl/ch-2.pl | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-119/cheok-yin-fung/perl/ch-2.pl diff --git a/challenge-119/cheok-yin-fung/perl/ch-2.pl b/challenge-119/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..6a0f1a1759 --- /dev/null +++ b/challenge-119/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +# The Weekly Challenge - 119 +# Task 2 Sequence without 1-on-1 +use strict; +use warnings; + +my $N = $ARGV[0]; + +my @arr = (0,1,2,3); + +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"; + } + $x = $y; +} + + +print $arr[$N], "\n"; -- cgit From cdfef3f9404041b4c7936a26532757e63fd4fcef Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Sun, 4 Jul 2021 22:17:19 +0800 Subject: wk 119 --- challenge-119/cheok-yin-fung/bash/ch-1.sh | 18 ++++++++++++++++ challenge-119/cheok-yin-fung/bash/ch-2.sh | 34 +++++++++++++++++++++++++++++++ challenge-119/cheok-yin-fung/perl/ch-2.pl | 34 +++++++++++++++++++------------ 3 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 challenge-119/cheok-yin-fung/bash/ch-1.sh create mode 100644 challenge-119/cheok-yin-fung/bash/ch-2.sh 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"); -- cgit