diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-04-30 00:27:30 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-04-30 00:27:30 +0100 |
| commit | c6465a7db09b23db32e77205cf4f8e2b49452881 (patch) | |
| tree | 998a18d6f24160ca8bd8b18b91a2c791313b45a0 /challenge-162 | |
| parent | 3ae59ea102b98dcbd9d7dcd3e30a49422191d90c (diff) | |
| download | perlweeklychallenge-club-c6465a7db09b23db32e77205cf4f8e2b49452881.tar.gz perlweeklychallenge-club-c6465a7db09b23db32e77205cf4f8e2b49452881.tar.bz2 perlweeklychallenge-club-c6465a7db09b23db32e77205cf4f8e2b49452881.zip | |
added some extra examples and tests for checksum & validate
Diffstat (limited to 'challenge-162')
| -rw-r--r-- | challenge-162/james-smith/perl/ch-1.pl | 27 |
1 files changed, 22 insertions, 5 deletions
diff --git a/challenge-162/james-smith/perl/ch-1.pl b/challenge-162/james-smith/perl/ch-1.pl index 71bba56e95..cbb4ed5663 100644 --- a/challenge-162/james-smith/perl/ch-1.pl +++ b/challenge-162/james-smith/perl/ch-1.pl @@ -9,19 +9,36 @@ use Benchmark qw(cmpthese timethis); use Data::Dumper qw(Dumper); my @TESTS = ( - [ '978-0-306-40615-7' , 1 ], - [ '978-3-16-148410-0', 1 ], - [ '978-0-306-40615-3' , 0 ], - [ '978-3-16-148410-4', 0 ], + [ '978-0-306-40615-7', 1, 7 ], + [ '978-3-16-148410-0', 1, 0 ], + [ '978-0-306-40615-3', 0, 7 ], + [ '978-3-16-148410-4', 0, 0 ], + [ '978-8-3516-1020-8', 1, 8 ], + [ '978-0-2788-4569-5', 1, 5 ], + [ '978-4-0677-1190-1', 1, 1 ], + [ '978-1-6672-1485-6', 1, 6 ], + [ '978-4-0881-0106-4', 1, 4 ], + [ '978-6-6163-6174-2', 1, 2 ], + [ '978-9-0613-4792-7', 1, 7 ], + [ '978-3-0302-4843-7', 1, 7 ], + [ '978-0-1906-8107-4', 1, 4 ], + [ '978-7-5021-7110-0', 1, 0 ], ); is( validate_isbn13($_->[0]) || 0, $_->[1] ) foreach @TESTS; +is( checksum_isbn13($_->[0]) || 0, $_->[2] ) foreach @TESTS; done_testing(); sub validate_isbn13 { my @p = ( my $s = 0, grep {/\d/} split //, $_[0] ); - $s += 3*shift(@p) + shift @p for 0..6; + $s += 3*shift(@p) + shift @p while @p; !($s%10); } +sub checksum_isbn13 { + my($s,@p) = ( 0, @{[grep {/\d/} split //, $_[0]]}[0..11] ); + $s -= shift(@p) + 3*shift @p while @p; + $s%10; +} + |
