- 论坛徽章:
- 1
|
本帖最后由 给个理由先 于 2014-07-15 23:03 编辑
- def isISBNvalid(isbn):
- '''
- To verify a ISBN valid or invalid, return True or False.
- Supports both 10/13 digits.
- '''
- if len(isbn) == 13:
- weight = (1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1)
- return sum([(ord(x)-48)*y for (x,y) in zip(isbn,weight)])%10 == 0
- elif len(isbn) == 10:
- weight = (10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
- return sum([(ord(x)-48)*y for (x,y) in zip(isbn,weight)])%11 == 0
- else:
- return False
- if __name__ == '__main__':
- for (isbn, result) in {'7560924182':True, '7302273608':False, '9771003551004':True}.items():
- assert(isISBNvalid(isbn) == result)
复制代码 验证isbn合法的一小段代码,so easy 都不用解释了
simple and comprehensible semantics
use to use and powerful built-in data structure
more productivity
self documentation
...... |
|