Skip to content

[ISBN Verifier] Error in tests that check if the X character is only the verification digit #4014

Closed
exercism/problem-specifications
#2601

Description

@Keftcha

Hi,
During a review, the person ask me if his code check if the X character is only acceptable at the end of the ISBN. So I look at the test and I see this test:

    def test_x_is_only_valid_as_a_check_digit(self):
        self.assertIs(is_valid("3-598-2X507-9"), False)

I think this is the test that test it. But here is the problem I have.
The sum % 11 is not equal to 0.

Here is his solution:

def is_valid(isbn):
    
    isbn = isbn.replace('-', '')
    
    if len(isbn) != 10:
        return False
        
    sum = 0
    for index, digit in enumerate(list(isbn)):
        if digit.isdecimal():
            sum += int(digit) * (10 - index)
        elif digit == 'X':
            sum += 10
        else:
            return False
            
    return sum % 11 == 0 

Here the operation his code is doing:

3 * 10 + 5 * 9 + 9 * 8 + 8 * 7 + 2 * 6 + 10 + 5 * 4 + 0 * 3 + 7 * 2 + 9 * 1 = 268
268 % 11 = 4

As you can see the 10 (replacing the X value) is not multiplied (this is how he write his solution). Because of this his test pass but not for the good reason. It pass because the sum % 11 != 0 and not because X is valid only as a check character.

I think we should add the test case where the ISBN is "3-598-2X507-5". This is a case where the sum is 264 (and 264 % 11 == 0). Doing so will handle that sum % 11 == 0 and the X character is not at the end.

Activity

  1. github-actions commented on Oct 16, 2025

    @github-actions
    Contributor

    Hello. Thanks for opening an issue on Exercism 🙂

    At Exercism we use our Community Forum, not GitHub issues, as the primary place for discussion. That allows maintainers and contributors from across Exercism's ecosystem to discuss your problems/ideas/suggestions without them having to subscribe to hundreds of repositories.

    This issue will be automatically closed. Please use this link to copy your GitHub Issue into a new topic on the forum, where we look forward to chatting with you!

    If you're interested in learning more about this auto-responder, please read this blog post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions