aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-07-04 15:26:10 +0100
committerGitHub <noreply@github.com>2021-07-04 15:26:10 +0100
commit32c9e16df072b7b54ea5014c5b0a7b55453e8e7b (patch)
treef2fe2351445f68451ec14f21072719bc9db488b0
parent5f7b76d5b841606c17f177a90397196ebf434c05 (diff)
parent28eabc69c70046cc047e98e2b2987d71b4aae9fa (diff)
downloadperlweeklychallenge-club-32c9e16df072b7b54ea5014c5b0a7b55453e8e7b.tar.gz
perlweeklychallenge-club-32c9e16df072b7b54ea5014c5b0a7b55453e8e7b.tar.bz2
perlweeklychallenge-club-32c9e16df072b7b54ea5014c5b0a7b55453e8e7b.zip
Merge pull request #4410 from E7-87-83/newt
Submission for Week 119
-rw-r--r--challenge-118/cheok-yin-fung/awk/ch-1.awk4
-rw-r--r--challenge-118/cheok-yin-fung/perl/ch-2.pl6
-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-1.pl16
-rw-r--r--challenge-119/cheok-yin-fung/perl/ch-2.pl33
6 files changed, 106 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],
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-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
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..802057f8e0
--- /dev/null
+++ b/challenge-119/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +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] || 0;
+print noo($N), "\n";
+
+sub noo {
+ my $p = $_[0];
+ my @arr = (0,1,2,3);
+
+ my $x = 0, my $y;
+
+ 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;
+ }
+ return $arr[$p];
+}
+
+ok (noo(5) == 13, "Example 1");
+ok (noo(10) == 32, "Example 2");
+ok (noo(60) == 2223, "Example 3");