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.
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:
remove the prefix 978 from the code
remove the check digit from the code
recompute the check digit according to the ISBN-10 rules
Define a class ISBN13 that can be used to represent ISBN-13 codes. This class must at least support the following methods:
An initialization method __init__ that takes an integer (int) that represents an ISBN-13 code. The initialization method must not explicitly check that the argument represents a valid ISBN-13 code. The initialization method also has a second optional parameter that indicates the number of digits (int) in the specification of the country group (default value: 1). In case the given length of the country group is not in the interval [1, 5], the method must raise an AssertionError with the message invalid ISBN code.
A method __str__ that returns a string representation (str) of the ISBN-13 code. This representation should contain dashes in 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.
A method __repr__ that returns a string representation (str) of the object that reads as a Python expression to create a new object having the same state as the current object.
A method isvalid that returns a Boolean value (bool) indicating whether the object represents a valid ISBN-13 code.
A method asISBN10 that returns a string (str) representing the code as an ISBN-10 code. If no such representation exists (because it is not a valid ISBN-13 code or because it does not start with prefix 978), the method must return the value None.
>>> code = ISBN13(9780136110675)
>>> print(code)
978-0-13611067-5
>>> code
ISBN13(9780136110675, 1)
>>> code.isvalid()
True
>>> code.asISBN10()
'0-13611067-3'
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.