Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I'm doing some exercises in Python and this one is about using the Luhn-algorithm to calculate the checksum digit in Swedish personal identification numbers.

The description is the same as this questionthis question:

Given a string of 9 digits, abcdefghi compute:

array = [a*2, b, c*2, d, e*2, f, g*2, h, i*2]

Then you compute the sum of the digits in this array, so for example if a*2 is 16, then that part counts as 1 + 6

Then the result is how much more you have to add to make it evenly divisible by 10. For example, if sum == 54 then the result is 6 as 60 - 54 = 6.

def control_numb(nbr_string):
    cut_string = nbr_string[:9]  ## We need 9 numbers to calculate the 10th
    mult_numb = [2,1]            ## we should alternate multiplying 2 and 1 starting with 2
    i = 0                        ## holds the index of which mult_numt to use. starting with index 0

    sum_of_digits = 0
    for s in cut_string:         ## loop through the string of digits
        for d in str(int(s) * mult_numb[i]):  ## use s as int to do the multiplication. then turn back to str to loop through digit
            sum_of_digits += int(d)
        i = (i+1)%2

    return (10 - sum_of_digits % 10) % 10

I'm pretty new to Python and would very much like feedback, if there are any pythonic way to do things please spread the knowledge. Or just tear the code apart completely. Any comments are welcome!

I'm doing some exercises in Python and this one is about using the Luhn-algorithm to calculate the checksum digit in Swedish personal identification numbers.

The description is the same as this question:

Given a string of 9 digits, abcdefghi compute:

array = [a*2, b, c*2, d, e*2, f, g*2, h, i*2]

Then you compute the sum of the digits in this array, so for example if a*2 is 16, then that part counts as 1 + 6

Then the result is how much more you have to add to make it evenly divisible by 10. For example, if sum == 54 then the result is 6 as 60 - 54 = 6.

def control_numb(nbr_string):
    cut_string = nbr_string[:9]  ## We need 9 numbers to calculate the 10th
    mult_numb = [2,1]            ## we should alternate multiplying 2 and 1 starting with 2
    i = 0                        ## holds the index of which mult_numt to use. starting with index 0

    sum_of_digits = 0
    for s in cut_string:         ## loop through the string of digits
        for d in str(int(s) * mult_numb[i]):  ## use s as int to do the multiplication. then turn back to str to loop through digit
            sum_of_digits += int(d)
        i = (i+1)%2

    return (10 - sum_of_digits % 10) % 10

I'm pretty new to Python and would very much like feedback, if there are any pythonic way to do things please spread the knowledge. Or just tear the code apart completely. Any comments are welcome!

I'm doing some exercises in Python and this one is about using the Luhn-algorithm to calculate the checksum digit in Swedish personal identification numbers.

The description is the same as this question:

Given a string of 9 digits, abcdefghi compute:

array = [a*2, b, c*2, d, e*2, f, g*2, h, i*2]

Then you compute the sum of the digits in this array, so for example if a*2 is 16, then that part counts as 1 + 6

Then the result is how much more you have to add to make it evenly divisible by 10. For example, if sum == 54 then the result is 6 as 60 - 54 = 6.

def control_numb(nbr_string):
    cut_string = nbr_string[:9]  ## We need 9 numbers to calculate the 10th
    mult_numb = [2,1]            ## we should alternate multiplying 2 and 1 starting with 2
    i = 0                        ## holds the index of which mult_numt to use. starting with index 0

    sum_of_digits = 0
    for s in cut_string:         ## loop through the string of digits
        for d in str(int(s) * mult_numb[i]):  ## use s as int to do the multiplication. then turn back to str to loop through digit
            sum_of_digits += int(d)
        i = (i+1)%2

    return (10 - sum_of_digits % 10) % 10

I'm pretty new to Python and would very much like feedback, if there are any pythonic way to do things please spread the knowledge. Or just tear the code apart completely. Any comments are welcome!

edited tags
Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309
edited tags
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473
added 11 characters in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
Loading
Tweeted twitter.com/#!/StackCodeReview/status/492287835878490112
tiny typos
Source Link
rolfl
  • 97.5k
  • 17
  • 218
  • 418
Loading
added 116 characters in body
Source Link
Simon Forsberg
  • 59.4k
  • 9
  • 156
  • 309
Loading
Source Link
Tejpbit
  • 573
  • 4
  • 13
Loading