28
6
Write a function which, given the first 12 digits of an ISBN-13 code, will calculate the entire ISBN via calculating and appending an appropriate check digit.
Your function's input is a string containing the first 12 digits of the ISBN. Its output is a string containing all 13 digits.
Formal specification
Write a function which, when given a string s consisting entirely of exactly 12 decimal digits (and no other characters), returns a string t with the following properties:
- t consists of exactly 13 decimal digits (and no other characters);
- s is a prefix of t;
- the sum of all digits in odd positions in t (i.e. the first, third, fifth, etc.), plus three times the sum of all digits in even positions in t (i.e. the second, fourth, sixth, etc), is a multiple of 10.
Example / test case
Input
978030640615
Output
9780306406157
Victory condition
As a code-golf challenge, the shortest answer wins.
Is outputting the full ISBN-13 acceptable as well? – Mr. Llama – 2012-03-02T19:43:20.793
6Note that questions should really be self contained, so it would be helpful to include a description of the algorithm here. – FlipTack – 2017-11-19T10:01:45.123
For me above post is poor of example test... There would be at last 10 isbn and one of that has to return 0 as last digit... – RosLuP – 2019-03-09T19:34:05.890
1Are input and output supposed to contain dashes or only the digits? – sepp2k – 2011-02-01T19:57:08.123
1Updated the description, the input and output are only the digits – Kevin Brown – 2011-02-01T20:06:38.837