Skip to main content
edited body
Source Link
AlexV
  • 7.4k
  • 2
  • 24
  • 47

I've not had much exposure with good tests. So this focuses on the first codeblock.

  1. I think *_TABLE isn't that useful. Instead PERMUATTIONSPERMUTATIONS and INVERSE looks nicer to me.
  2. Given that calculate and validate are almost duplicate functions you should probably define a private helper to handle the common code.
class VarhoeffVerhoeff:
    ...
    
    @classmethod
    def _find_check_digit(cls, digits):
        check_digit = 0
        for i, digit in digits:
            col_idx = cls.PERMUTATIONS[i % 8][int(digit)]
            check_digit = cls.MULTIPLICATIONS[check_digit][col_idx]
        return check_digit

    @classmethod
    def calculate(cls, input_: str) -> str:
        """Calculate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_), 1))
        return str(cls.INVERSES[check_digit])

    @classmethod
    def validate(cls, input_: str) -> bool:
        """Validate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_)))
        return cls.INVERSES[check_digit] == 0

I've not had much exposure with good tests. So this focuses on the first codeblock.

  1. I think *_TABLE isn't that useful. Instead PERMUATTIONS and INVERSE looks nicer to me.
  2. Given that calculate and validate are almost duplicate functions you should probably define a private helper to handle the common code.
class Varhoeff:
    ...
    
    @classmethod
    def _find_check_digit(cls, digits):
        check_digit = 0
        for i, digit in digits:
            col_idx = cls.PERMUTATIONS[i % 8][int(digit)]
            check_digit = cls.MULTIPLICATIONS[check_digit][col_idx]
        return check_digit

    @classmethod
    def calculate(cls, input_: str) -> str:
        """Calculate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_), 1))
        return str(cls.INVERSES[check_digit])

    @classmethod
    def validate(cls, input_: str) -> bool:
        """Validate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_)))
        return cls.INVERSES[check_digit] == 0

I've not had much exposure with good tests. So this focuses on the first codeblock.

  1. I think *_TABLE isn't that useful. Instead PERMUTATIONS and INVERSE looks nicer to me.
  2. Given that calculate and validate are almost duplicate functions you should probably define a private helper to handle the common code.
class Verhoeff:
    ...
    
    @classmethod
    def _find_check_digit(cls, digits):
        check_digit = 0
        for i, digit in digits:
            col_idx = cls.PERMUTATIONS[i % 8][int(digit)]
            check_digit = cls.MULTIPLICATIONS[check_digit][col_idx]
        return check_digit

    @classmethod
    def calculate(cls, input_: str) -> str:
        """Calculate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_), 1))
        return str(cls.INVERSES[check_digit])

    @classmethod
    def validate(cls, input_: str) -> bool:
        """Validate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_)))
        return cls.INVERSES[check_digit] == 0
Source Link
Peilonrayz
  • 44.5k
  • 7
  • 80
  • 158

I've not had much exposure with good tests. So this focuses on the first codeblock.

  1. I think *_TABLE isn't that useful. Instead PERMUATTIONS and INVERSE looks nicer to me.
  2. Given that calculate and validate are almost duplicate functions you should probably define a private helper to handle the common code.
class Varhoeff:
    ...
    
    @classmethod
    def _find_check_digit(cls, digits):
        check_digit = 0
        for i, digit in digits:
            col_idx = cls.PERMUTATIONS[i % 8][int(digit)]
            check_digit = cls.MULTIPLICATIONS[check_digit][col_idx]
        return check_digit

    @classmethod
    def calculate(cls, input_: str) -> str:
        """Calculate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_), 1))
        return str(cls.INVERSES[check_digit])

    @classmethod
    def validate(cls, input_: str) -> bool:
        """Validate the check digit using Verhoeff's algorithm"""
        check_digit = cls._find_check_digit(enumerate(reversed(input_)))
        return cls.INVERSES[check_digit] == 0