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. A number (int) must be passed when creating an ISBN-13 code (ISBN13). At the time the ISBN-13 code (ISBN13) is created, the class must still assume that this number represents a valid ISBN-13 code, without explicitly checking. An optional second argument can be passed that specifies the number of digits (int) in the specification of the country group (default value: 1) of the ISBN-13 code. If the specified length of the country group is not in the interval $$[1, 5]$$, an AssertionError should be raised with the message invalid ISBN code.

If an ISBN-13 code $$c$$ (ISBN13) is passed to the built-in function str, a string (str) representating ISBN-13 code $$c$$ must be returned. This representation should contain hyphens between the information fields of the ISBN-13 code: the prefix 978 or 979, the specification of the country group, the identification of the publisher and the item, and the check digit.

If an ISBN-13 code $$c$$ (ISBN13) is passed to the built-in function repr, a string (str) must be returned that reads as a Python expression to create ISBN-13 code $$c$$. The expression must explicitly pass the length of the country group as a second (positional) argument.

On an ISBN-13 code $$c$$ (ISBN13), you should be able to call at least 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.