dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
1.8 KiB
1.8 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69e2383af7832c8032603b8f | Challenge 273: ISBN-13 Validator | 29 | challenge-273 |
--description--
Given a string, determine if it is a valid ISBN-13 number.
A valid ISBN-13:
- Contains only digits and hyphens
- Has exactly 13 digits after removing hyphens
- Passes the following check:
- Multiply each digit by 1 or 3, alternating (multiply the first digit by 1, the second by 3, the third by 1, and so on).
- The sum of the results must be divisible by 10.
--hints--
is_valid_isbn_13("9780306406157") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn_13("9780306406157"), True)`)
}})
is_valid_isbn_13("97803064061570") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn_13("97803064061570"), False)`)
}})
is_valid_isbn_13("978-0-13-595705-9") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn_13("978-0-13-595705-9"), True)`)
}})
is_valid_isbn_13("978-030-64061A-4") should return False.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn_13("978-030-64061A-4"), False)`)
}})
is_valid_isbn_13("9-7-8-0-1-3-4-7-5-7-5-9-9") should return True.
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_isbn_13("9-7-8-0-1-3-4-7-5-7-5-9-9"), True)`)
}})
--seed--
--seed-contents--
def is_valid_isbn_13(s):
return s
--solutions--
def is_valid_isbn_13(s):
digits = s.replace("-", "")
if not digits.isdigit() or len(digits) != 13:
return False
total = sum(int(d) * (1 if i % 2 == 0 else 3) for i, d in enumerate(digits))
return total % 10 == 0