From a42addb6a561d57e470cfed7828109bbfba040fb Mon Sep 17 00:00:00 2001 From: Abigail Date: Wed, 28 Jul 2021 18:13:24 +0200 Subject: Use methods to factor out calculations. --- challenge-123/abigail/awk/ch-2.awk | 14 ++++++-------- challenge-123/abigail/bash/ch-2.sh | 14 ++++++++------ challenge-123/abigail/bc/ch-2.bc | 20 ++++++++++---------- challenge-123/abigail/c/ch-2.c | 16 ++++++++-------- challenge-123/abigail/go/ch-2.go | 17 +++++++++-------- challenge-123/abigail/java/ch-2.java | 17 ++++++++--------- challenge-123/abigail/lua/ch-2.lua | 14 +++++++------- challenge-123/abigail/node/ch-2.js | 15 ++++++++------- challenge-123/abigail/pascal/ch-2.p | 17 +++++++++-------- challenge-123/abigail/perl/ch-2.pl | 17 ++++++++++------- challenge-123/abigail/python/ch-2.py | 12 ++++++------ challenge-123/abigail/ruby/ch-2.rb | 16 +++++++++------- challenge-123/abigail/tcl/ch-2.tcl | 15 ++++++++------- 13 files changed, 106 insertions(+), 98 deletions(-) diff --git a/challenge-123/abigail/awk/ch-2.awk b/challenge-123/abigail/awk/ch-2.awk index 27709d6fd0..96ac5314b1 100644 --- a/challenge-123/abigail/awk/ch-2.awk +++ b/challenge-123/abigail/awk/ch-2.awk @@ -8,13 +8,11 @@ # Run as: awk -f ch-2.awk < input-file # -{ - e1 = ($1 - $3) ^ 2 + ($2 - $4) ^ 2 - e2 = ($3 - $5) ^ 2 + ($4 - $6) ^ 2 - e3 = ($5 - $7) ^ 2 + ($6 - $8) ^ 2 - e4 = ($7 - $1) ^ 2 + ($8 - $2) ^ 2 - d1 = ($1 - $5) ^ 2 + ($2 - $6) ^ 2 - d2 = ($3 - $7) ^ 2 + ($4 - $8) ^ 2 +function dist (x1, y1, x2, y2) {(x1 - x2) ^ 2 + (y1 - y2) ^ 2} - print ((e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2) ? 1 : 0) +{ + print (dist ($1, $2, $3, $4) == dist ($3, $4, $5, $6) && + dist ($3, $4, $5, $6) == dist ($5, $6, $7, $8) && + dist ($5, $6, $7, $8) == dist ($7, $8, $1, $2) && + dist ($1, $2, $5, $6) == dist ($3, $4, $7, $8) ? 1 : 0) } diff --git a/challenge-123/abigail/bash/ch-2.sh b/challenge-123/abigail/bash/ch-2.sh index fb12779c2e..68844b2f46 100644 --- a/challenge-123/abigail/bash/ch-2.sh +++ b/challenge-123/abigail/bash/ch-2.sh @@ -10,13 +10,15 @@ set -f +function dist () {((dist = ($1 - $3) ** 2 + ($2 - $4) ** 2))} + while read x1 y1 x2 y2 x3 y3 x4 y4 -do ((e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2)) - ((e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2)) - ((e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2)) - ((e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2)) - ((d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2)) - ((d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2)) +do dist $x1 $y1 $x2 $y2; ((e1 = dist)) + dist $x2 $y2 $x3 $y3; ((e2 = dist)) + dist $x3 $y3 $x4 $y4; ((e3 = dist)) + dist $x4 $y4 $x1 $y1; ((e4 = dist)) + dist $x1 $y1 $x3 $y3; ((d1 = dist)) + dist $x2 $y2 $x4 $y4; ((d2 = dist)) if ((e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2)) then echo 1 else echo 0 diff --git a/challenge-123/abigail/bc/ch-2.bc b/challenge-123/abigail/bc/ch-2.bc index 2ea9169a08..70815c52d7 100644 --- a/challenge-123/abigail/bc/ch-2.bc +++ b/challenge-123/abigail/bc/ch-2.bc @@ -8,6 +8,8 @@ # Input should be terminated with a line starting with a 0 # +define dist (a, b, c, d) {return ((a - c) ^ 2 + (b - d) ^ 2)} + while (1) { a = read () if (a == 0) {break} @@ -18,15 +20,13 @@ while (1) { f = read () g = read () h = read () - i = (a - c) ^ 2 + (b - d) ^ 2 - j = (c - e) ^ 2 + (d - f) ^ 2 - k = (e - g) ^ 2 + (f - h) ^ 2 - l = (g - a) ^ 2 + (h - b) ^ 2 - m = (a - e) ^ 2 + (b - f) ^ 2 - n = (c - g) ^ 2 + (d - h) ^ 2 - o = 0 - if (i == j && j == k && k == l && m == n) { - o = 1 + + if (dist (a, b, c, d) == dist (c, d, e, f) && \ + dist (c, d, e, f) == dist (e, f, g, h) && \ + dist (e, f, g, h) == dist (g, h, a, b) && \ + dist (a, b, e, f) == dist (c, d, g, h)) { + 1 + } else { + 0 } - o } diff --git a/challenge-123/abigail/c/ch-2.c b/challenge-123/abigail/c/ch-2.c index 56f0b615d4..60a24fb06b 100644 --- a/challenge-123/abigail/c/ch-2.c +++ b/challenge-123/abigail/c/ch-2.c @@ -10,19 +10,19 @@ * Run as: cc -o ch-2.o ch-2.c; ./ch-2.o < input-file */ +int dist (int x1, int y1, int x2, int y2) { + return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); +} + int main (void) { int x1, y1, x2, y2, x3, y3, x4, y4; while (scanf ("%d %d %d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4) == 8) { - int e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); - int e2 = (x2 - x3) * (x2 - x4) + (y2 - y3) * (y2 - y3); - int e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4); - int e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); - int d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); - int d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4); - - printf ("%d\n", e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0); + printf ("%d\n", dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) && + dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) && + dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) && + dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) ? 1 : 0); } return (0); diff --git a/challenge-123/abigail/go/ch-2.go b/challenge-123/abigail/go/ch-2.go index 37d275d5dc..6a287f6d73 100644 --- a/challenge-123/abigail/go/ch-2.go +++ b/challenge-123/abigail/go/ch-2.go @@ -12,22 +12,23 @@ import ( "fmt" ) +func dist (x1 int, y1 int, x2 int, y2 int) int { + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); +} + func main () { - var x1, y1, x2, y2, x3, y3, x4, y4, e1, e2, e3, e4, d1, d2 int; + var x1, y1, x2, y2, x3, y3, x4, y4 int; for { var n, err = fmt . Scanf ("%d %d %d %d %d %d %d %d", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4); if (err != nil || n != 8) { break; } - e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); - e2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3); - e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4); - e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); - d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); - d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4); - if (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2) { + if (dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) && + dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) && + dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) && + dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4)) { fmt . Printf ("%d\n", 1); } else { // Seriously, go, else needs to be cuddled? fmt . Printf ("%d\n", 0); diff --git a/challenge-123/abigail/java/ch-2.java b/challenge-123/abigail/java/ch-2.java index 8d13bcf8bf..24f7dbe9a9 100644 --- a/challenge-123/abigail/java/ch-2.java +++ b/challenge-123/abigail/java/ch-2.java @@ -9,6 +9,9 @@ import java.util.*; public class ch2 { + static int dist (int x1, int y1, int x2, int y2) { + return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); + } public static void main (String [] args) { Scanner scanner = new Scanner (System . in); while (scanner . hasNext ()) { @@ -21,15 +24,11 @@ public class ch2 { int x4 = scanner . nextInt (); int y4 = scanner . nextInt (); - int e1 = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); - int e2 = (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3); - int e3 = (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4); - int e4 = (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); - int d1 = (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); - int d2 = (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4); - - System . out . println (e1 == e2 && e2 == e3 && - e3 == e4 && d1 == d2 ? 1 : 0); + System . out . println ( + dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) && + dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) && + dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) && + dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) ? 1 : 0); } } } diff --git a/challenge-123/abigail/lua/ch-2.lua b/challenge-123/abigail/lua/ch-2.lua index b379f3078a..ddf8d86a14 100644 --- a/challenge-123/abigail/lua/ch-2.lua +++ b/challenge-123/abigail/lua/ch-2.lua @@ -13,16 +13,16 @@ for _ = 2, 8 do pat = pat .. "%s+(-?%d+)"; end +function dist (x1, y1, x2, y2) + return (x1 - x2) ^ 2 + (y1 - y2) ^ 2 +end for line in io . lines () do _, _, x1, y1, x2, y2, x3, y3, x4, y4 = line : find (pat) - local e1 = (x1 - x2) ^ 2 + (y1 - y2) ^ 2 - local e2 = (x2 - x3) ^ 2 + (y2 - y3) ^ 2 - local e3 = (x3 - x4) ^ 2 + (y3 - y4) ^ 2 - local e4 = (x4 - x1) ^ 2 + (y4 - y1) ^ 2 - local d1 = (x1 - x3) ^ 2 + (y1 - y3) ^ 2 - local d2 = (x2 - x4) ^ 2 + (y2 - y4) ^ 2 - if e1 == e2 and e2 == e3 and e3 == e4 and d1 == d2 then + if dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) and + dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) and + dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) and + dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) then print (1) else print (0) diff --git a/challenge-123/abigail/node/ch-2.js b/challenge-123/abigail/node/ch-2.js index 85ec5ce186..8e5fdb499e 100644 --- a/challenge-123/abigail/node/ch-2.js +++ b/challenge-123/abigail/node/ch-2.js @@ -8,15 +8,16 @@ // Run as: node ch-2.js < input-file // +function dist (x1, y1, x2, y2) { + return (x1 - x2) ** 2 + (y1 - y2) ** 2 +} + require ('readline') . createInterface ({input: process . stdin}) . on ('line', line => { let [x1, y1, x2, y2, x3, y3, x4, y4] = line . split (/ +/) . map (_ => +_) - let e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2 - let e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2 - let e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2 - let e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2 - let d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2 - let d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2 - console . log (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0) + console . log (dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) && + dist (x2, y2, x3, y3) == dist (x3, y3, x4, y4) && + dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) && + dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4) ? 1 : 0) }) diff --git a/challenge-123/abigail/pascal/ch-2.p b/challenge-123/abigail/pascal/ch-2.p index d71fa1c2f0..3bd63c819a 100644 --- a/challenge-123/abigail/pascal/ch-2.p +++ b/challenge-123/abigail/pascal/ch-2.p @@ -8,20 +8,21 @@ Program IsSquare; (* Run as: fpc -och-2.out ch-2.p; ./ch-2.out < input-file *) (* *) +function dist (x1, y1, x2, y2: integer): integer; +begin + dist := (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) +end; + var x1, y1, x2, y2, x3, y3, x4, y4: integer; - e1, e2, e3, e4, d1, d2: integer; begin while not eof () do begin readln (x1, y1, x2, y2, x3, y3, x4, y4); - e1 := (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2); - e2 := (x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3); - e3 := (x3 - x4) * (x3 - x4) + (y3 - y4) * (y3 - y4); - e4 := (x4 - x1) * (x4 - x1) + (y4 - y1) * (y4 - y1); - d1 := (x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3); - d2 := (x2 - x4) * (x2 - x4) + (y2 - y4) * (y2 - y4); - if (e1 = e2) and (e2 = e3) and (e3 = e4) and (d1 = d2) then begin + if (dist (x1, y1, x2, y2) = dist (x2, y2, x3, y3)) and + (dist (x2, y2, x3, y3) = dist (x3, y3, x4, y4)) and + (dist (x3, y3, x4, y4) = dist (x4, y4, x1, y1)) and + (dist (x1, y1, x3, y3) = dist (x2, y2, x4, y4)) then begin writeln (1); end else begin diff --git a/challenge-123/abigail/perl/ch-2.pl b/challenge-123/abigail/perl/ch-2.pl index 7803ed57b0..6662c53e32 100644 --- a/challenge-123/abigail/perl/ch-2.pl +++ b/challenge-123/abigail/perl/ch-2.pl @@ -22,12 +22,15 @@ use experimental 'lexical_subs'; # So we check whether all edges are equal, and whether both diagonals are equal. # +# +# Given the coordinates of two points, return the square of +# the distance between them. +# +sub dist ($x1, $y1, $x2, $y2) {($x1 - $x2) ** 2 + ($y1 - $y2) ** 2} + while (<>) { - my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = split; - say + ($x1 - $x2) ** 2 + ($y1 - $y2) ** 2 == - ($x2 - $x3) ** 2 + ($y2 - $y3) ** 2 == - ($x3 - $x4) ** 2 + ($y3 - $y4) ** 2 == - ($x4 - $x1) ** 2 + ($y4 - $y1) ** 2 && - ($x1 - $x3) ** 2 + ($y1 - $y3) ** 2 == - ($x2 - $x4) ** 2 + ($y2 - $y4) ** 2 ? 1 : 0 + my ($x1, $y1, $x2, $y2, $x3, $y3, $x4, $y4) = split; + say dist ($x1, $y1, $x2, $y2) == dist ($x2, $y2, $x3, $y3) == + dist ($x3, $y3, $x4, $y4) == dist ($x4, $y4, $x1, $x2) && + dist ($x1, $y1, $x3, $y3) == dist ($x2, $y2, $x4, $y4) ? 1 : 0 } diff --git a/challenge-123/abigail/python/ch-2.py b/challenge-123/abigail/python/ch-2.py index e61a2cc0c5..437dc072ff 100644 --- a/challenge-123/abigail/python/ch-2.py +++ b/challenge-123/abigail/python/ch-2.py @@ -10,15 +10,15 @@ import fileinput +def dist (x1, y1, x2, y2): + return (x1 - x2) ** 2 + (y1 - y2) ** 2 + for line in fileinput . input (): [x1, y1, x2, y2, x3, y3, x4, y4] = \ map (lambda x: int (x), line . split (' ')) - if (x1 - x2) ** 2 + (y1 - y2) ** 2 == \ - (x2 - x3) ** 2 + (y2 - y3) ** 2 == \ - (x3 - x4) ** 2 + (y3 - y4) ** 2 == \ - (x4 - x1) ** 2 + (y4 - y1) ** 2 and \ - (x1 - x3) ** 2 + (y1 - y3) ** 2 == \ - (x2 - x4) ** 2 + (y2 - y4) ** 2: + if dist (x1, y1, x2, y2) == dist (x2, y2, x3, y3) == \ + dist (x3, y3, x4, y4) == dist (x4, y4, x1, y1) and \ + dist (x1, y1, x3, y3) == dist (x2, y2, x4, y4): print (1) else: print (0) diff --git a/challenge-123/abigail/ruby/ch-2.rb b/challenge-123/abigail/ruby/ch-2.rb index 7e7f54b680..56fb532063 100644 --- a/challenge-123/abigail/ruby/ch-2.rb +++ b/challenge-123/abigail/ruby/ch-2.rb @@ -8,14 +8,16 @@ # Run as: ruby ch-2.rb < input-file # +def dist (x1, y1, x2, y2) + return (x1 - x2) ** 2 + (y1 - y2) ** 2 +end + ARGF . each_line do |line| x1, y1, x2, y2, x3, y3, x4, y4 = line . split(" ") . map {|n| n . to_i} - e1 = (x1 - x2) ** 2 + (y1 - y2) ** 2 - e2 = (x2 - x3) ** 2 + (y2 - y3) ** 2 - e3 = (x3 - x4) ** 2 + (y3 - y4) ** 2 - e4 = (x4 - x1) ** 2 + (y4 - y1) ** 2 - d1 = (x1 - x3) ** 2 + (y1 - y3) ** 2 - d2 = (x2 - x4) ** 2 + (y2 - y4) ** 2 - puts (e1 == e2 && e2 == e3 && e3 == e4 && d1 == d2 ? 1 : 0) + + puts (dist(x1, y1, x2, y2) == dist(x2, y2, x3, y3) && + dist(x2, y2, x3, y3) == dist(x3, y3, x4, y4) && + dist(x3, y3, x4, y4) == dist(x4, y4, x1, y1) && + dist(x1, y1, x3, y3) == dist(x2, y2, x4, y4) ? 1 : 0) end diff --git a/challenge-123/abigail/tcl/ch-2.tcl b/challenge-123/abigail/tcl/ch-2.tcl index ee5ed1563e..2f0ad1e8a3 100644 --- a/challenge-123/abigail/tcl/ch-2.tcl +++ b/challenge-123/abigail/tcl/ch-2.tcl @@ -6,15 +6,16 @@ # Run as: tclsh ch-2.tcl < input-file # +proc dist {x1 y1 x2 y2} { + return [expr ($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2)] +} + while {[gets stdin line] >= 0} { lassign [split $line " "] x1 y1 x2 y2 x3 y3 x4 y4 - set e1 [expr ($x1 - $x2) * ($x1 - $x2) + ($y1 - $y2) * ($y1 - $y2)] - set e2 [expr ($x2 - $x3) * ($x2 - $x3) + ($y2 - $y3) * ($y2 - $y3)] - set e3 [expr ($x3 - $x4) * ($x3 - $x4) + ($y3 - $y4) * ($y3 - $y4)] - set e4 [expr ($x4 - $x1) * ($x4 - $x1) + ($y4 - $y1) * ($y4 - $y1)] - set d1 [expr ($x1 - $x3) * ($x1 - $x3) + ($y1 - $y3) * ($y1 - $y3)] - set d2 [expr ($x2 - $x4) * ($x2 - $x4) + ($y2 - $y4) * ($y2 - $y4)] - if {$e1 == $e2 && $e2 == $e3 && $e3 == $e4 && $d1 == $d2} { + if {[dist $x1 $y1 $x2 $y2] == [dist $x2 $y2 $x3 $y3] && + [dist $x2 $y2 $x3 $y3] == [dist $x3 $y3 $x4 $y4] && + [dist $x3 $y3 $x4 $y4] == [dist $x4 $y4 $x1 $y1] && + [dist $x1 $y1 $x3 $y3] == [dist $x2 $y2 $x4 $y4]} { puts 1 } else { puts 0 -- cgit From f21d42435018467bc1e910ef2770baa57f6410ab Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Thu, 29 Jul 2021 14:15:22 +0800 Subject: n-th commit --- challenge-123/cheok-yin-fung/perl/ch-2.pl | 10 ++--- challenge-123/cheok-yin-fung/perl/ch-2a.pl | 67 +++++++++++------------------ challenge-123/cheok-yin-fung/perl/ch-2ax.pl | 51 +++++++++++----------- 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"; -- cgit From dfecb68047b32c0a49bae4acce5a6c795d6ec17e Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Fri, 30 Jul 2021 12:43:39 +0800 Subject: update week 123 --- challenge-123/cheok-yin-fung/perl/ch-2.pl | 86 +++++++++++++++++++++-------- challenge-123/cheok-yin-fung/perl/ch-2a.pl | 59 +++++++++++++------- challenge-123/cheok-yin-fung/perl/ch-2ax.pl | 86 +++++++++++++++++------------ 3 files changed, 154 insertions(+), 77 deletions(-) diff --git a/challenge-123/cheok-yin-fung/perl/ch-2.pl b/challenge-123/cheok-yin-fung/perl/ch-2.pl index ed7142b2b9..7421f86fc7 100644 --- a/challenge-123/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-123/cheok-yin-fung/perl/ch-2.pl @@ -8,7 +8,7 @@ use strict; use warnings; use v5.10.0; -use Test::More tests => 11; +use Test::More tests => 13; my $D = $ARGV[0] || 2; @@ -25,19 +25,24 @@ sub is_square { my $v0 = vec_subtract($p0, $p1); my $v1 = vec_subtract($p0, $p2); my $v2 = vec_subtract($p0, $p3); - # "cross product test"; - return 0 unless (vec_prod($v1, $v2) == 0) xor - (vec_prod($v0, $v2) == 0) xor - (vec_prod($v0, $v1) == 0); + # "dot product test"; + # return 0 unless (vec_prod_f($v1, $v2) == 0) xor + # (vec_prod_f($v0, $v2) == 0) xor + # (vec_prod_f($v0, $v1) == 0); my @n_vector = map { norm($_) } ($v0, $v1, $v2); @n_vector = sort {$a<=>$b} @n_vector; # "norm test" #if ( $n_vector[0] == $n_vector[1] ) { # the above conditional is fit for integter coordinates -# the below is special arrangement starting from the 7th test case +# the below is special arrangement starting from the 5th 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]) + && 2*sprintf("%f", $n_vector[0]) == sprintf("%f", $n_vector[2]) +# the above line concerning length of diagonal is sqrt(2) +# times the edge length would not be a necessary check +# if we preserve the dot product test, because in +# Euclidean space, if two vectors are orthogonal and in equal length, +# we can apply the Pythagorean theorem. ) { return 1; } @@ -53,6 +58,10 @@ sub vec_prod { return $sum; } +sub vec_prod_f { + return sprintf("%f", vec_prod($_[0], $_[1])); +} + sub norm { my $p = $_[0]; my $sum = 0; @@ -108,33 +117,66 @@ 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"; + == 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"; + == 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"; - + == 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" + ." (expected fail)"; +; + +ok is_square( + [-2, 2*sqrt(3), -2*sqrt(3)-1 ], + [-2, 4+2*sqrt(3), -1+2*sqrt(3) ], + [ 6, 2*sqrt(3), -2*sqrt(3)-1], + [ 6, 4+2*sqrt(3), -1+2*sqrt(3)] + ); + +ok is_square( + [ 6, -2*sqrt(3), 3-2*sqrt(3)], + [-2, -2*sqrt(3), 3-2*sqrt(3)], + [-2, 4-2*sqrt(3), 3+2*sqrt(3)], + [ 6, 4-2*sqrt(3), 3+2*sqrt(3)], + ); done_testing(); diff --git a/challenge-123/cheok-yin-fung/perl/ch-2a.pl b/challenge-123/cheok-yin-fung/perl/ch-2a.pl index 533bebf029..a7ea05eea8 100644 --- a/challenge-123/cheok-yin-fung/perl/ch-2a.pl +++ b/challenge-123/cheok-yin-fung/perl/ch-2a.pl @@ -1,10 +1,12 @@ #!/usr/bin/perl # The Weekly Challenge 123 # Task 2 extension: Square/Cube/Hypercube Points -# Usage: ch-2a.pl (optional $k) (optional)$D +# Usage: $ ch-2a.pl (optional) $k (optional)$D # $k: 2 or 3 or 4, stands for square or cube or hypercube, default is 3 # $D: 2 or above, cannot be smaller than $k, default is same as $k +# Note: check out $ diff ch-2a.pl ch-2ax.pl + use strict; use warnings; use v5.10.0; @@ -26,12 +28,18 @@ sub is_square { my $v0 = vec_subtract($p0, $p1); my $v1 = vec_subtract($p0, $p2); my $v2 = vec_subtract($p0, $p3); - return 0 unless (vec_prod($v1, $v2) == 0) xor - (vec_prod($v0, $v2) == 0) xor - (vec_prod($v0, $v1) == 0); + return 0 unless (vec_prod_f($v1, $v2) == 0) xor + (vec_prod_f($v0, $v2) == 0) xor + (vec_prod_f($v0, $v1) == 0); +#========== BEGIN: diff of ch-2a.pl and ch-2ax.pl ========= 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) ); +#=========== END: diff of ch-2a.pl and ch-2ax.pl ========== +# COMMENT: +# this test is mathematically NOT necessary, +# and if you check it against ch-2ax.pl, +# you will find this section is the source of failed tests! my @n_vector = map { norm_f($_) } ($v0, $v1, $v2); @n_vector = sort {$a<=>$b} @n_vector; if ( $n_vector[0] == $n_vector[1] ) { @@ -46,11 +54,11 @@ sub is_cube { my @p = @_; my %v; $v{$_} = vec_subtract($p[0], $p[$_]) for (1..7); - my @ind = sort { norm($v{$a}) <=> norm($v{$b}) } keys %v; + my @ind = sort { norm_f($v{$a}) <=> norm_f($v{$b}) } keys %v; my ($N, $W, $U) = ($v{$ind[0]} , $v{$ind[1]} , $v{$ind[2]}) ; return 0 unless norm_f($N) == norm_f($W) && norm_f($N) == norm_f($U); - return 0 unless vec_prod($N,$W) == 0 && vec_prod($W,$U) == 0 - && vec_prod($U,$N) == 0; + return 0 unless vec_prod_f($N,$W) == 0 && vec_prod_f($W,$U) == 0 + && vec_prod_f($U,$N) == 0; my $NW = vec_sum($N, $W); my $WU = vec_sum($W, $U); my $UN = vec_sum($U, $N); @@ -93,19 +101,19 @@ sub is_hypercube { my @p = @_; my %v; $v{$_} = vec_subtract($p[0], $p[$_]) for (1..15); - my @ind = sort { norm($v{$a}) <=> norm($v{$b}) } keys %v; + my @ind = sort { norm_f($v{$a}) <=> norm_f($v{$b}) } keys %v; my ($N, $W, $U, $A) = ( $v{$ind[0]}, $v{$ind[1]} , $v{$ind[2]}, $v{$ind[3]} ); return 0 unless norm_f($N) == norm_f($W) && norm_f($W) == norm_f($U) && norm_f($U) == norm_f($A); return 0 unless - vec_prod($N, $W) == 0 && - vec_prod($N, $U) == 0 && - vec_prod($N, $A) == 0 && - vec_prod($A, $W) == 0 && - vec_prod($A, $U) == 0 && - vec_prod($W, $U) == 0 ; + vec_prod_f($N, $W) == 0 && + vec_prod_f($N, $U) == 0 && + vec_prod_f($N, $A) == 0 && + vec_prod_f($A, $W) == 0 && + vec_prod_f($A, $U) == 0 && + vec_prod_f($W, $U) == 0 ; my $AU = vec_sum($A, $U); my $AW = vec_sum($A, $W); @@ -159,6 +167,10 @@ sub vec_prod { return $sum; } +sub vec_prod_f { + return sprintf("%f", vec_prod($_[0], $_[1])); +} + sub norm { my $p = $_[0]; my $sum = 0; @@ -204,7 +216,7 @@ sub vec_subtract { -# 4 tests +# 9 tests ok is_square( [1,0], [0,1], [-1,0],[0,-1]) == 1, "on x-axis and y-axis"; ok is_square( [5/sqrt(26), 1/sqrt(26)], @@ -227,7 +239,9 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of larger size (multipled by 2.7), caught by the equalities"; + == 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))], @@ -235,7 +249,9 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of larger size (multipled by 2.8), caught by the equalities"; + == 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))], @@ -243,7 +259,9 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of larger size (multipled by 4.0), caught by the equalities"; + == 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))], @@ -251,7 +269,10 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of a much smaller size (multiple by 0.0009), caught by the equalities (_\"not ok\" is normal_)"; + == 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 ac3ba42cce..cc5521e311 100644 --- a/challenge-123/cheok-yin-fung/perl/ch-2ax.pl +++ b/challenge-123/cheok-yin-fung/perl/ch-2ax.pl @@ -1,16 +1,17 @@ #!/usr/bin/perl # The Weekly Challenge 123 # Task 2 extension: Square/Cube/Hypercube Points -# Usage: ch-2a.pl (optional $k) (optional)$D +# Usage: ch-2ax.pl (optional) $k (optional)$D # $k: 2 or 3 or 4, stands for square or cube or hypercube, default is 3 # $D: 2 or above, cannot be smaller than $k, default is same as $k +# Note: check out $ diff ch-2a.pl ch-2ax.pl use strict; use warnings; use v5.10.0; use Test::More tests => 14; -use Algorithm::Combinatorics qw(permutations); #use for cube and hypercube +use Algorithm::Combinatorics qw(permutations); #use for hypercube my $k = $ARGV[0] || 3; @@ -26,12 +27,12 @@ sub is_square { my $v0 = vec_subtract($p0, $p1); my $v1 = vec_subtract($p0, $p2); my $v2 = vec_subtract($p0, $p3); - return 0 unless (vec_prod($v1, $v2) == 0) xor - (vec_prod($v0, $v2) == 0) xor - (vec_prod($v0, $v1) == 0); + return 0 unless (vec_prod_f($v1, $v2) == 0) xor + (vec_prod_f($v0, $v2) == 0) xor + (vec_prod_f($v0, $v1) == 0); 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] ) { + if ( $n_vector[0] == $n_vector[1] ) { return 1; } else { @@ -43,11 +44,11 @@ sub is_cube { my @p = @_; my %v; $v{$_} = vec_subtract($p[0], $p[$_]) for (1..7); - my @ind = sort { norm($v{$a}) <=> norm($v{$b}) } keys %v; + my @ind = sort { norm_f($v{$a}) <=> norm_f($v{$b}) } keys %v; my ($N, $W, $U) = ($v{$ind[0]} , $v{$ind[1]} , $v{$ind[2]}) ; return 0 unless norm_f($N) == norm_f($W) && norm_f($N) == norm_f($U); - return 0 unless vec_prod($N,$W) == 0 && vec_prod($W,$U) == 0 - && vec_prod($U,$N) == 0; + return 0 unless vec_prod_f($N,$W) == 0 && vec_prod_f($W,$U) == 0 + && vec_prod_f($U,$N) == 0; my $NW = vec_sum($N, $W); my $WU = vec_sum($W, $U); my $UN = vec_sum($U, $N); @@ -81,10 +82,10 @@ sub is_cube { 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); + 2*norm_f($N) == norm_f($NW) && + norm_f($NW) == norm_f($WU) && + norm_f($WU) == norm_f($UN) && + 3*norm_f($N) == norm_f($NWU); =cut return 1; } @@ -97,19 +98,19 @@ sub is_hypercube { my @p = @_; my %v; $v{$_} = vec_subtract($p[0], $p[$_]) for (1..15); - my @ind = sort { norm($v{$a}) <=> norm($v{$b}) } keys %v; + my @ind = sort { norm_f($v{$a}) <=> norm_f($v{$b}) } keys %v; my ($N, $W, $U, $A) = ( $v{$ind[0]}, $v{$ind[1]} , $v{$ind[2]}, $v{$ind[3]} ); return 0 unless norm_f($N) == norm_f($W) && norm_f($W) == norm_f($U) && norm_f($U) == norm_f($A); return 0 unless - vec_prod($N, $W) == 0 && - vec_prod($N, $U) == 0 && - vec_prod($N, $A) == 0 && - vec_prod($A, $W) == 0 && - vec_prod($A, $U) == 0 && - vec_prod($W, $U) == 0 ; + vec_prod_f($N, $W) == 0 && + vec_prod_f($N, $U) == 0 && + vec_prod_f($N, $A) == 0 && + vec_prod_f($A, $W) == 0 && + vec_prod_f($A, $U) == 0 && + vec_prod_f($W, $U) == 0 ; my $AU = vec_sum($A, $U); my $AW = vec_sum($A, $W); @@ -149,17 +150,17 @@ sub is_hypercube { 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); + 2*norm_f($N) == norm_f($NW) && + norm_f($NW) == norm_f($AU) && + norm_f($NW) == norm_f($UN) && + norm_f($NW) == norm_f($WU) && + norm_f($NW) == norm_f($AW) && + norm_f($NW) == norm_f($AN) && + 3*norm_f($N) == norm_f($UNW) && + 3*norm_f($N) == norm_f($ANW) && + 3*norm_f($N) == norm_f($AWU) && + 3*norm_f($N) == norm_f($AUN) && + 4*norm_f($N) == norm_f($AUNW); =cut return 1; } @@ -177,6 +178,10 @@ sub vec_prod { return $sum; } +sub vec_prod_f { + return sprintf("%f", vec_prod($_[0], $_[1])); +} + sub norm { my $p = $_[0]; my $sum = 0; @@ -222,7 +227,7 @@ sub vec_subtract { -# 4 tests +# 9 tests ok is_square( [1,0], [0,1], [-1,0],[0,-1]) == 1, "on x-axis and y-axis"; ok is_square( [5/sqrt(26), 1/sqrt(26)], @@ -245,7 +250,9 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of larger size (multipled by 2.7), caught by the equalities"; + == 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))], @@ -253,7 +260,9 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of larger size (multipled by 2.8), caught by the equalities"; + == 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))], @@ -261,7 +270,9 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of larger size (multipled by 4.0), caught by the equalities"; + == 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))], @@ -269,7 +280,10 @@ 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))] ) - == 1, "arctan(1/5) by atan2() of a much smaller size (multiple by 0.0009), caught by the equalities (_\"not ok\" is normal_)"; + == 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"; -- cgit From 85df61729cc854d57e1eb7e5f653a0a95fbacc31 Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Fri, 30 Jul 2021 13:19:07 +0800 Subject: update week 123, esp ch-2x.pl --- challenge-123/cheok-yin-fung/perl/ch-2a.pl | 1 + challenge-123/cheok-yin-fung/perl/ch-2ax.pl | 1 + challenge-123/cheok-yin-fung/perl/ch-2x.pl | 310 ++++++++++++++++++++++++++++ 3 files changed, 312 insertions(+) create mode 100644 challenge-123/cheok-yin-fung/perl/ch-2x.pl diff --git a/challenge-123/cheok-yin-fung/perl/ch-2a.pl b/challenge-123/cheok-yin-fung/perl/ch-2a.pl index a7ea05eea8..e257c351dd 100644 --- a/challenge-123/cheok-yin-fung/perl/ch-2a.pl +++ b/challenge-123/cheok-yin-fung/perl/ch-2a.pl @@ -6,6 +6,7 @@ # $D: 2 or above, cannot be smaller than $k, default is same as $k # Note: check out $ diff ch-2a.pl ch-2ax.pl +# ALSO: ch-2x.pl is the best implementation. use strict; use warnings; diff --git a/challenge-123/cheok-yin-fung/perl/ch-2ax.pl b/challenge-123/cheok-yin-fung/perl/ch-2ax.pl index cc5521e311..5aab9c492c 100644 --- a/challenge-123/cheok-yin-fung/perl/ch-2ax.pl +++ b/challenge-123/cheok-yin-fung/perl/ch-2ax.pl @@ -6,6 +6,7 @@ # $D: 2 or above, cannot be smaller than $k, default is same as $k # Note: check out $ diff ch-2a.pl ch-2ax.pl +# ALSO: ch-2x.pl is the best implementation. use strict; use warnings; use v5.10.0; diff --git a/challenge-123/cheok-yin-fung/perl/ch-2x.pl b/challenge-123/cheok-yin-fung/perl/ch-2x.pl new file mode 100644 index 0000000000..968afdfad9 --- /dev/null +++ b/challenge-123/cheok-yin-fung/perl/ch-2x.pl @@ -0,0 +1,310 @@ +#!/usr/bin/perl +# The Weekly Challenge 123 +# Task 2 extension: Square/Cube/Hypercube Points +# Usage: $ ch-2x.pl (optional) $k (optional)$D +# $k: 2 or 3 or 4, stands for square or cube or hypercube, default is 3 +# $D: 2 or above, cannot be smaller than $k, default is same as $k + +use strict; +use warnings; +use v5.10.0; +use Test::More tests => 14; + +use Algorithm::Combinatorics qw(permutations); #use for hypercube + + +my $k = $ARGV[0] || 3; +my $D = $ARGV[1] || $k; + +die "Usage: ch-2x.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; + + +sub is_square { + my ($p0,$p1,$p2,$p3) = @_; + my $v0 = vec_subtract($p0, $p1); + my $v1 = vec_subtract($p0, $p2); + my $v2 = vec_subtract($p0, $p3); + return 0 unless (vec_prod_f($v1, $v2) == 0) xor + (vec_prod_f($v0, $v2) == 0) xor + (vec_prod_f($v0, $v1) == 0); + my @n_vector = map { norm_f($_) } ($v0, $v1, $v2); + @n_vector = sort {$a<=>$b} @n_vector; + if ( $n_vector[0] == $n_vector[1] ) { + return 1; + } + else { + return 0; + } +} + +sub is_cube { + my @p = @_; + my %v; + $v{$_} = vec_subtract($p[0], $p[$_]) for (1..7); + my @ind = sort { norm_f($v{$a}) <=> norm_f($v{$b}) } keys %v; + my ($N, $W, $U) = ($v{$ind[0]} , $v{$ind[1]} , $v{$ind[2]}) ; + return 0 unless norm_f($N) == norm_f($W) && norm_f($N) == norm_f($U); + return 0 unless vec_prod_f($N,$W) == 0 && vec_prod_f($W,$U) == 0 + && vec_prod_f($U,$N) == 0; + my $NW = vec_sum($N, $W); + my $WU = vec_sum($W, $U); + my $UN = vec_sum($U, $N); + my $bool = undef; + if (vec_same_f($NW, $v{$ind[3]})) { + if ( vec_same_f($WU, $v{$ind[4]}) + && vec_same_f($UN, $v{$ind[5]}) ) { $bool = 1; + } elsif ( vec_same_f($WU, $v{$ind[5]}) + && vec_same_f($UN, $v{$ind[4]}) ) { $bool = 1; + } + } + if (!$bool && vec_same_f($NW, $v{$ind[4]})) { + if ( vec_same_f($WU, $v{$ind[3]}) + && vec_same_f($UN, $v{$ind[5]}) ) { $bool = 1; + } elsif ( vec_same_f($WU, $v{$ind[5]}) + && vec_same_f($UN, $v{$ind[3]}) ) { $bool = 1; + } + } + if (!$bool && vec_same_f($NW, $v{$ind[5]})) { + if ( vec_same_f($WU, $v{$ind[4]}) + && vec_same_f($UN, $v{$ind[3]}) ) { $bool = 1; + } elsif ( vec_same_f($WU, $v{$ind[3]}) + && vec_same_f($UN, $v{$ind[4]}) ) { $bool = 1; + } else { + return 0; + } + } + return 0 if !$bool; + + my $NWU = vec_sum( $N, $WU); + if ( vec_same_f( $v{$ind[6]} , $NWU ) ) { + return 1; + } + else { + return 0; + } +} + +sub is_hypercube { + my @p = @_; + my %v; + $v{$_} = vec_subtract($p[0], $p[$_]) for (1..15); + my @ind = sort { norm_f($v{$a}) <=> norm_f($v{$b}) } keys %v; + my ($N, $W, $U, $A) = ( $v{$ind[0]}, $v{$ind[1]} , + $v{$ind[2]}, $v{$ind[3]} ); + return 0 unless + norm_f($N) == norm_f($W) && norm_f($W) == norm_f($U) + && norm_f($U) == norm_f($A); + return 0 unless + vec_prod_f($N, $W) == 0 && + vec_prod_f($N, $U) == 0 && + vec_prod_f($N, $A) == 0 && + vec_prod_f($A, $W) == 0 && + vec_prod_f($A, $U) == 0 && + vec_prod_f($W, $U) == 0 ; + + my $AU = vec_sum($A, $U); + my $AW = vec_sum($A, $W); + my $AN = vec_sum($A, $N); + my $NW = vec_sum($N, $W); + my $WU = vec_sum($W, $U); + my $UN = vec_sum($U, $N); + my $bool_face = undef; + my $iter_face = permutations([$AU, $UN, $NW, $WU, $AW, $AN]); + while (!$bool_face && (my $p = $iter_face->next)) { + $bool_face = + vec_same_f($v{$ind[4]}, $p->[0]) && + vec_same_f($v{$ind[5]}, $p->[1]) && + vec_same_f($v{$ind[6]}, $p->[2]) && + vec_same_f($v{$ind[7]}, $p->[3]) && + vec_same_f($v{$ind[8]}, $p->[4]) && + vec_same_f($v{$ind[9]}, $p->[5]) ; + } + return 0 if !$bool_face; + + my $UNW = vec_sum($UN, $W); + my $ANW = vec_sum($NW, $A); + my $AWU = vec_sum($WU, $A); + my $AUN = vec_sum($UN, $A); + my $bool_cube = undef; + my $iter_cube = permutations([$UNW, $ANW, $AWU, $AUN]); + while (!$bool_cube && (my $p = $iter_cube->next)) { + $bool_cube = + vec_same_f($v{$ind[10]}, $p->[0]) && + vec_same_f($v{$ind[11]}, $p->[1]) && + vec_same_f($v{$ind[12]}, $p->[2]) && + vec_same_f($v{$ind[13]}, $p->[3]); + } + return 0 if !$bool_cube; + + my $AUNW = vec_sum($AU,$NW); + if ( vec_same_f($v{$ind[14]}, $AUNW) ) { + return 1; + } + else { + return 0; + } +} + +sub vec_prod { + my $first = $_[0]; + my $second = $_[1]; + warn "Not the same dimension in vec_prod \n" if $first->$#* != $second->$#*; + my $sum = 0; + $sum+= ($first->[$_]*$second->[$_]) for (0..$first->$#*); + return $sum; +} + +sub vec_prod_f { + return sprintf("%f", vec_prod($_[0], $_[1])); +} + +sub norm { + my $p = $_[0]; + my $sum = 0; + $sum+= ($p->[$_])**2 for (0..$p->$#*); + return $sum; +} + +sub norm_f { + return sprintf("%f", norm($_[0])); +} + +sub vec_sum { + my $first = $_[0]; + my $second = $_[1]; + my $ans = []; + warn "Not the same dimension in vec_sum \n" if $first->$#* != $second->$#*; + for my $s (0..$first->$#*) { + push $ans->@*, $first->[$s] + $second->[$s]; + } + return $ans; +} + +sub vec_same { + my $first = $_[0]; + my $second = $_[1]; + warn "Not the same dimension in vec_same_f \n" if $first->$#* != $second->$#*; + for my $s (0..$first->$#*) { + return undef if $first->[$s] != $second->[$s]; + } + return 1; +} + +sub vec_same_f { + my $first = $_[0]; + my $second = $_[1]; + warn "Not the same dimension in vec_same_f \n" if $first->$#* != $second->$#*; + for my $s (0..$first->$#*) { + return undef if sprintf("%f",$first->[$s]) != sprintf("%f",$second->[$s]); + } + return 1; +} + +sub vec_subtract { + my $first = $_[0]; + my $second = $_[1]; + my $ans = []; + warn "Not the same dimension in vec_subtract\n" if $first->$#* != $second->$#*; + for my $s (0..$first->$#*) { + push $ans->@*, $second->[$s] - $first->[$s]; + } + return $ans; +} + + + +# 9 tests +ok is_square( [1,0], [0,1], [-1,0],[0,-1]) == 1, "on x-axis and y-axis"; + +ok is_square( [5/sqrt(26), 1/sqrt(26)], + [-1/sqrt(26), 5/sqrt(26)], + [-5/sqrt(26), -1/sqrt(26)], + [1/sqrt(26), -5/sqrt(26)]) == 1, + "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))] + ) + == 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))] + ) + == 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))] + ) + == 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))] + ) + == 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))] + ) + == 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"; + +# 4 tests +ok is_cube( [1, 1, 1], [1, 1, 0], [1, 0, 1], [1, 0, 0], + [0, 1, 1], [0, 1, 0], [0, 0, 1], [0, 0, 0] ) == 1, + "standard 2**3"; +ok is_cube([ -2, -2, -2], [ -2, -2, 2], [ -2, 2, -2], [ -2, 2, 2], + [ 2, -2, -2], [ 2, -2, 2], [ 2, 2, -2], [ 2, 2, 2]) == 1, + "centre at origin"; +ok is_cube( + [-2, -2*sqrt(3), 3-2*sqrt(3)] , + [-2, 4-2*sqrt(3), 3+2*sqrt(3)], + [-2, 2*sqrt(3), -2*sqrt(3)-1 ], + [-2, 4+2*sqrt(3), -1+2*sqrt(3) ], + [ 6, -2*sqrt(3), 3-2*sqrt(3)], + [ 6, 4-2*sqrt(3), 3+2*sqrt(3)], + [ 6, 2*sqrt(3), -2*sqrt(3)-1], + [ 6, 4+2*sqrt(3), -1+2*sqrt(3)] + ) + == 1, "a rotated cube centred at (2,2,1)"; +ok is_cube([ 2, 2, 2], [ 2, 3, 2], [ 2, 2, 3], [ 2, 4, 2], + [ 3, 3, 2], [ 2, 2, 1], [ 2, 3, 2], [ 2, 7, 3]) == 0, + "this is not a cube"; + +# 1 test +ok is_hypercube( + [0,0,0,0],[0,0,0,1],[0,0,1,0], + [0,0,1,1],[0,1,0,0],[0,1,0,1], + [0,1,1,0],[0,1,1,1],[1,0,0,0], + [1,0,0,1],[1,0,1,0],[1,0,1,1], + [1,1,0,0],[1,1,0,1],[1,1,1,0], + [1,1,1,1] + ) == 1, "hypercube"; +done_testing(); -- cgit From 34c168160f7f115eb789edac96422b3dd27ee5bd Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Fri, 30 Jul 2021 09:37:12 +0100 Subject: - Added solutions by Pete Houston. --- challenge-123/pete-houston/perl/ch-1.pl | 27 + challenge-123/pete-houston/perl/ch-2.pl | 68 +++ stats/pwc-current.json | 311 +++++------ stats/pwc-language-breakdown-summary.json | 56 +- stats/pwc-language-breakdown.json | 828 +++++++++++++++--------------- stats/pwc-leaders.json | 732 +++++++++++++------------- stats/pwc-summary-1-30.json | 36 +- stats/pwc-summary-121-150.json | 34 +- stats/pwc-summary-151-180.json | 132 ++--- stats/pwc-summary-181-210.json | 112 ++-