From 3f20c0dada6b30fa200ab128258ee9b161d0e3fc Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Wed, 3 Sep 2025 22:15:21 -0700 Subject: [PATCH 1/3] Update isbn_verifier.py --- isbn-verifier/isbn_verifier.py | 42 +++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/isbn-verifier/isbn_verifier.py b/isbn-verifier/isbn_verifier.py index e09831f..889bbdb 100644 --- a/isbn-verifier/isbn_verifier.py +++ b/isbn-verifier/isbn_verifier.py @@ -16,8 +16,10 @@ If the result is 0, then it is a valid ISBN-10, otherwise it is invalid. """ +ISBN: str = "0123456789X" -def calc_formula(digits: str) -> int: + +def calc_formula(digits: list[int]) -> int: """ Calculate the weighted sum for ISBN-10 validation formula. @@ -26,13 +28,26 @@ def calc_formula(digits: str) -> int: """ result: int = 0 for i, digit in enumerate(digits): - if digit.isdigit(): - result += int(digit) * (10 - i) - elif digit.lower() == "x": - result += 10 * 1 - else: - # Invalid char, calc failed - return 1 + result += digit * (10 - i) + return result + + +def formated_isbn(isbn: str) -> list[int]: + """ + Extract and format ISBN digits from input string. + + Converts ISBN string to list of integers, treating 'X' as 10. + Ignores non-digit characters except for trailing 'X'. + + :param isbn: ISBN string that may contain digits, hyphens, and trailing 'X' + :return: List of integers representing ISBN digits, with 'X' converted to 10 + """ + result: list[int] = [] + for char in isbn: + if char.isdigit(): + result.append(int(char)) + if isbn[-1].lower() == "x": + result.append(10) return result @@ -43,8 +58,13 @@ def is_valid(isbn: str) -> bool: :param isbn: 9 digits ISBN 10 format :return: Tru if isbn is valid, False otherwise """ - formated_isbn: str = isbn.replace("-", "") - if len(formated_isbn) != 10: + # Non ISBN chars or empty strings not allowed + if not all(char in ISBN for char in isbn.replace("-", "")) or not isbn: + return False + # Convert all isbn chars to digits + isbn_digits: list[int] = formated_isbn(isbn) + # ISBN total length should be = 10 + if len(isbn_digits) != 10: return False - return calc_formula(formated_isbn) % 11 == 0 + return calc_formula(isbn_digits) % 11 == 0 From 9c8b3f793cfb5dfe682a16d5609d77da67819695 Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Wed, 3 Sep 2025 22:17:20 -0700 Subject: [PATCH 2/3] Update isbn_verifier.py --- isbn-verifier/isbn_verifier.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/isbn-verifier/isbn_verifier.py b/isbn-verifier/isbn_verifier.py index 889bbdb..6cb0de7 100644 --- a/isbn-verifier/isbn_verifier.py +++ b/isbn-verifier/isbn_verifier.py @@ -32,7 +32,7 @@ def calc_formula(digits: list[int]) -> int: return result -def formated_isbn(isbn: str) -> list[int]: +def formatted_isbn(isbn: str) -> list[int]: """ Extract and format ISBN digits from input string. @@ -62,7 +62,7 @@ def is_valid(isbn: str) -> bool: if not all(char in ISBN for char in isbn.replace("-", "")) or not isbn: return False # Convert all isbn chars to digits - isbn_digits: list[int] = formated_isbn(isbn) + isbn_digits: list[int] = formatted_isbn(isbn) # ISBN total length should be = 10 if len(isbn_digits) != 10: return False From 6ff34f97fd39b30ef892edaead639ed7e3372d8e Mon Sep 17 00:00:00 2001 From: Egor Kostan Date: Wed, 3 Sep 2025 22:20:09 -0700 Subject: [PATCH 3/3] Update isbn_verifier.py --- isbn-verifier/isbn_verifier.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isbn-verifier/isbn_verifier.py b/isbn-verifier/isbn_verifier.py index 6cb0de7..20d26b3 100644 --- a/isbn-verifier/isbn_verifier.py +++ b/isbn-verifier/isbn_verifier.py @@ -40,7 +40,7 @@ def formatted_isbn(isbn: str) -> list[int]: Ignores non-digit characters except for trailing 'X'. :param isbn: ISBN string that may contain digits, hyphens, and trailing 'X' - :return: List of integers representing ISBN digits, with 'X' converted to 10 + :return: List of integers representing ISBN digits, 'X' converted to 10 """ result: list[int] = [] for char in isbn: