Python: isbn_verifier
This commit is contained in:
parent
6475e73ac4
commit
c1edf41415
4 changed files with 180 additions and 0 deletions
29
python/isbn-verifier/isbn_verifier.py
Normal file
29
python/isbn-verifier/isbn_verifier.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
ISBN_LENGTH = 10
|
||||
|
||||
|
||||
def is_valid(isbn):
|
||||
|
||||
isbn = isbn.replace('-', '')
|
||||
if len(isbn) != ISBN_LENGTH:
|
||||
return False
|
||||
|
||||
checksum = 0
|
||||
|
||||
for ch, i in zip(isbn, range(ISBN_LENGTH, 1, -1)):
|
||||
if not ch.isdigit():
|
||||
return False
|
||||
|
||||
checksum += int(ch)*i
|
||||
|
||||
ch = isbn[-1]
|
||||
|
||||
if ch.isdigit():
|
||||
x = int(ch)
|
||||
elif ch == 'X':
|
||||
x = 10
|
||||
else:
|
||||
return False
|
||||
|
||||
checksum += x
|
||||
|
||||
return checksum % 11 == 0
|
||||
Loading…
Add table
Add a link
Reference in a new issue