RabbitFarm
2022-05-01
The Weekly Challenge 162
The examples used here are from The Weekly Challenge problem statement and demonstrate the working solution.
Part 1
Write a script to generate the check digit of a given ISBN-13 code.
Solution
use strict;
use warnings;
sub isbn_check_digit{
my($isbn) = @_;
my $i = 0;
my @weights = (1, 3);
my $check_sum = 0;
my $check_digit;
map {$check_sum += $_ * $weights[$i]; $i = $i == 0 ? 1 : 0} split(//, $isbn);
$check_digit = $check_sum % 10;
return 10 - $check_digit;
}
MAIN:{
print isbn_check_digit(978030640615) . "\n";
}
Sample Run
$ perl perl/ch-1.pl
7
References
posted at: 14:34 by: Adam Russell | path: /perl | permanent link to this entry