curriculum/challenges/english/blocks/daily-coding-challenges-python/69b1028d6e265413d0198a2a.md
Given a string, determine if it's a valid ISBN-10.
An ISBN-10 consists of hyphens ("-") and 10 other characters. After removing the hyphens ("-"):
"X", which represents the number 10.To validate it:
is_valid_isbn10("0-306-40615-2") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-306-40615-2"), True)`)
}})
is_valid_isbn10("0-306-40615-1") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-306-40615-1"), False)`)
}})
is_valid_isbn10("0-8044-2957-X") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-8044-2957-X"), True)`)
}})
is_valid_isbn10("X-306-40615-2") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("X-306-40615-2"), False)`)
}})
is_valid_isbn10("0-6822-2589-4") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn10("0-6822-2589-4"), True)`)
}})
def is_valid_isbn10(s):
return s
def is_valid_isbn10(s):
isbn = s.replace("-", "")
if len(isbn) != 10:
return False
total = 0
for i in range(9):
if not isbn[i].isdigit():
return False
total += int(isbn[i]) * (i + 1)
last = isbn[9]
if last == "X":
total += 10 * 10
elif last.isdigit():
total += int(last) * 10
else:
return False
return total % 11 == 0