From b9261a75ac76d2fc12a4d5ee179e2be3bb5a57c9 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 26 Jul 2021 12:31:09 +0100 Subject: solution 2... --- challenge-123/james-smith/perl/ch-1.pl | 22 ++++++++++++++++++ challenge-123/james-smith/perl/ch-2.pl | 42 ++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 challenge-123/james-smith/perl/ch-1.pl create mode 100644 challenge-123/james-smith/perl/ch-2.pl 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.. +} + -- cgit