diff options
| -rw-r--r-- | challenge-076/james-smith/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-076/james-smith/perl/ch-2.pl | 22 | ||||
| -rw-r--r-- | challenge-145/james-smith/perl/ch-1.pl | 33 | ||||
| -rw-r--r-- | challenge-145/james-smith/perl/ch-2.pl | 71 |
4 files changed, 148 insertions, 0 deletions
diff --git a/challenge-076/james-smith/perl/ch-1.pl b/challenge-076/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..2348c8b946 --- /dev/null +++ b/challenge-076/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-076/james-smith/perl/ch-2.pl b/challenge-076/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..2348c8b946 --- /dev/null +++ b/challenge-076/james-smith/perl/ch-2.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-145/james-smith/perl/ch-1.pl b/challenge-145/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..30a9c82f5a --- /dev/null +++ b/challenge-145/james-smith/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/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 = ( + [ [1,2,3], [4,5,6], 32 ], + + [ [1..10], [1..10], 385 ], + [ [1..100], [1..100], 338_350 ], + [ [1..1e3], [1..1e3], 333_833_500 ], + [ [1..1e4], [1..1e4], 333_383_335_000 ], + [ [1..1e5], [1..1e5], 333_338_333_350_000 ], + [ [1..1e6], [1..1e6], 333_333_833_333_500_000 ], + + [ [1..10], [reverse 1..10], 220 ], +); + +is( dot_product($_->[0],$_->[1]), $_->[2] ) foreach @TESTS; + +done_testing(); + +sub dot_product { + my ($t,@y) = (0,@{$_[1]}); + $t += $_ * shift @y foreach @{$_[0]}; + $t; +} + diff --git a/challenge-145/james-smith/perl/ch-2.pl b/challenge-145/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..9748778fbf --- /dev/null +++ b/challenge-145/james-smith/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!/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 = ( + [ 'redivider', 'r redivider e edivide d divid i ivi v'], + [ 'deific', 'd e i ifi f c'], + [ 'llama', 'l ll a ama m' ], + [ 'rotors', 'r rotor o oto t s'], + [ 'challenge', 'c h a l ll e n g'], + [ 'champion', 'c h a m p i o n'], + [ 'christmas', 'c h r i s t m a'], + [ 'tattarrattat', 't tat tattarrattat a atta attarratta tt ttarratt tarrat arra r rr'], + [ 'bobaboba', 'b bob bobabob o obabo bab a aboba'], + [ 'llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch', 'l ll a n f i r p w g y gog o e c h d b lll llll t s ili ogo ogogo ogogogo gogog' ], +); + +is( eertree($_->[0]), $_->[1] ) for @TESTS; +done_testing(); + +sub eertree { + my $str = [split //, $_[0]]; + ## Create the null tree..., two nodes... + my $tree = { + -1 => { 'start' => undef, 'edges' => {}, 'suff' => -1 }, + q() => { 'start' => undef, 'edges' => {}, 'suff' => -1 }, + }; + ## add nodes (single letter nodes to the "-1" part of the tree ) + ## (double letter nodes to the "" part of the tree ) + + add_str( $tree, $str, -1, $_, $_ ), + add_str( $tree, $str, q(), $_, $_+1 ) for 0.. @{$str}-1; + + ## grab nodes and sort into start/length order and join.... + return join q( ), + sort { $tree->{$a}{'start'} <=> $tree->{$b}{'start'} || + length $a <=> length $b + } + grep { defined $tree->{$_}{'start'} } + keys %{$tree}; +} + +sub add_str { + my( $tr, $c, $node, $st, $en ) = @_; + + ## exit loop if the start or end values are outside the string + ## OR the start and end characters are different. + + while( $st >=0 && $en < @{$c} && $c->[$st] eq $c->[$en] ) { + + ## Add new palindrome as an "edge" of the current node - if + ## no already added {use an index value!) + ## + ## Then create the new node - with a link back to the suffix + ## node... + ## + ## Finally expand the palindrome by 1 character either way... + + $tr->{$node}{'edges'}{my $s = join q(), @{$c}[$st..$en] } ||= keys %{$tr->{$node}{'edges'}}; + $tr->{$node=$s} ||= { 'start' => $st, 'edges' => {}, 'suff' => $st==$en ? -1 : $en==$st+1 ? q() : $c->[$st] }; + $st--; + $en++; + } +} + |
