aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2020-09-05 13:02:32 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2020-09-05 13:02:32 +0800
commitb3e1048f409a0e31e6f8b9ca15de21c12c42a33e (patch)
tree3e7c26c4f5ffa7d49fb258fa1dc4c4bd4e07d319
parent731e3ff85a5a649cd7ee5cbe889da2426fa059a0 (diff)
downloadperlweeklychallenge-club-b3e1048f409a0e31e6f8b9ca15de21c12c42a33e.tar.gz
perlweeklychallenge-club-b3e1048f409a0e31e6f8b9ca15de21c12c42a33e.tar.bz2
perlweeklychallenge-club-b3e1048f409a0e31e6f8b9ca15de21c12c42a33e.zip
improvement
-rw-r--r--challenge-076/cheok-yin-fung/perl/ch-1.pl15
-rw-r--r--challenge-076/cheok-yin-fung/perl/ch-2.pl105
2 files changed, 63 insertions, 57 deletions
diff --git a/challenge-076/cheok-yin-fung/perl/ch-1.pl b/challenge-076/cheok-yin-fung/perl/ch-1.pl
index 51275f8742..c790d2c0e5 100644
--- a/challenge-076/cheok-yin-fung/perl/ch-1.pl
+++ b/challenge-076/cheok-yin-fung/perl/ch-1.pl
@@ -6,7 +6,7 @@
# Usage: ch-1.pl $N
# Given the Goldbach's conjecture conjecture is verified
-# for n < 4 000 000 000 000 000 000 000 000
+# for n < 4 000 000 000 000 000 000
# ( source: mathworld.wolfram.com )
use strict;
@@ -37,7 +37,7 @@ if ($V == 2) {
}
if ($V == 4) {
- print "2\nas 2 + 2 = 4.\n";
+ print "2\nas 2 + 2 = 4 and 4 is not a prime.\n";
exit;
}
@@ -53,6 +53,7 @@ if ($V % 2 == 0) {
for (keys %oddprime) {
if ($oddprime{$V-$_}) {
print "as $_", " + ", $V-$_," = $V.\n" ;
+ last;
}
}
}
@@ -61,14 +62,18 @@ else {
if ($oddprime{$V}) {
print "1\nas $V is a prime.\n";
}
- elsif ($oddprime{$V-2} == 1) {
- print "2\nas 2 + " ,$V-2, " = $V.\n" ,
+ elsif ($oddprime{$V-2}) {
+ print "2\nas 2 + " ,$V-2, " = $V\nand $V is not a prime.\n" ,
}
else {
print "3\n";
for (keys %oddprime) {
if ($oddprime{$V-3-$_}) {
- print "as 3 + $_", " + ", $V-$_-3," = $V.\n";
+ print "3 + $_", " + ", $V-$_-3," = $V .\n";
+ print "The answer can't be 1 or 2 because",
+ " neither ", $V-2 ," nor $V is a prime; and\n",
+ " there are no even primes except 2\n",
+ " (two odd primes sum to an even number).\n";
last;
}
}
diff --git a/challenge-076/cheok-yin-fung/perl/ch-2.pl b/challenge-076/cheok-yin-fung/perl/ch-2.pl
index 28c1dc62d9..1642231842 100644
--- a/challenge-076/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-076/cheok-yin-fung/perl/ch-2.pl
@@ -1,8 +1,10 @@
-# Perl Weekly Challenge #076 Task 2
-# Word Search
-# task statement:
-# Write a script that takes two file names. The first file would contain word search grid as shown below. The second file contains list of words, one word per line. You could even use local dictionary file.
-#
+# Perl Weekly Challenge #076 Task 2 Word Search
+# task statements:
+# Write a script that takes two file names.
+# The first file would contain word search grid
+# as shown [below]. The second file contains list of words,
+# one word per line. You could even use
+# local dictionary file.
# Usage: ch-2.pl gridfile dictfile
use strict;
@@ -58,12 +60,7 @@ my $ylen = scalar @matrix;
# ==============
sub joinline {
- my @temp = @_;
- @temp = grep {defined($_)} @temp;
- # without the `grep {defined($_)}`, the program crashes
- # while the input grid is a vertically longer rectangle.
- # I haven't figured out why. Aug 3rd 2020
- return lc(join "", @temp);
+ return lc(join "", @_);
}
sub reversestring {
@@ -93,43 +90,45 @@ for my $j (0..$xlen-1) {
# ==============================
sub find_diagonal {
my @mat = @{$_[0]};
- my @ulefttolright;
- my @extra_dia;
- my $xlen_t = scalar @{$mat[0]};
- my $ylen_t = scalar @mat;
- $ulefttolright[0] = joinline map {${$mat[$_]}[$_]} (0..$ylen_t-1);
- $ulefttolright[1] = reversestring $ulefttolright[0];
-
- if ($xlen_t >= $ylen_t) {
- for my $c (0..$xlen_t-$ylen_t) {
- $ulefttolright[$c*2] = joinline
- map {${$mat[$_]}[$c+$_]} (0..$ylen_t-1) ;
- $ulefttolright[$c*2+1] = reversestring $ulefttolright[$c*2];
+ my @ulefttolright = ();
+ my @extra_dia = ();
+ my $t_xlen = scalar @{$mat[0]};
+ my $t_ylen = scalar @mat;
+ my $t_limit = ($t_xlen > $t_ylen) ? $t_ylen : $t_xlen;
+ my $t_diff = ($t_xlen > $t_ylen) ? $t_xlen - $t_ylen : $t_ylen - $t_xlen;
+
+ if ($t_xlen >= $t_ylen) {
+ for my $c (0..$t_diff) {
+ $ulefttolright[$c*2] = joinline
+ map {${$mat[$_]}[$c+$_]} (0..$t_ylen-1) ;
+ $ulefttolright[$c*2+1] = reversestring $ulefttolright[$c*2];
}
- for my $c (0..$ylen_t-2) {
- $extra_dia[$c*4] = joinline
- map {${$mat[$_]}[$xlen_t-$ylen_t+$c+$_]} (0..$ylen_t-$c-1) ;
- $extra_dia[$c*4+1] = reversestring $extra_dia[$c*4];
- $extra_dia[$c*4+2] = joinline
- map {${$mat[$c+$_]}[$_]} (0..$ylen_t-$c-1) ;
- $extra_dia[$c*4+3] = reversestring $extra_dia[$c*4+2];
+ for my $d (0..$t_limit-2) {
+ $extra_dia[$d*4] = joinline
+ map {${$mat[$_]}[$t_diff+$d+1+$_]} (0..$t_limit-$d-2) ;
+ $extra_dia[$d*4+1] = reversestring $extra_dia[$d*4];
+ $extra_dia[$d*4+2] = joinline
+ map {${$mat[$d+1+$_]}[$_]} (0..$t_limit-$d-2) ;
+ $extra_dia[$d*4+3] = reversestring $extra_dia[$d*4+2];
}
}
else {
- for my $c (0..$ylen_t-$xlen_t) {
+ for my $c (0..$t_diff) {
$ulefttolright[$c*2] = joinline
- map {${$mat[$_+$c]}[$_]} (0..$xlen_t-1) ;
+ map {${$mat[$_+$c]}[$_]} (0..$t_xlen-1) ;
$ulefttolright[$c*2+1] = reversestring $ulefttolright[$c*2];
}
- for my $c (0..$xlen_t-2) {
- $extra_dia[$c*4] = joinline
- map {${$mat[$ylen_t-$xlen_t+$c+$_]}[$_]} (0..$xlen_t-$c-1) ;
- $extra_dia[$c*4+1] = reversestring $extra_dia[$c*4];
- $extra_dia[$c*4+2] = joinline
- map {${$mat[$_]}[$c+$_]} (0..$xlen_t-$c-1) ;
- $extra_dia[$c*4+3] = reversestring $extra_dia[$c*4+2];
+ for my $d (0..$t_limit-2) {
+ $extra_dia[$d*4] = joinline
+ map {${$mat[$t_diff+$d+1+$_]}[$_]} (0..$t_limit-$d-2) ;
+ $extra_dia[$d*4+1] = reversestring $extra_dia[$d*4];
+ $extra_dia[$d*4+2] = joinline
+ map {${$mat[$_]}[$d+1+$_]} (0..$t_limit-$d-2) ;
+ $extra_dia[$d*4+3] = reversestring $extra_dia[$d*4+2];
}
+
+
}
return (@ulefttolright, @extra_dia);
}
@@ -142,12 +141,12 @@ sub find_diagonal {
sub vertical_reflection {
my @mat = @{$_[0]};
my @newmat;
- my $xlen_t = scalar @{$mat[0]};
- my $ylen_t = scalar @mat;
- for my $i (0..$xlen_t-1) {
+ my $t_xlen = scalar @{$mat[0]};
+ my $t_ylen = scalar @mat;
+ for my $i (0..$t_ylen-1) {
@{$newmat[$i]} = ();
- for my $j (0..$ylen_t-1) {
- ${$newmat[$i]}[$j] = ${$mat[$i]}[$ylen_t-1-$j];
+ for my $j (0..$t_xlen-1) {
+ ${$newmat[$i]}[$j] = ${$mat[$i]}[-1-$j];
}
}
return @newmat;
@@ -160,29 +159,31 @@ my @antidiagonal = find_diagonal( \@newmatrix);
# ===========================
-
# main dish:
my %unique;
for my $word (@dictwords) {
- for (@rowline,@colline,@diagonal,@antidiagonal) {
- if (not($unique{$word}) && defined($_) && $_ =~ /$word/ ) {
+ for (@rowline,@colline,@diagonal,@antidiagonal) {
+ if (defined $_) {
+ if ($_ =~ /$word/ && not($unique{$word}) ) {
print $word, " ";
$unique{$word} = 1;
}
+ }
}
}
-print "\n";
+print "\n";
# grid: as given in task statement
-# words list:
+# word list:
# https://www.oxfordlearnersdictionaries.com/wordlists/oxford3000-5000
# words with length more than or equal to 5 in the grid:
# align broad constitution depart enter midst social virus
#
# Running time
-# real 0m0.314s
-# user 0m0.302s
-# sys 0m0.012s
+# real 0m0.254s
+# user 0m0.249s
+# sys 0m0.004s
+