aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-07-26 12:31:09 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-07-26 12:31:09 +0100
commitb9261a75ac76d2fc12a4d5ee179e2be3bb5a57c9 (patch)
tree2e635c42bda468cc981d0ff13b599f16d694d647
parent4231c2f762b397e1cacd2cb7e3c2799254fcc1a4 (diff)
downloadperlweeklychallenge-club-b9261a75ac76d2fc12a4d5ee179e2be3bb5a57c9.tar.gz
perlweeklychallenge-club-b9261a75ac76d2fc12a4d5ee179e2be3bb5a57c9.tar.bz2
perlweeklychallenge-club-b9261a75ac76d2fc12a4d5ee179e2be3bb5a57c9.zip
solution 2...
-rw-r--r--challenge-123/james-smith/perl/ch-1.pl22
-rw-r--r--challenge-123/james-smith/perl/ch-2.pl42
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..
+}
+