Skip to main content
Simplified last line according to comments
Source Link
jcollado
  • 1.4k
  • 7
  • 8

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        quotient, remainder = divmod(number * mult_factor, 10)
        return quotient + remainder

    final_sum = sum(
        partial_sum(int(character), mult_factor)
        for character, mult_factor in zip(swedish_id, mult_factors))

    # Calculate control number based on partial sums
    control_number = (10 - (final_sum % 10)) % 10

    return control_number

I hope this helps.

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        quotient, remainder = divmod(number * mult_factor, 10)
        return quotient + remainder

    final_sum = sum(
        partial_sum(int(character), mult_factor)
        for character, mult_factor in zip(swedish_id, mult_factors))

    # Calculate control number based on partial sums
    control_number = (10 - (final_sum % 10)) % 10

    return control_number

I hope this helps.

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        quotient, remainder = divmod(number * mult_factor, 10)
        return quotient + remainder

    final_sum = sum(
        partial_sum(int(character), mult_factor)
        for character, mult_factor in zip(swedish_id, mult_factors))

    # Calculate control number based on partial sums
    control_number = -final_sum % 10

    return control_number

I hope this helps.

Handle edge case as pointed out by comment
Source Link
jcollado
  • 1.4k
  • 7
  • 8

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        quotient, remainder = divmod(number * mult_factor, 10)
        return quotient + remainder

    final_sum = sum(
        partial_sum(int(character), mult_factor)
        for character, mult_factor in zip(swedish_id, mult_factors))

    # Calculate control number based on partial sums
    control_number = (10 - (final_sum % 10)) % 10

    return control_number

I hope this helps.

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        quotient, remainder = divmod(number * mult_factor, 10)
        return quotient + remainder

    final_sum = sum(
        partial_sum(int(character), mult_factor)
        for character, mult_factor in zip(swedish_id, mult_factors))

    # Calculate control number based on partial sums
    control_number = 10 - (final_sum % 10)

    return control_number

I hope this helps.

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        quotient, remainder = divmod(number * mult_factor, 10)
        return quotient + remainder

    final_sum = sum(
        partial_sum(int(character), mult_factor)
        for character, mult_factor in zip(swedish_id, mult_factors))

    # Calculate control number based on partial sums
    control_number = (10 - (final_sum % 10)) % 10

    return control_number

I hope this helps.

Updated after Josay feedback
Source Link
jcollado
  • 1.4k
  • 7
  • 8

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    # Map string to one-digit numbers
    numbers = [int(character) for character in swedish_id]

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        totalquotient, remainder = divmod(number * mult_factor, 10)
        return (total / 10quotient + total % 10)remainder

    partial_sumsfinal_sum = [partial_sumsum(number, mult_factor)
           partial_sum(int(character), mult_factor)
        for numbercharacter, mult_factor in zip(numbersswedish_id, mult_factors)])

    # Calculate control number based on partial sums
    control_number = 10 - (sum(partial_sums)final_sum % 10) 

    return control_number

I hope this helps.

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    # Map string to one-digit numbers
    numbers = [int(character) for character in swedish_id]

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        total = number * mult_factor
        return (total / 10 + total % 10)

    partial_sums = [partial_sum(number, mult_factor)
                    for number, mult_factor in zip(numbers, mult_factors)]

    # Calculate control number based on partial sums
    control_number = 10 - (sum(partial_sums) % 10)
    return control_number

I hope this helps.

Some comments:

  • Variable names could be improved a little bit. For example, I think nbr_string could be better as swedish_id
  • Please add a docstring (PEP257)
  • itertools.cycle is useful to iterate multiple times over the same sequence (in this case, the multiplication factors)
  • Use list comprehensions where possible instead of for loops

Things that might be different from the question's code:

  • I'm assuming the initial string has 9 digits, but if it has already 10, then go with your code to keep only the first 9 numbers, but note that you can use some assertions to be sure the data passed is correct.
  • I've calculated partial sums to apply the algorithm as in the explanation, but doing the sums as in the question should be fine.

Please find my solution based on your code below:

from itertools import cycle


def control_number(swedish_id):
    """Calculate control number in Swedish personal IDs

    :param swedish_id: A Swedish personal ID
    :type swedish_id: str

    """
    assert swedish_id.isdigit()
    assert len(swedish_id) == 9

    # Multiplication factors for each digit depending on its position
    mult_factors = cycle([2, 1])

    def partial_sum(number, mult_factor):
        """Calculate partial sum ofr a single digit."""
        quotient, remainder = divmod(number * mult_factor, 10)
        return quotient + remainder

    final_sum = sum(
        partial_sum(int(character), mult_factor)
        for character, mult_factor in zip(swedish_id, mult_factors))

    # Calculate control number based on partial sums
    control_number = 10 - (final_sum % 10) 

    return control_number

I hope this helps.

corrected some small typo
Source Link
Marc-Andre
  • 6.7k
  • 5
  • 38
  • 65
Loading
Source Link
jcollado
  • 1.4k
  • 7
  • 8
Loading