aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorE7-87-83 <fungcheokyin@gmail.com>2021-07-29 14:15:22 +0800
committerE7-87-83 <fungcheokyin@gmail.com>2021-07-29 14:15:22 +0800
commitf21d42435018467bc1e910ef2770baa57f6410ab (patch)
tree97d23cd238e9a3843dfdd2e74488e86cc6ccdf96
parent93f0a4db9b909601dbd7d29a4a24e46ae3a99b25 (diff)
downloadperlweeklychallenge-club-f21d42435018467bc1e910ef2770baa57f6410ab.tar.gz
perlweeklychallenge-club-f21d42435018467bc1e910ef2770baa57f6410ab.tar.bz2
perlweeklychallenge-club-f21d42435018467bc1e910ef2770baa57f6410ab.zip
n-th commit
-rw-r--r--challenge-123/cheok-yin-fung/perl/ch-2.pl10
-rw-r--r--challenge-123/cheok-yin-fung/perl/ch-2a.pl67
-rw-r--r--challenge-123/cheok-yin-fung/perl/ch-2ax.pl51
3 files changed, 58 insertions, 70 deletions
diff --git a/challenge-123/cheok-yin-fung/perl/ch-2.pl b/challenge-123/cheok-yin-fung/perl/ch-2.pl
index 42233dfb71..ed7142b2b9 100644
--- a/challenge-123/cheok-yin-fung/perl/ch-2.pl
+++ b/challenge-123/cheok-yin-fung/perl/ch-2.pl
@@ -34,7 +34,7 @@ sub is_square {
# "norm test"
#if ( $n_vector[0] == $n_vector[1] ) {
# the above conditional is fit for integter coordinates
-# the below is special arrangement satarting from the 7th test case
+# the below is special arrangement starting from the 7th test case
# or floating point number in general
if ( sprintf("%f",$n_vector[0]) == sprintf("%f", $n_vector[1])
&& 2*sprintf("%f", $n_vector[0]) == sprintf("%f", $n_vector[2])
@@ -47,7 +47,7 @@ sub is_square {
sub vec_prod {
my $first = $_[0];
my $second = $_[1];
- die "Not the same dimension in vec_prod \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_prod \n" if $first->$#* != $second->$#*;
my $sum = 0;
$sum+= ($first->[$_]*$second->[$_]) for (0..$first->$#*);
return $sum;
@@ -64,7 +64,7 @@ sub vec_sum {
my $first = $_[0];
my $second = $_[1];
my $ans = [];
- die "Not the same dimension in vec_sum \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_sum \n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
push $ans->@*, $first->[$s] + $second->[$s];
}
@@ -74,7 +74,7 @@ sub vec_sum {
sub vec_same {
my $first = $_[0];
my $second = $_[1];
- die "Not the same dimension in vec_same \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_same \n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
return 0 if $first->[$s] != $second->[$s];
}
@@ -85,7 +85,7 @@ sub vec_subtract {
my $first = $_[0];
my $second = $_[1];
my $ans = [];
- die "Not the same dimension in vec_subtract\n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_subtract\n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
push $ans->@*, $second->[$s] - $first->[$s];
}
diff --git a/challenge-123/cheok-yin-fung/perl/ch-2a.pl b/challenge-123/cheok-yin-fung/perl/ch-2a.pl
index 1c817a6c99..533bebf029 100644
--- a/challenge-123/cheok-yin-fung/perl/ch-2a.pl
+++ b/challenge-123/cheok-yin-fung/perl/ch-2a.pl
@@ -60,8 +60,6 @@ sub is_cube {
&& vec_same($UN, $v{$ind[5]}) ) { $bool = 1;
} elsif ( vec_same($WU, $v{$ind[5]})
&& vec_same($UN, $v{$ind[4]}) ) { $bool = 1;
- } else {
- $bool = undef;
}
}
if (!$bool && vec_same($NW, $v{$ind[4]})) {
@@ -69,9 +67,7 @@ sub is_cube {
&& vec_same($UN, $v{$ind[5]}) ) { $bool = 1;
} elsif ( vec_same($WU, $v{$ind[5]})
&& vec_same($UN, $v{$ind[3]}) ) { $bool = 1;
- } else {
- $bool = undef;
- }
+ }
}
if (!$bool && vec_same($NW, $v{$ind[5]})) {
if ( vec_same($WU, $v{$ind[4]})
@@ -86,13 +82,6 @@ sub is_cube {
my $NWU = vec_sum( $N, $WU);
if ( vec_same( $v{$ind[6]} , $NWU ) ) {
-=pod
- return 0 unless
- 2*norm($N) == norm($NW) &&
- norm($NW) == norm($WU) &&
- norm($WU) == norm($UN) &&
- 3*norm($N) == norm($NWU);
-=cut
return 1;
}
else {
@@ -154,20 +143,6 @@ sub is_hypercube {
my $AUNW = vec_sum($AU,$NW);
if ( vec_same($v{$ind[14]}, $AUNW) ) {
-=pod
- return 0 unless
- 2*norm($N) == norm($NW) &&
- norm($NW) == norm($AU) &&
- norm($NW) == norm($UN) &&
- norm($NW) == norm($WU) &&
- norm($NW) == norm($AW) &&
- norm($NW) == norm($AN) &&
- 3*norm($N) == norm($UNW) &&
- 3*norm($N) == norm($ANW) &&
- 3*norm($N) == norm($AWU) &&
- 3*norm($N) == norm($AUN) &&
- 4*norm($N) == norm($AUNW);
-=cut
return 1;
}
else {
@@ -178,7 +153,7 @@ sub is_hypercube {
sub vec_prod {
my $first = $_[0];
my $second = $_[1];
- die "Not the same dimension in vec_prod \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_prod \n" if $first->$#* != $second->$#*;
my $sum = 0;
$sum+= ($first->[$_]*$second->[$_]) for (0..$first->$#*);
return $sum;
@@ -199,7 +174,7 @@ sub vec_sum {
my $first = $_[0];
my $second = $_[1];
my $ans = [];
- die "Not the same dimension in vec_sum \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_sum \n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
push $ans->@*, $first->[$s] + $second->[$s];
}
@@ -209,7 +184,7 @@ sub vec_sum {
sub vec_same {
my $first = $_[0];
my $second = $_[1];
- die "Not the same dimension in vec_same \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_same \n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
return undef if $first->[$s] != $second->[$s];
}
@@ -220,7 +195,7 @@ sub vec_subtract {
my $first = $_[0];
my $second = $_[1];
my $ans = [];
- die "Not the same dimension in vec_subtract\n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_subtract\n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
push $ans->@*, $second->[$s] - $first->[$s];
}
@@ -239,34 +214,44 @@ ok is_square( [5/sqrt(26), 1/sqrt(26)],
"inclined by arctan(1/5), centre at origin";
ok is_square(
- [cos(atan2(1,5)), sin(atan2(1,5))], [-sin(atan2(1,5)), cos(atan2(1,5))],
- [-cos(atan2(1,5)), -sin(atan2(1,5))],[sin(atan2(1,5)), -cos(atan2(1,5))]
+ [cos(atan2(1,5)), sin(atan2(1,5))],
+ [-sin(atan2(1,5)), cos(atan2(1,5))],
+ [-cos(atan2(1,5)), -sin(atan2(1,5))],
+ [sin(atan2(1,5)), -cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2(), caught by the equalities";
ok is_square(
- [2.7*cos(atan2(1,5)), 2.7*sin(atan2(1,5))], [-2.7*sin(atan2(1,5)), 2.7*cos(atan2(1,5))],
- [-2.7*cos(atan2(1,5)), -2.7*sin(atan2(1,5))],[2.7*sin(atan2(1,5)), -2.7*cos(atan2(1,5))]
+ [2.7*cos(atan2(1,5)), 2.7*sin(atan2(1,5))],
+ [-2.7*sin(atan2(1,5)), 2.7*cos(atan2(1,5))],
+ [-2.7*cos(atan2(1,5)), -2.7*sin(atan2(1,5))],
+ [2.7*sin(atan2(1,5)), -2.7*cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2() of larger size (multipled by 2.7), caught by the equalities";
ok is_square(
- [2.8*cos(atan2(1,5)), 2.8*sin(atan2(1,5))], [-2.8*sin(atan2(1,5)), 2.8*cos(atan2(1,5))],
- [-2.8*cos(atan2(1,5)), -2.8*sin(atan2(1,5))],[2.8*sin(atan2(1,5)), -2.8*cos(atan2(1,5))]
+ [2.8*cos(atan2(1,5)), 2.8*sin(atan2(1,5))],
+ [-2.8*sin(atan2(1,5)), 2.8*cos(atan2(1,5))],
+ [-2.8*cos(atan2(1,5)), -2.8*sin(atan2(1,5))],
+ [2.8*sin(atan2(1,5)), -2.8*cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2() of larger size (multipled by 2.8), caught by the equalities";
ok is_square(
- [4.0*cos(atan2(1,5)), 4.0*sin(atan2(1,5))], [-4.0*sin(atan2(1,5)), 4.0*cos(atan2(1,5))],
- [-4.0*cos(atan2(1,5)), -4.0*sin(atan2(1,5))],[4.0*sin(atan2(1,5)), -4.0*cos(atan2(1,5))]
+ [4.0*cos(atan2(1,5)), 4.0*sin(atan2(1,5))],
+ [-4.0*sin(atan2(1,5)), 4.0*cos(atan2(1,5))],
+ [-4.0*cos(atan2(1,5)), -4.0*sin(atan2(1,5))],
+ [4.0*sin(atan2(1,5)), -4.0*cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2() of larger size (multipled by 4.0), caught by the equalities";
ok is_square(
- [0.0009*cos(atan2(1,5)), 0.0009*sin(atan2(1,5))], [-0.0009*sin(atan2(1,5)), 0.0009*cos(atan2(1,5))],
- [-0.0009*cos(atan2(1,5)), -0.0009*sin(atan2(1,5))],[0.0009*sin(atan2(1,5)), -0.0009*cos(atan2(1,5))]
+ [0.0009*cos(atan2(1,5)), 0.0009*sin(atan2(1,5))],
+ [-0.0009*sin(atan2(1,5)), 0.0009*cos(atan2(1,5))],
+ [-0.0009*cos(atan2(1,5)), -0.0009*sin(atan2(1,5))],
+ [0.0009*sin(atan2(1,5)), -0.0009*cos(atan2(1,5))]
)
- == 1, "arctan(1/5) by atan2() of a much smaller size (multiple by 0.0009), caught by the equalities";
+ == 1, "arctan(1/5) by atan2() of a much smaller size (multiple by 0.0009), caught by the equalities (_\"not ok\" is normal_)";
ok is_square( [1, 2] , [4,3], [3,1], [2,4] ) == 1, "Knight's square";
ok is_square( [1, 1] , [-1, 1], [1,-1], [-1,-1] ) == 1, "centre at origin";
diff --git a/challenge-123/cheok-yin-fung/perl/ch-2ax.pl b/challenge-123/cheok-yin-fung/perl/ch-2ax.pl
index 578868ee26..ac3ba42cce 100644
--- a/challenge-123/cheok-yin-fung/perl/ch-2ax.pl
+++ b/challenge-123/cheok-yin-fung/perl/ch-2ax.pl
@@ -10,13 +10,13 @@ use warnings;
use v5.10.0;
use Test::More tests => 14;
-use Algorithm::Combinatorics qw(permutations); #use for hypercube
+use Algorithm::Combinatorics qw(permutations); #use for cube and hypercube
my $k = $ARGV[0] || 3;
my $D = $ARGV[1] || $k;
-die "Usage: ch-2a.pl [2, 3 or 4] (optional)[dimension of space] "
+die "Usage: ch-2ax.pl [2, 3 or 4] (optional)[dimension of space] "
if $k > 4 or $k <= 1;
die "How can I put a $k-polytope into $D-dim space? \n" if $k > $D;
@@ -29,9 +29,6 @@ sub is_square {
return 0 unless (vec_prod($v1, $v2) == 0) xor
(vec_prod($v0, $v2) == 0) xor
(vec_prod($v0, $v1) == 0);
-# return 0 unless vec_same($v0, vec_sum($v1, $v2) ) xor
-# vec_same($v1, vec_sum($v2, $v0) ) xor
-# vec_same($v2, vec_sum($v0, $v1) );
my @n_vector = map { norm_f($_) } ($v0, $v1, $v2);
@n_vector = sort {$a<=>$b} @n_vector;
if ( $n_vector[0] == $n_vector[1] && 2*$n_vector[0] == $n_vector[2] ) {
@@ -60,8 +57,6 @@ sub is_cube {
&& vec_same($UN, $v{$ind[5]}) ) { $bool = 1;
} elsif ( vec_same($WU, $v{$ind[5]})
&& vec_same($UN, $v{$ind[4]}) ) { $bool = 1;
- } else {
- $bool = undef;
}
}
if (!$bool && vec_same($NW, $v{$ind[4]})) {
@@ -69,8 +64,6 @@ sub is_cube {
&& vec_same($UN, $v{$ind[5]}) ) { $bool = 1;
} elsif ( vec_same($WU, $v{$ind[5]})
&& vec_same($UN, $v{$ind[3]}) ) { $bool = 1;
- } else {
- $bool = undef;
}
}
if (!$bool && vec_same($NW, $v{$ind[5]})) {
@@ -178,7 +171,7 @@ sub is_hypercube {
sub vec_prod {
my $first = $_[0];
my $second = $_[1];
- die "Not the same dimension in vec_prod \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_prod \n" if $first->$#* != $second->$#*;
my $sum = 0;
$sum+= ($first->[$_]*$second->[$_]) for (0..$first->$#*);
return $sum;
@@ -199,7 +192,7 @@ sub vec_sum {
my $first = $_[0];
my $second = $_[1];
my $ans = [];
- die "Not the same dimension in vec_sum \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_sum \n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
push $ans->@*, $first->[$s] + $second->[$s];
}
@@ -209,7 +202,7 @@ sub vec_sum {
sub vec_same {
my $first = $_[0];
my $second = $_[1];
- die "Not the same dimension in vec_same \n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_same \n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
return undef if $first->[$s] != $second->[$s];
}
@@ -220,7 +213,7 @@ sub vec_subtract {
my $first = $_[0];
my $second = $_[1];
my $ans = [];
- die "Not the same dimension in vec_subtract\n" if $first->$#* != $second->$#*;
+ warn "Not the same dimension in vec_subtract\n" if $first->$#* != $second->$#*;
for my $s (0..$first->$#*) {
push $ans->@*, $second->[$s] - $first->[$s];
}
@@ -239,34 +232,44 @@ ok is_square( [5/sqrt(26), 1/sqrt(26)],
"inclined by arctan(1/5), centre at origin";
ok is_square(
- [cos(atan2(1,5)), sin(atan2(1,5))], [-sin(atan2(1,5)), cos(atan2(1,5))],
- [-cos(atan2(1,5)), -sin(atan2(1,5))],[sin(atan2(1,5)), -cos(atan2(1,5))]
+ [cos(atan2(1,5)), sin(atan2(1,5))],
+ [-sin(atan2(1,5)), cos(atan2(1,5))],
+ [-cos(atan2(1,5)), -sin(atan2(1,5))],
+ [sin(atan2(1,5)), -cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2(), caught by the equalities";
ok is_square(
- [2.7*cos(atan2(1,5)), 2.7*sin(atan2(1,5))], [-2.7*sin(atan2(1,5)), 2.7*cos(atan2(1,5))],
- [-2.7*cos(atan2(1,5)), -2.7*sin(atan2(1,5))],[2.7*sin(atan2(1,5)), -2.7*cos(atan2(1,5))]
+ [2.7*cos(atan2(1,5)), 2.7*sin(atan2(1,5))],
+ [-2.7*sin(atan2(1,5)), 2.7*cos(atan2(1,5))],
+ [-2.7*cos(atan2(1,5)), -2.7*sin(atan2(1,5))],
+ [2.7*sin(atan2(1,5)), -2.7*cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2() of larger size (multipled by 2.7), caught by the equalities";
ok is_square(
- [2.8*cos(atan2(1,5)), 2.8*sin(atan2(1,5))], [-2.8*sin(atan2(1,5)), 2.8*cos(atan2(1,5))],
- [-2.8*cos(atan2(1,5)), -2.8*sin(atan2(1,5))],[2.8*sin(atan2(1,5)), -2.8*cos(atan2(1,5))]
+ [2.8*cos(atan2(1,5)), 2.8*sin(atan2(1,5))],
+ [-2.8*sin(atan2(1,5)), 2.8*cos(atan2(1,5))],
+ [-2.8*cos(atan2(1,5)), -2.8*sin(atan2(1,5))],
+ [2.8*sin(atan2(1,5)), -2.8*cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2() of larger size (multipled by 2.8), caught by the equalities";
ok is_square(
- [4.0*cos(atan2(1,5)), 4.0*sin(atan2(1,5))], [-4.0*sin(atan2(1,5)), 4.0*cos(atan2(1,5))],
- [-4.0*cos(atan2(1,5)), -4.0*sin(atan2(1,5))],[4.0*sin(atan2(1,5)), -4.0*cos(atan2(1,5))]
+ [4.0*cos(atan2(1,5)), 4.0*sin(atan2(1,5))],
+ [-4.0*sin(atan2(1,5)), 4.0*cos(atan2(1,5))],
+ [-4.0*cos(atan2(1,5)), -4.0*sin(atan2(1,5))],
+ [4.0*sin(atan2(1,5)), -4.0*cos(atan2(1,5))]
)
== 1, "arctan(1/5) by atan2() of larger size (multipled by 4.0), caught by the equalities";
ok is_square(
- [0.0009*cos(atan2(1,5)), 0.0009*sin(atan2(1,5))], [-0.0009*sin(atan2(1,5)), 0.0009*cos(atan2(1,5))],
- [-0.0009*cos(atan2(1,5)), -0.0009*sin(atan2(1,5))],[0.0009*sin(atan2(1,5)), -0.0009*cos(atan2(1,5))]
+ [0.0009*cos(atan2(1,5)), 0.0009*sin(atan2(1,5))],
+ [-0.0009*sin(atan2(1,5)), 0.0009*cos(atan2(1,5))],
+ [-0.0009*cos(atan2(1,5)), -0.0009*sin(atan2(1,5))],
+ [0.0009*sin(atan2(1,5)), -0.0009*cos(atan2(1,5))]
)
- == 1, "arctan(1/5) by atan2() of a much smaller size (multiple by 0.0009), caught by the equalities";
+ == 1, "arctan(1/5) by atan2() of a much smaller size (multiple by 0.0009), caught by the equalities (_\"not ok\" is normal_)";
ok is_square( [1, 2] , [4,3], [3,1], [2,4] ) == 1, "Knight's square";
ok is_square( [1, 1] , [-1, 1], [1,-1], [-1,-1] ) == 1, "centre at origin";