Example #1
0
def calculate_control_value(number):
    """
    Calculates the control value for the given
    number.

    @type number: int
    @param number: The number to calculate the control value.
    @rtype: int
    @return: The control value for the given
    number.
    """

    # calculates the number of digits in the number
    number_digits = number_util.get_number_length(number)

    # starts the accumulator
    accumulator = 0

    # iterates over the range of the number of digits
    for index in range(number_digits):
        # retrieves the current digit
        current_digit = number_util.get_digit(number, index)

        # retrieves the current coefficient
        current_coefficient = COEFFICIENT_VALUES[index]

        # increments the accumulator with the current digit
        # multiplied with the current coefficient
        accumulator += current_digit * current_coefficient

    # calculates the partial value from
    # the accumulator value
    partial_value = accumulator % 11

    # in case the partial value is zero
    # or one
    if partial_value in (0, 1):
        # sets the control value as zero
        control_value = 0
    # otherwise
    else:
        # calculates the control value based
        # on the partial value
        control_value = 11 - partial_value

    # returns the control value
    return control_value
Example #2
0
def calculate_control_value(number):
    """
    Calculates the control value for the given
    number.

    @type number: int
    @param number: The number to calculate the control value.
    @rtype: int
    @return: The control value for the given
    number.
    """

    # calculates the number of digits in the number
    number_digits = number_util.get_number_length(number)

    # starts the accumulator
    accumulator = 0

    # iterates over the range of the number of digits
    for index in range(number_digits):
        # in case it's even, sets the multiplier as one
        # otherwise it must be odd and the multiplier
        # should be set as three
        if index % 2: multiplier = 1
        else: multiplier = 3

        # retrieves the current digit
        current_digit = number_util.get_digit(number, index)

        # increments the accumulator with the current digit
        # multiplied with the current multiplier
        accumulator += current_digit * multiplier

    # calculates the partial value from
    # the accumulator value
    partial_value = accumulator % 10

    # calculates the control value based
    # on the partial value
    control_value = 10 - partial_value

    # returns the control value
    return control_value