aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-10-18 11:18:11 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-10-18 11:18:11 +0100
commitf1f0e2555ff76d6dc26ab4336ccf9ae79ab25a9f (patch)
tree4e2120493f614f7c188d7ecb25d5e8a5674c9af5
parent6b3fec8f9c258a2b4d8da3f50d52efa6a2380a60 (diff)
downloadperlweeklychallenge-club-f1f0e2555ff76d6dc26ab4336ccf9ae79ab25a9f.tar.gz
perlweeklychallenge-club-f1f0e2555ff76d6dc26ab4336ccf9ae79ab25a9f.tar.bz2
perlweeklychallenge-club-f1f0e2555ff76d6dc26ab4336ccf9ae79ab25a9f.zip
135
-rw-r--r--challenge-135/james-smith/perl/ch-1.pl31
-rw-r--r--challenge-135/james-smith/perl/ch-2.pl37
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;
+}
+