aboutsummaryrefslogtreecommitdiff
path: root/challenge-162/james-smith
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-04-30 00:28:02 +0100
committerdrbaggy <js5@sanger.ac.uk>2022-04-30 00:28:02 +0100
commitf1ac923d2545249d3f3791211039b51b8f5e8ed4 (patch)
tree4ea1b1fe509ad387ab7b71b9ff92ec556efb4488 /challenge-162/james-smith
parentc6465a7db09b23db32e77205cf4f8e2b49452881 (diff)
parented9f1c31c68fa6bc8754f730e56e60ebf1c59e04 (diff)
downloadperlweeklychallenge-club-f1ac923d2545249d3f3791211039b51b8f5e8ed4.tar.gz
perlweeklychallenge-club-f1ac923d2545249d3f3791211039b51b8f5e8ed4.tar.bz2
perlweeklychallenge-club-f1ac923d2545249d3f3791211039b51b8f5e8ed4.zip
Merge branch 'master' of github.com:drbaggy/perlweeklychallenge-club
Diffstat (limited to 'challenge-162/james-smith')
-rw-r--r--challenge-162/james-smith/README.md15
1 files changed, 10 insertions, 5 deletions
diff --git a/challenge-162/james-smith/README.md b/challenge-162/james-smith/README.md
index 106cb78373..248d432347 100644
--- a/challenge-162/james-smith/README.md
+++ b/challenge-162/james-smith/README.md
@@ -24,9 +24,15 @@ Rather than computing the checksum - as we have ISBN numbers with the checksum w
```perl
sub validate_isbn13 {
- my( $s, @p ) = ( 0, grep {/\d/} split //, $_[0] );
- $s -= shift(@p) + 3*shift @p for 0..5;
- $p[0] == $s%10;
+ my @p = ( my $s = 0, grep {/\d/} split //, $_[0] );
+ $s += 3*shift(@p) + shift @p while @p;
+ !($s%10);
+}
+
+sub checksum_isbn13 {
+ my($s,@p) = ( 0, @{[grep {/\d/} split //, $_[0]]}[0..11] ); ## Remove checksum if present..
+ $s -= shift(@p) + 3*shift @p while @p;
+ $s%10;
}
```
@@ -48,7 +54,7 @@ sub _crypt {
($_ eq 'j' && ($_='i')), exists $l{$_} || ($l{$_}=[int $p/5,($p++)%5]) for grep { /[a-z]/ } split(//,$key),'a'..'i','j'..'z';
$r[$l{$_}[0]][$l{$_}[1]]=$_ for keys %l;
- my @seq = grep {/[a-z]/} split //, shift =~ s{j}{j}gr; ## Prep sequence
+ my @seq = grep {/[a-z]/} split //, shift =~ s{j}{i}gr; ## Prep sequence
while(my($m,$n)=splice @seq,0,2) { ## Loop through letter pairs
unshift(@seq,$n), $n='x' if $n && $n eq $m and $n ne 'x'; ## Deal with case when both letters the same
@@ -61,4 +67,3 @@ sub _crypt {
$out;
}
```
-