aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-06-27 17:28:09 +0100
committerGitHub <noreply@github.com>2021-06-27 17:28:09 +0100
commit0b677ef0c66c68a1cd1c5d16fb6c2892fd96abcb (patch)
treea0d94bda4a7926ea5d3769a66b8d2321e17343a5
parent553115b6b775de8fa1f94b359857e41922d86925 (diff)
parentddb660dac6853e936a9e82213be219fefddee349 (diff)
downloadperlweeklychallenge-club-0b677ef0c66c68a1cd1c5d16fb6c2892fd96abcb.tar.gz
perlweeklychallenge-club-0b677ef0c66c68a1cd1c5d16fb6c2892fd96abcb.tar.bz2
perlweeklychallenge-club-0b677ef0c66c68a1cd1c5d16fb6c2892fd96abcb.zip
Merge pull request #4348 from E7-87-83/newt
1 awk script, 2 blogposts, ch-1.pl, ch-2.pl
-rw-r--r--challenge-118/cheok-yin-fung/awk/ch-1.awk20
-rw-r--r--challenge-118/cheok-yin-fung/blog.txt2
-rw-r--r--challenge-118/cheok-yin-fung/perl/ch-2.pl11
3 files changed, 27 insertions, 6 deletions
diff --git a/challenge-118/cheok-yin-fung/awk/ch-1.awk b/challenge-118/cheok-yin-fung/awk/ch-1.awk
new file mode 100644
index 0000000000..fe7e67c04b
--- /dev/null
+++ b/challenge-118/cheok-yin-fung/awk/ch-1.awk
@@ -0,0 +1,20 @@
+# The Weekly Challenge 118
+# Task 1 Binary Palindrome
+# Usage: echo "N" | awk -f 'ch-1.awk'
+# or: awk -f 'ch-1.awk' < file_contains_an_integer_on_each_line
+
+{
+ n = $0;
+ i = 0;
+ while (n > 0) {
+ arr[i] = n % 2
+ n = int(n / 2)
+ i++
+ }
+ len = i-1
+ i = 0
+ while (arr[len-i] == arr[i] && i < len/2) {
+ i++
+ }
+ print arr[len-i]==arr[i] ? 1 : 0
+}
diff --git a/challenge-118/cheok-yin-fung/blog.txt b/challenge-118/cheok-yin-fung/blog.txt
new file mode 100644
index 0000000000..46fca31c76
--- /dev/null
+++ b/challenge-118/cheok-yin-fung/blog.txt
@@ -0,0 +1,2 @@
+https://e7-87-83.github.io/coding/challenge_118.html
+https://e7-87-83.github.io/coding/challenge_118t1.html
diff --git a/challenge-118/cheok-yin-fung/perl/ch-2.pl b/challenge-118/cheok-yin-fung/perl/ch-2.pl
index 9a3e282d32..d74f82c92e 100644
--- a/challenge-118/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-118/cheok-yin-fung/perl/ch-2.pl
@@ -6,23 +6,19 @@
# 3 2^ 1 2
# 2 1 4 3
# 3^ 2 3 2
-
a b c d
--------
N * * * |4
* * * * |3
* x * * |2
* x x * |1
-
x : b1, b2, c1
-
b1 <-> b2 : 3
b1 <-> c1 : 3
b2 <-> c1 : 2
a4(N) <-> b1 : 2
a4 <-> b2 : 1
a4 <-> c1 : 3
-
N -> b1 -> b2 -> c1 : 2 + 3 + 2 = 7
N -> b1 -> c1 -> b2 : 2 + 3 + 2 = 7
N -> b2 -> b1 -> c1 : 1 + 3 + 3 = 7
@@ -38,6 +34,9 @@ N -> c1 -> b2 -> b1 : 3 + 2 + 3 = 8
use strict;
use warnings;
use Algorithm::Combinatorics qw/permutations/;
+#use Memoize; # faster! learn from Mr Roger Bell_West's code
+
+#memoize("expand"); # faster! learn from Mr Roger Bell_West's code
die "Give me positions with treasure!\n" unless $ARGV[0];
my @treasures = map { binumeric_position($_) } @ARGV;
@@ -62,11 +61,11 @@ my $iter = permutations( \@treasures );
while (my $p = $iter->next) {
my $path_length = dist_fun([0,0], $p->[0]);
my $i = 0;
- while ($i < $p->$#*) {
+ while ($i < $p->$#* && $path_length < $min_path_length) {
$path_length += dist_fun($p->[$i], $p->[$i+1]);
$i++;
}
- compare_mini($path_length, $p);
+ compare_mini($path_length, $p) if $i == $p->$#*;
}