diff options
| author | drbaggy <js5@sanger.ac.uk> | 2021-07-26 20:45:36 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2021-07-26 20:45:36 +0100 |
| commit | d3be7070715a1574b23eb786d5143a3fa23ea8d8 (patch) | |
| tree | b13772331882b1f5632e3be7cd1854dcd43f4e71 | |
| parent | 039bd71627180c2b0c921114d94d5529f145aa00 (diff) | |
| download | perlweeklychallenge-club-d3be7070715a1574b23eb786d5143a3fa23ea8d8.tar.gz perlweeklychallenge-club-d3be7070715a1574b23eb786d5143a3fa23ea8d8.tar.bz2 perlweeklychallenge-club-d3be7070715a1574b23eb786d5143a3fa23ea8d8.zip | |
use reverse to flip keys and values in hash to make last line logic easier
| -rw-r--r-- | challenge-123/james-smith/perl/ch-2.pl | 9 |
1 files changed, 4 insertions, 5 deletions
diff --git a/challenge-123/james-smith/perl/ch-2.pl b/challenge-123/james-smith/perl/ch-2.pl index af4005104e..7dd9dde161 100644 --- a/challenge-123/james-smith/perl/ch-2.pl +++ b/challenge-123/james-smith/perl/ch-2.pl @@ -11,6 +11,8 @@ use Data::Dumper qw(Dumper); my @TESTS = ( [ [ [10,20],[20,20],[20,10],[10,10] ], 1 ], [ [ [12,24],[16,10],[20,12],[18,16] ], 0 ], + [ [ [-2,5],[2,-5],[5,2],[-5,-2] ], 1 ], + [ [ [0,1],[1,0],[0,-1],[-1,0] ], 1 ], ); is( is_square(@{$_->[0]}), $_->[1] ) foreach @TESTS; @@ -33,10 +35,7 @@ sub is_square { my $a = shift @pts; $D{($a->[0]-$_->[0])**2+($a->[1]-$_->[1])**2}++ foreach @pts; } - my @K = keys %D; - return 0 unless @K== 2; ## More than two distances - return 1 if $K[0]*2==$K[1]; ## First no is the diagonal - return 1 if $K[1]*2==$K[0]; ## First no is the edge.. - return 0; ## Equilat triangles.. + my %F = reverse %D; + return exists $F{2} && exists $F{4} && $F{2} == 2*$F{4} || 0; } |
