isbn-10 validity identifier
September 1st, 2008 by webstersprodigyWellp, here’s a program that does the isbn error checking
From my class – a tip to being a better programmer
- Specific to our program?
- Mathematical assignments – assumed pencil and paper
-
- Algorithms
- Theory of Computation
- Cryptography
- Code these assignments if possible
- A language like python would be perfect because of the available libraries eg numpy
- Usually takes a bit more effort for small numbers of problems, but I usually understand both the Math better and become a better programmer
Here’s one of these assignments coded. This was part of a coding theory assignment (MATH599).
#!/usr/bin/env python
#this program checks if isbn-10 numbers are valid
import sys
def usage():
"You're using it wrong!"
try:
isbn = sys.argv[1].replace('-','')
except:
usage()
isbnNum = isbn[0:9]
checksumPassed = int(isbn[9:])
sum = 0
index = 10
for i in isbnNum:
sum += int(i)* index
index -= 1
print "sum is ", sum
checksum = 11 - (sum % 11)
print 11 - checksum
if checksum == checksumPassed:
print "isbn checksum is fine"
else:
print "isbn checksum error"
Tags: python