PROG0182 - ISBN

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 calculated 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 calculated as $$x_{13} = (10 - (x_1 + x_3 + x_5 + x_7 + x_9 + x_{11} + 3(x_2 + x_4 + x_6 + x_8 + x_{10} + x_{12}))\!\!\!\!\!\mod{10})\!\!\!\!\!\mod{10}$$ As a result, $x_{13}$ always takes a value in between 0 and 9, so that ISBN-13 codes only contain digits.

Assignment

  1. Write a function
    isISBN(code[, isbn13])
    

    that takes a string and an optional second argument. The function must determine whether or not the first argument is a valid ISBN code. The second argument indicates whether the function should check validity for ISBN-13 or ISBN-10 codes. This argument takes Boolean value, which is True if the function needs to check for ISBN-13 codes. This is also the default value for the second parameter. See the example below to figure out how the function needs to be used.

  2. Write a function
    areISBN(codes[, isbn13])
    

    that determines for all codes in a given list whether or not they are valid ISBN codes. This function takes a list of codes as its first argument. An optional second argument can be passed to the function. If this argument has the value True, the function must check whether all codes in the given list are valid ISBN-13 codes. If the optional argument is False, the function must check whether all codes in the given list are valid ISBN-10 codes. If the optional argument has the value None (its default value), the type of each code in the given list should be derived from its length as a string. Any code that is not a string is a priori considered to be an invalid ISBN code. Codes having length 13 should be checked as ISBN-13 codes and codes of having length 10 should be checked as ISBN-10 codes. Codes having a length that differs from 10 and 13 are a priori considered to be invalid ISBN codes. The function must return a new list that only contains the values True and False, indicating whether or not the code at the corresponding position in the given list of codes is a valid ISBN code.

Example

>>> isISBN('9789027439642', False)
False
>>> isISBN('9789027439642', True)
True
>>> isISBN('9789027439642')
True
>>> isISBN('080442957X')
False
>>> isISBN('080442957X', False)
True

>>> areISBN(
...     [
...         '0012345678', '0012345679', '9971502100', '080442957X',
...         5, True, 'The Practice of Computing Using Python',
...         '9789027439642', '5486948320146'
...     ]
... )
[False, True, True, True, False, False, False, True, False]

>>> areISBN(
...     [
...         '0012345678', '0012345679', '9971502100', '080442957X', 
...         5, True, 'The Practice of Computing Using Python', 
...         '9789027439642', '5486948320146'
...     ],
...     True
... )
[False, False, False, False, False, False, False, True, False]

>>> areISBN(
...     [
...         '0012345678', '0012345679', '9971502100', '080442957X', 
...         5, True, 'The Practice of Computing Using Python', 
...         '9789027439642', '5486948320146'
...     ],
...     False
... )
[False, True, True, True, False, False, False, False, False]

Binnen het ISBN-10 (International Standard Book Numbering) systeem dat tot eind 2006 gebruikt werd, kreeg elk boek een unieke code toegewezen die bestaat uit 10 cijfers. De eerste 9 daarvan geven informatie over het boek zelf, terwijl het laatste louter een controlecijfer is dat dient om foutieve ISBN-10 codes te detecteren.

ISBN
ISBN in tekst en streepjescode

Indien $x_1, \ldots, x_9$ de eerste 9 cijfers van een ISBN-10 code voorstellen, dan wordt het controlecijfer $x_{10}$ als volgt berekend: $$x_{10} = (x_1+ 2x_2+ 3x_3+ 4x_4+ 5x_5+ 6x_6+ 7x_7+ 8x_8+ 9x_9)\!\!\!\!\mod{11}\,$$ $x_{10}$ kan m.a.w. de waarden 0 tot en met 10 aannemen. Indien het controlecijfer gelijk is aan 10, dan wordt dit in de ISBN-code genoteerd als de letter X.

Binnen het nieuwe ISBN-13 systeem krijgt elk boek een unieke code bestaande uit 13 cijfers. De eerste 12 daarvan geven informatie over het boek zelf, terwijl het laatste louter een controlecijfer is dat dient om foutieve ISBN-13 codes te detecteren. Indien $x_1, \ldots, x_{12}$ de eerste 12 cijfers van een ISBN-13 code voorstellen, dan wordt het controlecijfer $x_{13}$ als volgt berekend: $$x_{13} = (10 - (x_1 + x_3 + x_5 + x_7 + x_9 + x_{11} + 3(x_2 + x_4 + x_6 + x_8 + x_{10} + x_{12}))\!\!\!\!\!\mod{10})\!\!\!\!\!\mod{10}$$ $x_{13}$ kan m.a.w. de waarden 0 tot en met 9 aannemen, waardoor ISBN-13 codes uitsluitend uit cijfers bestaan.

Opgave

  1. Schrijf een functie
    isISBN(code[, isbn13])
    

    waaraan een string en een optioneel tweede argument kunnen doorgegeven worden. De functie moet bepalen of het eerste argument een geldige ISBN-code is. Het tweede argument geeft aan of het om een ISBN-10 of een ISBN-13 code gaat. Dit argument is een Booleaanse waarde en is True als het om een ISBN-13 code gaat. Dit is meteen ook de standaardwaarde voor deze parameter. Hieronder vind je een voorbeeld van hoe deze functie moet kunnen gebruikt worden.

  2. Schrijf een functie
    zijnISBN(codes[, isbn13])
    

    die voor alle codes uit een gegeven lijst bepaalt of het al dan niet geldigde ISBN codes zijn. Aan deze functie moet verplicht een lijst van codes als eerste argument doorgegeven worden. Optioneel kan ook een tweede argument doorgeven worden. Indien dit argument de waarde True heeft, dan moet de functie nagaan of alle codes uit de lijst geldige ISBN-13 codes zijn. Indien het optionele argument de waarde False heeft, dan moet de functie nagaan of alle codes uit de lijst geldige ISBN-10 codes zijn. Indien het optionele argument de waarde None heeft (de standaardwaarde), dan wordt het type van elke code uit de lijst bepaald op basis van de lengte van de code. Indien een code geen string is, dan wordt die a priori als ongeldig bestempeld. Voor codes van lengte 13 moet getest worden of het geldige ISBN-13 codes zijn, en voor codes van lengte 10 of het geldige ISBN-10 codes zijn. Codes met afwijkende lengtes (geen 10 en geen 13) worden ook a priori als ongeldige ISBN-codes bestempeld. De functie moet als resultaat een nieuwe lijst teruggeven, die enkel de waarden True of False bevat, naargelang de code op de corresponderende positie in de gegeven lijst van codes een geldige ISBN-code is of niet.

Voorbeeld

>>> isISBN('9789027439642', False)
False
>>> isISBN('9789027439642', True)
True
>>> isISBN('9789027439642')
True
>>> isISBN('080442957X')
False
>>> isISBN('080442957X', False)
True

>>> zijnISBN(
...     [
...         '0012345678', '0012345679', '9971502100', '080442957X',
...         5, True, 'The Practice of Computing Using Python',
...         '9789027439642', '5486948320146'
...     ]
... )
[False, True, True, True, False, False, False, True, False]

>>> zijnISBN(
...     [
...         '0012345678', '0012345679', '9971502100', '080442957X',
...         5, True, 'The Practice of Computing Using Python',
...         '9789027439642', '5486948320146'
...     ],
...     True
... )
[False, False, False, False, False, False, False, True, False]

>>> zijnISBN(
...     [
...         '0012345678', '0012345679', '9971502100', '080442957X',
...         5, True, 'The Practice of Computing Using Python',
...         '9789027439642', '5486948320146'
...     ],
...     False
... )
[False, True, True, True, False, False, False, False, False]

Added by:Peter Dawyndt
Date:2011-11-02
Time limit:5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:PY_NBC
Resource:None

© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.