diff options
| -rw-r--r-- | challenge-123/james-smith/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-123/james-smith/perl/ch-2.pl | 42 |
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-123/james-smith/perl/ch-1.pl b/challenge-123/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..2348c8b946 --- /dev/null +++ b/challenge-123/james-smith/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); + +my @TESTS = ( + [ 0, 1 ], +); + +is( my_function($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub my_function { + return 1; +} + diff --git a/challenge-123/james-smith/perl/ch-2.pl b/challenge-123/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..af4005104e --- /dev/null +++ b/challenge-123/james-smith/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +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 ], +); + +is( is_square(@{$_->[0]}), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub is_square { + my @pts = @_; + + ## If we measure the squared distances between each + ## pari of points of a square we get two distances - + ## the edge and diagonal. + ## The latter being twice the former... + ## + ## No other combination of points has this property. + + ## Compute distances... + my %D; + while(@pts>1) { + 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.. +} + |
