isbn verifier exercise

This commit is contained in:
Xevion
2019-07-14 03:39:33 -05:00
parent 05203204d7
commit 83c3b8a76c
4 changed files with 162 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
def is_valid(isbn):
# Filter out hyphens or any other junk
isbn = [num for num in isbn if num in '0123456789X']
# Check length as well as out of place 'X' chars
if len(isbn) < 10 or len(isbn) > 10 or any([True if (isbn[i] == 'X' and i < 9) else False for i in range(len(isbn))]): return False
# Convert to ints then calculate isbn validity
isbn = [int(digit) if digit != 'X' else 10 for digit in isbn]
return (isbn[0] * 10 + isbn[1] * 9 + isbn[2] * 8 + isbn[3] * 7 + isbn[4] * 6 + isbn[5] * 5 + isbn[6] * 4 + isbn[7] * 3 + isbn[8] * 2 + isbn[9] * 1) % 11 == 0