21
Introduction
In this challenge your task is to generate the ISBN-10 code for books given its ISBN-13 code, assuming that such a code exists. Such an ISBN-13 code consists of several parts separated by -
:
978-GG-PPPP-TTT-C
The letters G
(group), P
(publisher), T
(title) and C
(checksum) all stand for one digit. For the purpose of this challenge the grouping and the computation of C
(see this challenge) are not interesting and we'll drop all hyphens to make this task simpler.
An ISBN-10 number has a very similar layout:
GG-PPPP-TTT-c
The letters G
,P
and T
are the same as for the 13 digits ISBN, however c
is different (and is computed using a different algorithm). The digit c
is chosen in a way such that the following equivalence holds (digits in order):
10*G + 9*G + 8*P + … + 3*T + 2*T + 1*c = 0 (mod 11)
Example
Let us consider the ISBN number 9780345391803
: To get its corresponding ISBN-10 code we simply drop the leading 978
and the checksum 3
yielding 034539180
.
Next we need to compute the new checksum:
10*0 + 9*3 + 8*4 + 7*5 + 6*3 + 5*9 + 4*1 + 3*8 + 2*0 = 185
The next number divisible by 11
is 187
, so the new checksum is 2
and thus the resulting ISBN-10 code 0345391802
.
Rules
- Your input will always have a corresponding ISBN-10 number (ie. it is exactly 13 digits long and starts with
978
) - The input doesn't necessarily have to be a valid ISBN-13 (eg.
9780000000002
) - You're guaranteed that the resulting ISBN won't end with
X
- You may take input as an integer or string (with or without hyphens) however a precomputed list of digits are not allowed
- Your output must be a valid ISBN-10 number (with or without hyphens)
- Your output may be an integer or string (again no lists of digits)
Testcases
9780000000002 -> 0000000000
9780201882957 -> 0201882957
9781420951301 -> 1420951300
9780452284234 -> 0452284236
9781292101767 -> 1292101768
9780345391803 -> 0345391802
Note the leading zeroes!
5It doesn't affect solutions at all, but just for the sake of being That Guy, your description of how the parts of an ISBN (either -10 or -13) are separated is incorrect. The registration group element is variable length, and the number of digits for subsequent parts can vary between, and within, registration groups. For example, in both
0-684-84328-5
and99921-58-10-7
, the first part (0
and99921
respectively) is the registration group, the second part is the publisher, and so on. – Jordan – 2018-01-19T19:52:04.320510/10 sample ISBN choices – Jakob – 2018-03-24T16:01:33.690