diff options
| -rw-r--r-- | challenge-135/james-smith/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-135/james-smith/perl/ch-2.pl | 37 |
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..a91fc49d88 --- /dev/null +++ b/challenge-135/james-smith/perl/ch-1.pl @@ -0,0 +1,31 @@ +#!/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 = ( + [ 1234567, 345 ], + [ -123, 123 ], + [ 1, 'Too short' ], + [ 'dred', 'Not a number' ], + [ 1000, 'Even digits' ], +); + +is( middle3($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub middle3 { + my $n = shift; + return 'Not a number' unless $n =~ m{^-?\d+$}; + return 'Too short' unless $n =~ m{\d{3}}; + return 'Even digits' if $n =~ m{^-?(?:\d\d)+$}; + $n =~ s{^-}{}; + return substr $n, (-3 + length $n ) / 2, 3; +} + diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..2585b4f41d --- /dev/null +++ b/challenge-135/james-smith/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/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 = ( + [ '2936921', 1 ], + [ '1234567', 0 ], + [ 'B0YBKL9', 1 ], + [ '0263494', 1 ], + [ '0540528', 1 ], + [ 'BG03Y86', 1 ], +); + +is( is_sedol($_->[0]), $_->[1] ) foreach @TESTS; + +done_testing(); + +sub is_sedol { +## Check correct format... + return 0 unless $_[0] =~ m{^[0-9B-HJ-NP-TW-Z]{6}\d$}; + +## Total and weights foreach digit + my( $t, @wts ) = qw(0 1 3 1 7 3 9 1); + +## Calculate SEDOL sum... note YODA sum -55 + ord $_ to avoid precedence issue + $t += shift @wts * ( $_ =~/[A-Z]/ ? -55 + ord $_ : $_ ) foreach split m//, $_[0]; + +## Return true if total modulo 10 is 0 + return $t % 10 ? 0 : 1; +} + |
