diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-07-28 00:37:14 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-28 00:37:14 +0100 |
| commit | 261ea168fa508621cd369dc6a6d3f9bfce641223 (patch) | |
| tree | 9999e8c0d1ba6ef63d150073caf9674ba1255f0d | |
| parent | fbda1c9b2f044b85cd9ad661287a8d9cc49baa67 (diff) | |
| parent | 337606a8afa9a655a3c1a4dc404714d3de6339ba (diff) | |
| download | perlweeklychallenge-club-261ea168fa508621cd369dc6a6d3f9bfce641223.tar.gz perlweeklychallenge-club-261ea168fa508621cd369dc6a6d3f9bfce641223.tar.bz2 perlweeklychallenge-club-261ea168fa508621cd369dc6a6d3f9bfce641223.zip | |
Merge pull request #4619 from PerlBoy1967/branch-for-challenge-123
Task 2 - Check diffently distance frequency count
| -rwxr-xr-x | challenge-123/perlboy1967/perl/ch-2.pl | 20 |
1 files changed, 6 insertions, 14 deletions
diff --git a/challenge-123/perlboy1967/perl/ch-2.pl b/challenge-123/perlboy1967/perl/ch-2.pl index cabec1e3e8..926188c449 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,28 +39,21 @@ 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 # will give 2 identical and on other foreach my $i (0 .. 3) { - 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; - - return 1 if (scalar keys %f == 2); + my @v = sort { $b <=> $a } values %dP; - return 0; + return ($v[0] == 8 && $v[1] == 4 ? 1 : 0); } |
