Python: isbn_verifier

This commit is contained in:
Dmitry Kokorin 2021-07-06 17:58:50 +03:00
parent 6475e73ac4
commit c1edf41415
4 changed files with 180 additions and 0 deletions

View 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