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 From 3ebea6660bb9e212bb39008590758a023f7a71de Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 26 Jul 2021 12:50:07 +0100 Subject: I think there is a bit of nasty code to add to "nth-ugly" to speed this up - which remembers where to start the inner for loops - but need to get my brain around it - at least this works - and is better than a full scan of 1..N as n gets larger --- challenge-123/james-smith/perl/ch-1.pl | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/challenge-123/james-smith/perl/ch-1.pl b/challenge-123/james-smith/perl/ch-1.pl index 2348c8b946..6c24f48516 100644 --- a/challenge-123/james-smith/perl/ch-1.pl +++ b/challenge-123/james-smith/perl/ch-1.pl @@ -3,20 +3,35 @@ use strict; use warnings; -use feature qw(say); +use feature qw(say state); use Test::More; use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); -my @TESTS = ( - [ 0, 1 ], -); +my @uglies = qw(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36 40 45 48 50 54 60 64 72 75 80 81 90 96 100 108 120 125 128 135 144 150 160 162 180 192 200 216 225 240 243 250 256 270 288 300 320 324 360 375 384 400 405 432 450 480 486 500 512 540 576 600 625 640 648 675 720 729 750 768 800 810 864 900 960 972 1000); -is( my_function($_->[0]), $_->[1] ) foreach @TESTS; +my $c=0; +my @TESTS = map { [ ++$c, $_ ] } @uglies; +is( nth_ugly($_->[0]), $_->[1] ) foreach @TESTS; done_testing(); -sub my_function { - return 1; +sub nth_ugly { + my $n = shift; + state @uglies; + @uglies=(1) unless @uglies; + while(1) { + return $uglies[$n-1] if $n <= @uglies; + ## Get the next ugly.... + my $next = 0; + foreach my $n (5,3,2) { + foreach(@uglies) { + next if $_ * $n <= $uglies[-1]; + $next = $_*$n if !$next || $next > $_*$n; + last; + } + } + push @uglies, $next; + } } -- cgit From 039bd71627180c2b0c921114d94d5529f145aa00 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 26 Jul 2021 12:51:56 +0100 Subject: I think there is a bit of nasty code to add to "nth-ugly" to speed this up - which remembers where to start the inner for loops - but need to get my brain around it - at least this works - and is better than a full scan of 1..N as n gets larger --- challenge-123/james-smith/perl/ch-1.pl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/challenge-123/james-smith/perl/ch-1.pl b/challenge-123/james-smith/perl/ch-1.pl index 6c24f48516..fddf7775f4 100644 --- a/challenge-123/james-smith/perl/ch-1.pl +++ b/challenge-123/james-smith/perl/ch-1.pl @@ -18,8 +18,7 @@ done_testing(); sub nth_ugly { my $n = shift; - state @uglies; - @uglies=(1) unless @uglies; + state @uglies = (1); while(1) { return $uglies[$n-1] if $n <= @uglies; ## Get the next ugly.... -- cgit From aa609a626af59795fc92e62bdbdb8fa446728602 Mon Sep 17 00:00:00 2001 From: chirvasitua Date: Mon, 26 Jul 2021 08:41:50 -0400 Subject: 1st commit on 123_raku --- challenge-123/stuart-little/raku/ch-1.raku | 14 ++++++++++++++ challenge-123/stuart-little/raku/ch-2.raku | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100755 challenge-123/stuart-little/raku/ch-1.raku create mode 100755 challenge-123/stuart-little/raku/ch-2.raku diff --git a/challenge-123/stuart-little/raku/ch-1.raku b/challenge-123/stuart-little/raku/ch-1.raku new file mode 100755 index 0000000000..1dee01f189 --- /dev/null +++ b/challenge-123/stuart-little/raku/ch-1.raku @@ -0,0 +1,14 @@ +#!/usr/bin/env raku +use v6; + +# run as