In the ISBN-10 (International Standard Book Numbering) system that was used until the end of 2006, each book is assigned a unique 10-digit code. The first nine digits uniquely identify the book itself, whereas the last digit merely serves as a check digit to detect invalid ISBN-10 codes.

ISBN
ISBN in text and barcode.

If $$x_1, \ldots, x_9$$ represent the first nine digits of an ISBN-10 code, the check digit $$x_{10}$$ is computed as \[x_{10} = (x_1+ 2x_2+ 3x_3+ 4x_4+ 5x_5+ 6x_6+ 7x_7+ 8x_8+ 9x_9)\!\!\!\!\mod{11}\,.\] As a result, $$x_{10}$$ always takes a value in between 0 and 10. If the check digit is equal to 10, it is represented in the ISBN-10 code by the uppercase letter X. As such, only a single character is needed to represent the check digit.

In the new ISBN-13 system, each book is assigned a unique 13-digit code. The first twelve digits identify the book itself, whereas the last digit merely serves as a check digit to detect invalid ISBN-13 codes. If $$x_1, \ldots, x_{12}$$ represent the first twelve digits of an ISBN-13 code, the check digit $$x_{13}$$ is computed as \[\begin{align} o &= x_1 + x_3 + x_5 + x_7 + x_9 + x_{11} \\ e &= x_2 + x_4 + x_6 + x_8 + x_{10} + x_{12} \\ x_{13} &= (10 - (o + 3e)\!\!\!\!\mod{10})\!\!\!\!\!\mod{10}\end{align}\] As a result, the check digit $$x_{13}$$ always takes a value in between 0 and 9, so that ISBN-13 codes only contain digits.

An ISBN-13 code has a fixed structure that contains quite some information. The first 3 digits are 978 or 979. Codes that start with 978 are books that also have an ISBN-10 code. After the prefix of the first 3 digits is a specification of the country group in which the book was published. This specification can take up 1 to 5 digits. This is followed by an identification of the publisher and the item. The last digit is a check digit, as described above.

It is easy to derive the ISBN-10 code associated to a book with a given ISBN-13 code. The association, however, is only valid for ISBN-13 codes that start with prefix 978:

Assignment

Define a class ISBN13 that can be used to represent ISBN-13 codes. This class must at least support the following methods:

Example

>>> code = ISBN13(9780136110675)
>>> print(code)
978-0-13611067-5
>>> code
ISBN13(9780136110675, 1)
>>> code.isvalid()
True
>>> code.asISBN10()
'0-13611067-3'

Ask Pythia …

In the following instruction video, Pythia explains how to tackle this assignment. Watch this video as a stepping stone to solve other exercises about object oriented programming1.