aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-10-18 14:51:27 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-10-18 14:51:27 +0100
commit624df489701537fdbe85a3d72447975258d61161 (patch)
treeae1bc9a1af6c7f4113276a197f832f9e67ed2410
parent2ce0d1e77d6261b0a2ba07369482c78c7d237ab4 (diff)
downloadperlweeklychallenge-club-624df489701537fdbe85a3d72447975258d61161.tar.gz
perlweeklychallenge-club-624df489701537fdbe85a3d72447975258d61161.tar.bz2
perlweeklychallenge-club-624df489701537fdbe85a3d72447975258d61161.zip
tidyingup
-rw-r--r--challenge-135/james-smith/perl/ch-1.pl14
-rw-r--r--challenge-135/james-smith/perl/ch-2.pl13
2 files changed, 19 insertions, 8 deletions
diff --git a/challenge-135/james-smith/perl/ch-1.pl b/challenge-135/james-smith/perl/ch-1.pl
index 62022269aa..780b93d005 100644
--- a/challenge-135/james-smith/perl/ch-1.pl
+++ b/challenge-135/james-smith/perl/ch-1.pl
@@ -16,7 +16,8 @@ my @TESTS = (
[ 1000, 'Even digits' ],
);
-is( middle3($_->[0]), $_->[1] ) foreach @TESTS;
+is( middle3( $_->[0]), $_->[1] ) foreach @TESTS;
+is( middle3no($_->[0]), $_->[1] ) foreach @TESTS[0..2,4];
done_testing();
@@ -26,6 +27,15 @@ sub middle3 {
$n = abs $n;
return length $n < 3 ? 'Too short'
: (length $n)%2 ? substr $n, (-3 + length $n ) / 2, 3
- : 'Even digits';
+ : 'Even digits'
+ ;
+}
+
+sub middle3no {
+ my $n = abs shift;
+ return length $n < 3 ? 'Too short'
+ : (length $n)%2 ? substr $n, (-3 + length $n ) / 2, 3
+ : 'Even digits'
+ ;
}
diff --git a/challenge-135/james-smith/perl/ch-2.pl b/challenge-135/james-smith/perl/ch-2.pl
index 2585b4f41d..1e0097ff0d 100644
--- a/challenge-135/james-smith/perl/ch-2.pl
+++ b/challenge-135/james-smith/perl/ch-2.pl
@@ -14,24 +14,25 @@ my @TESTS = (
[ 'B0YBKL9', 1 ],
[ '0263494', 1 ],
[ '0540528', 1 ],
+ [ '1A34O67', 0 ],
[ 'BG03Y86', 1 ],
);
-is( is_sedol($_->[0]), $_->[1] ) foreach @TESTS;
+is( is_sedol($_->[0]), $_->[1] ) for @TESTS;
done_testing();
sub is_sedol {
-## Check correct format...
+## Check correct format... numbers and consonants only
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);
+## Accumulator and weights for each charachter
+ my( $t, @w ) = 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];
+ $t += ( /\d/ ? $_ : -55 + ord $_ ) * shift @w for split //, $_[0];
-## Return true if total modulo 10 is 0
+## Return true (1) if total modulo 10 is 0, and false (0) otherwise
return $t % 10 ? 0 : 1;
}