From edb52ce397db24e01e8e4ddd9a17a2e6a6192143 Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Tue, 27 Jul 2021 23:22:14 +0000 Subject: Task 2 - Check diffently distance frequency count * Add equilateral triangle - Thanks to James Curtis-Smith --- challenge-123/perlboy1967/perl/ch-2.pl | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/challenge-123/perlboy1967/perl/ch-2.pl b/challenge-123/perlboy1967/perl/ch-2.pl index cabec1e3e8..15a7207ac2 100755 --- a/challenge-123/perlboy1967/perl/ch-2.pl +++ b/challenge-123/perlboy1967/perl/ch-2.pl @@ -11,8 +11,6 @@ use v5.16; use strict; use warnings; -use List::MoreUtils qw(frequency); - use Test::More; # Prototype(s) @@ -27,6 +25,7 @@ my $tests = [ [ [ 0, 1],[ 1, 2],[ 2, 1],[ 1, 0], 1 ], [ [-5, 0],[ 5, 0],[ 0, 1],[ 0,-1], 0 ], [ [-1, 0],[ 5, 0],[ 0, 1],[ 0,-1], 0 ], + [ [-1, 0],[ 1, 0],[ 0, sqrt(3)],[ 0, sqrt(3)/3], 0 ], # equilateral triangle ]; foreach my $t (@$tests) { @@ -40,7 +39,7 @@ done_testing(); sub areSquarePoints(\@) { my ($ar) = @_; - my @dP; + my %dP; # If all points given are coordinates of a square # then the distance between one point and the other three @@ -49,18 +48,14 @@ sub areSquarePoints(\@) { my @d; foreach my $j (0 .. 3) { next if ($i == $j); - push(@d,sqrt(($ar->[$i][0]-$ar->[$j][0])**2 + - ($ar->[$i][1]-$ar->[$j][1])**2)); + $dP{sqrt(($ar->[$i][0]-$ar->[$j][0])**2 + + ($ar->[$i][1]-$ar->[$j][1])**2)}++; } - my %f = frequency @d; - push(@dP,keys %f); } - # If all four points give same distance 'frequencies' - # then we have a square - my %f = frequency @dP; + my @v = sort { $b <=> $a } values %dP; - return 1 if (scalar keys %f == 2); + return 1 if ($v[0] == 8 && $v[1] == 4); return 0; } -- cgit From 337606a8afa9a655a3c1a4dc404714d3de6339ba Mon Sep 17 00:00:00 2001 From: Niels van Dijke Date: Tue, 27 Jul 2021 23:35:21 +0000 Subject: Cleanup and slightly shorter version --- challenge-123/perlboy1967/perl/ch-2.pl | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/challenge-123/perlboy1967/perl/ch-2.pl b/challenge-123/perlboy1967/perl/ch-2.pl index 15a7207ac2..926188c449 100755 --- a/challenge-123/perlboy1967/perl/ch-2.pl +++ b/challenge-123/perlboy1967/perl/ch-2.pl @@ -45,7 +45,6 @@ sub areSquarePoints(\@) { # then the distance between one point and the other three # will give 2 identical and on other foreach my $i (0 .. 3) { - my @d; foreach my $j (0 .. 3) { next if ($i == $j); $dP{sqrt(($ar->[$i][0]-$ar->[$j][0])**2 + @@ -55,8 +54,6 @@ sub areSquarePoints(\@) { my @v = sort { $b <=> $a } values %dP; - return 1 if ($v[0] == 8 && $v[1] == 4); - - return 0; + return ($v[0] == 8 && $v[1] == 4 ? 1 : 0); } -- cgit