2017-11-18 7 views
0

答えはハードコードされたソリューションではないように計算するにはどうすればよいですか?他のクレジットカード番号を入力したいのであれば、その新しいクレジットカードの結果をどのように返すことができますか?また、私は1つのxリストを作成する方法ではなく、2で分割された値を今持っている方法で作成することができますか?クレジットカードの検証 - ハードコードされていない値を返す

はまたここに参照のために元の質問です:多次元配列としてクレジットカード番号を取るプログラムを実装するのLuhnの式3を用い

クレジットカード番号が検証され(あなたはそれが正確で構成されて取ることができます値がのリストを返します。有効なカード番号であれば、それ以外の場合は無効な場合はが無効です。

有効性テストを実行する1つの方法は、次のとおりです。
1.チェック桁である右端の桁から、左に移動し、2桁目の値を2倍します。この2倍演算の積が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のせずにこれを行うための簡単な方法はありますか?

+1

あなたがしているのいくつかの質問をする。それを分割し、関連するコード行でそれぞれに焦点を合わせます。小さな部品にはあなたの完全なソリューションは必要ありません – Gibolt

+0

また、numpyはLuhn'sを実装するための大過剰の過剰です。 –

+0

これはnumpyなしでこれを行うより簡単な方法ですか? – derickim

答えて

0

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 

をnumpyの使用せずにロジックを実装するために使用することができ、以下** OUTPUT: [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]

無効 **

0

ここにいくつかの方法があります。最初は、 "Luhnアルゴリズム" Wikipediaページ(link)の擬似コードのPythonへのストレートポートです。第二には、事前に計算してルックアップテーブルを使用して他のすべての桁の値が倍増:

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 
関連する問題