Converting ISBN-13 to ISBN-10 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 -&gt; 0000000000
9780201882957 -&gt; 0201882957
9781420951301 -&gt; 1420951300
9780452284234 -&gt; 0452284236
9781292101767 -&gt; 1292101768
9780345391803 -&gt; 0345391802


Note the leading zeroes!
Retina,  44  39 28 bytes

&gt;,L3,-2`.+
.
$.&gt;`**
_{11}

_


Try it online!

Explanation

Time to show off some new Retina features. :)

&gt;,L3,-2`.+


We match the entire input with .+, return that match L, but select only the characters 3 (zero-based) to -2 (penultimate), inclusive. We also print the result without a trailing linefeed (&gt;).

Now, subtracting things in Retina is a bit annoying. But luckily, we're working modulo 11, so we can just invert the coefficients of the linear combination (mod 11) and add everything up. In other words, if the constraint is:

10*G + 9*G + 8*P + … + 3*T + 2*T + 1*c = 0 (mod 11)


then we get:

c = 1*G + 2*G + 3*P + … + 8*T + 9*T (mod 11)


That simplifies things a lot here:

.
$.&gt;`**


We replace each character with that thing at the bottom. * is Retina's repetition operator. It's right-associative and it has implicit operands $&amp; on the left and _ on the right, so the substitution is actually short for $.&gt;`*$&amp;*_. $&amp;*_ creates a string of d underscores, where d is the digit we're currently replacing. Then $.&gt;` is the length of the string up to and including the match.1 Hence, the entire expression results in a unary representation of the nth term of our linear combination.

_{11}



Doing the actual modulo is trivial in unary: we just drop all complete sets of 11 underscores.

_


Finally, we count how many underscores remain and print the result, which completes the ISBN-10.



1 How does $.&gt;` give the length of the string up to and including the match? You might be familiar with $` in regex substitutions, which gives you the string up to (but excluding) the match. By inserting a &gt;, we can shift the context of the $` to the separator between the current match and the next (which is an empty string between the current digit and the next). That separator's $` will include the current match. So $&gt;` is a shorter way to write $`$&amp;. Finally, for all $x-type substitution elements, Retina lets you insert a . after the $ to get its length.