0

This is a homework assignment that I've been working on to compute if a credit card number is valid. It has many steps and uses 2 other helper functions.

The first helper function makes a list consisting of each digit in n:

def intToList(n):
    strr = [num for num in str(n)]
    theList = list(map(int, strr))
    return theList

The second helper function adds the sum of digits in a number. For example:

def addDigits(n):
    sums = 0
    while n:
        if n > 0:        
            sums += n % 10
            n //= 10
        else:
            return
    return sums

>>>(332) #(3+3+2) = 7
>>> 7

So the function I am working on is suppose to validate a 16 digit credit card number. It has specific orders to follow in the order given.

  1. Verifies that it contains only digits. #Done.
  2. Verifies that it is 16 digits long. #Done.
  3. if n is a string, it converts it to an integer.
  4. creates a list using the function intToList(n).
  5. Multiplies the odd indices of the list made by intToList(n) by 2 and any products that produce two-digit numbers are replaced by the sum of the digits using the function addDigits(n).
  6. Computes the sum of all the single digits in the list made my intToList(n). If the sum is equal to 0 modulo 10, the original value, n, is a valid credit card number.

As of right now I have this:

def checkCreditCard(n):
    #Suppose to convert n to int.
    n = int(n)
    #Helper function 1 to make a list.
    myList = intToList(n)
    #For loop to apply the math to each odd indices.*
    for ele in myList:
        if ele % 2 == 1:
            ele *= 2
        if ele >= 10:
            single = addDigits(?) #not sure what to put I've tried everything
        if sum(myList) % 10 == 0:
            return True
        return False

Here is my issue, I am unsure where to go from here. I am pretty sure the code above is correct so far, but I don't know how to make the products that produce two-digit numbers compute to single digit ones using my function and computes the sum of all the single digits in the list.

Any help would be greatly appreciated. Let me know if I can clear anything up.

added what I've worked on.

thatoneguy
  • 73
  • 6

1 Answers1

1

Simple trick: The sum of the digits of all numbers from 10 to 18 (the possible two digit values for doubling or adding single digit values) can be computed simply by subtracting 9. So if you have a possible single, possibly double digit value, you can use it as a single digit with:

singledigit = maybetwodigit - 9 * (maybetwodigit >= 10)

For the record, your code as written is not correct:

def checkCreditCard(n):
    #My checks for length and digits.
    if len(str(n)) == 16 and str(n).isdigit():
        return True
    else:
        return False
    # nothing at this line or below will ever execute, because both your if
    # and else conditions return

Also, your (currently unused) loop will never work, because you don't assign what you've calculated. You probably want something like this:

for i, ele in enumerate(myList):
    if i % 2 == 1:
        ele *= 2
        myList[i] = ele - 9 * (ele >= 10)  # Seamlessly sum digits of two digit nums
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • The issue is is that I have to use the helper function addDigits(n) to make any possible two-digit values into a single digit value. such as 8*2 = 16 after the addDigits(n) function goes through it should take 16 and make it 7. – thatoneguy Nov 03 '16 at 03:22
  • @thatoneguy: Yup. This is a shortcut to avoid needing to actually add digits. If you have 16, subtracting 9 gets you 7 (the sum of 1 and 6). 17 - 9 = 8 (sum of 1 and 7), etc. – ShadowRanger Nov 03 '16 at 03:28
  • As part of the assignment I have to use the helper function. I would love to use the shortcut, but my assignment wants me to use the function. – thatoneguy Nov 03 '16 at 03:30
  • @thatoneguy: Well, you can put the shortcut in the helper function. I just mentioned it because your question wanted a nicer way to do it. – ShadowRanger Nov 03 '16 at 03:33
  • Here is my issue, when calculating 9867153285310976 it equals 80. Whereas it is suppose to be 67 if the addDigits(n) function is used. Which would be false yes, but the addDigits function has to iterate through the numbers and find the ones that are more than a single digit after their multiplication. Thats what I am having an issue with. It completely ignores the whole if statements other than my sum(myList) % 10 == 0 – thatoneguy Nov 03 '16 at 03:41
  • I am slowly editing my post to reflect some changes. – thatoneguy Nov 03 '16 at 03:50