信用卡验证 - 返回未经过硬编码的值

时间:2017-11-18 17:41:37

标签: python python-2.7 numpy luhn

如何计算答案,以便答案不是硬编码解决方案?如果我想要输入另一个信用卡号码,我该如何退回该新信用卡的结果?另外,我如何创建一个x列表而不是现在的方式,将值拆分为2?

此处还有原始问题供参考:

  

使用Luhn公式3验证信用卡号。实施一个程序,将信用卡号作为多维数组(您可以假设它完全由16列组成),并返回一个值为 Valid <的列表/ em>如果是有效的卡号,则无效

     

进行有效性测试的一种方法如下:
  1.从最右边的数字,即向左移动,向左移动,每秒数字的值加倍;如果这个倍增操作的乘积大于9(例如,8 2 = 16),则将产品的数字相加(例如,16:1 + 6 = 7,18:1 + 8 = 9)。
  2.取所有数字的总和   3.如果总数可被10整除,则该数字根据Luhn公式有效;否则它无效。

     

注意:您可以将卡号作为字符串(使用索引和切片来提取数字等)或整数(使用整数除法和余数来操纵数字)。

我的剧本:

    import numpy as py

    x = [[7,1],[6,3],[4,5],[6,2],[7,8],[3,4],[6,8],[3,9]] #credit card numbers
    x2 = np.array(x)
    evryothernum = x2[:,1]       #returns every other number / every seconds digit
    evryothernum2 = np.multiply(evryothernum,2)
    sumdigits = []

    def card_validate(x):
        evryothernum = x2[:,1]        #valid
        evryothernum2 = np.multiply(evryothernum,2) #multiplys         evryothernum by 2
        b=np.sort(evryothernum2, axis = None) #sorts the evryothernum2 array in order
        b2 = np.array(b)
        b3 = b2[4:8] #shows the highest values aka greater than 9 
        b3
        b3 = [1,7,7,9]
        newb3 = np.sum(b3)
        newx2 = np.sum(x2[:,0])
        total = np.sum(newb3+newx2)
        if ( (total % 10) == 0 ):
            print "Valid"
        else:
            print "Invalid"
        return card_validate()

如果没有Numpy,还有更简单的方法吗?

2 个答案:

答案 0 :(得分:0)

以下内容可用于实现逻辑而不使用numpy

a=[[4,0,1,2,8,8,8,8,8,8,8,8,1,8,8,1],[4,0,1,2,8,8,8,8,8,8,8,8,1,8,8,2]] #array of credit card numbers
def validate(creditcard_numbers,tmp):
    even_index=-1

    for j in range(0,16): 
        sum_val=0
        if even_index%2 ==0:  #index calculation for even indices
            val=creditcard_numbers[even_index]*2 
            if val>9:   # add the digits if the value got after multiplying by 2 is two digit number
                while val !=0:
                    rem=val%10
                    val=val/10
                    sum_val=sum_val+rem

                tmp[even_index]=sum_val  #append the temporary list with the new values which is got by adding the digits if the result of multiplying by 2 is a 2 digit number

            else:
                tmp[even_index]=val

        else:
            tmp[even_index]=creditcard_numbers[even_index]
        even_index=even_index-1

    total=0
    for i in tmp:
        total=total+i
    if total%10 == 0:
        return "valid"
    else:
        return "invalid"


for creditcard_numbers in a:
    print creditcard_numbers
    tmp=[0]*len(creditcard_numbers) #temporary list with zeros's 
    result=validate(creditcard_numbers,tmp)
    print result

**输出: [4,0,1,2,8,8,8,8,8,8,8,8,1,8,8,1]

有效

[4,0,1,2,8,8,8,8,8,8,8,8,1,8,8,2]

无效 **

答案 1 :(得分:0)

这里有几种方法。第一个是关于&#34; Luhn算法的伪代码的Python的直接端口&#34;维基百科页面(link)。第二个使用查找表来计算每个其他数字的预先计算的加倍值:

def checkLuhn(purportedCC):
     sm = 0
     nDigits = len(purportedCC)
     parity = nDigits % 2
     for i in range(nDigits):
         digit = int(purportedCC[i])
         if i % 2 == parity:
             digit = digit * 2
             if digit > 9:
                 digit = digit - 9
         sm = sm + digit
     return (sm % 10) == 0

def check(s):
    lookup = [0,2,4,6,8,1,3,5,7,9]
    digits = [int(c) for c in reversed(s)] # count odd/even from the right
    odd = digits[::2]
    even = digits[1::2]
    total = sum(odd) + sum(lookup[c] for c in even)
    return total % 10 == 0

cards = '1111222233334444','1111222233334445','01111222233334444'

for card in cards:
    print(card,check(card))
    print(card,checkLuhn(card))

输出(注意如果正确完成,则前导零不会影响验证):

1111222233334444 True
1111222233334444 True
1111222233334445 False
1111222233334445 False
01111222233334444 True
01111222233334444 True