aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNiels van Dijke <perlboy@cpan.org>2021-07-27 23:22:14 +0000
committerNiels van Dijke <perlboy@cpan.org>2021-07-27 23:22:14 +0000
commitedb52ce397db24e01e8e4ddd9a17a2e6a6192143 (patch)
treec34a4fc2fe6ea4e93088c63edcf1335111b6b46d
parent5746a8d5a6437903f87f33b2202eddc7988a7f72 (diff)
downloadperlweeklychallenge-club-edb52ce397db24e01e8e4ddd9a17a2e6a6192143.tar.gz
perlweeklychallenge-club-edb52ce397db24e01e8e4ddd9a17a2e6a6192143.tar.bz2
perlweeklychallenge-club-edb52ce397db24e01e8e4ddd9a17a2e6a6192143.zip
Task 2 - Check diffently distance frequency count
* Add equilateral triangle - Thanks to James Curtis-Smith
-rwxr-xr-xchallenge-123/perlboy1967/perl/ch-2.pl17
1 files 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;
}