"""
NOTE: Haskell code
import Char(digitToInt)
calcCheckDigit :: [Int] -> Int
calcCheckDigit = (`mod` 10) . (10 -) . sum . zipWith (*) (cycle [1,3])
isValidISBN13 :: [Char] -> Bool
isValidISBN13 isbn =
if (length isbn) /= 13 then
False
else
let digits = (map digitToInt isbn) in
(==) (last digits) (calcCheckDigit (take 12 digits))
"""
def is_valid_isbn13(isbn):
from string import digits
from operator import mul
from itertools import cycle, starmap, izip
assert isinstance(isbn, str)
assert len(isbn) == 13
assert all(c for c in isbn if c in digits) # all(map(digits.__contains__, isbm))
return int(isbn[-1]) == (10-sum(starmap(mul,izip(map(int,isbn[:-1]),cycle([1,3])))))%10
if __name__ == '__main__':
print is_valid_isbn13("9784062772211")
print is_valid_isbn13("9784150315684")
IiIiCgpOT1RFOiBIYXNrZWxsIGNvZGUKCmltcG9ydCBDaGFyKGRpZ2l0VG9JbnQpCgpjYWxjQ2hlY2tEaWdpdCA6OiBbSW50XSAtPiBJbnQKY2FsY0NoZWNrRGlnaXQgPSAoYG1vZGAgMTApIC4gKDEwIC0pIC4gc3VtIC4gemlwV2l0aCAoKikgKGN5Y2xlIFsxLDNdKQoKaXNWYWxpZElTQk4xMyA6OiBbQ2hhcl0gLT4gQm9vbAppc1ZhbGlkSVNCTjEzIGlzYm4gPQogIGlmIChsZW5ndGggaXNibikgLz0gMTMgdGhlbgogICAgRmFsc2UKICBlbHNlCiAgICBsZXQgZGlnaXRzID0gKG1hcCBkaWdpdFRvSW50IGlzYm4pIGluCiAgICAgICg9PSkgKGxhc3QgZGlnaXRzKSAoY2FsY0NoZWNrRGlnaXQgKHRha2UgMTIgZGlnaXRzKSkKCiIiIgoKZGVmIGlzX3ZhbGlkX2lzYm4xMyhpc2JuKToKICAgIGZyb20gc3RyaW5nIGltcG9ydCBkaWdpdHMKICAgIGZyb20gb3BlcmF0b3IgaW1wb3J0IG11bAogICAgZnJvbSBpdGVydG9vbHMgaW1wb3J0IGN5Y2xlLCBzdGFybWFwLCBpemlwCg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