luhn algorithm python

Solutions on MaxInterview for luhn algorithm python by the best coders in the world

showing results for - "luhn algorithm python"
Brice
26 Feb 2020
1#Worse
2#This is the longer Luhn's algorithm with step-by-step instructions
3
4#Function to split a string into a list of all its constituent characters
5def split_char(number_string):
6    return [char for char in number_string]
7
8#Function to convert a string list to an integer list
9def list_str_to_int(test_list):
10    for i in range(0, len(test_list)):
11        test_list[i] = int(test_list[i])
12    return test_list
13
14#Function to convert an integer list to a string list
15def list_int_to_str(test_list):
16    for i in range(0, len(test_list)):
17        test_list[i] = str(test_list[i])
18    return test_list
19
20#Function to multiply all elements of a list by 2
21def list_multiplied_by_2(test_list):
22    for i in range(0, len(test_list)):
23        test_list[i] *= 2
24    return test_list
25
26#Prompt the user for Input
27ccn = int(input("Enter your digital card number: "))
28
29ccn = str(ccn)
30
31ccn_string = str(ccn)
32
33length_var = len(ccn)
34
35#Specifying a condition to exit the loop
36if 12 > length_var < 17:
37    print("INVALID")
38    exit()
39
40# Condition for digital card number with even length
41elif length_var % 2 == 0:
42    even_num1 = ccn
43    even_num2 = " " + ccn
44
45    even_num3 = "".join(even_num1[::2])
46    even_num4 = "".join(even_num2[::2])
47
48    even_alternate1 = even_num3.replace(" ", "")
49    even_alternate2 = even_num4.replace(" ", "")
50
51    even_sep1 = split_char(even_alternate1)
52    even_sep2 = split_char(even_alternate2)
53
54    even_str_to_int1 = list_str_to_int(even_sep1)
55    even_str_to_int2 = list_str_to_int(even_sep2)
56
57    even_str_2_1 = list_multiplied_by_2(even_str_to_int1)
58
59    even_int_to_str1 = list_int_to_str(even_str_2_1)
60    even_sep_digits1 = ""
61    for i in even_int_to_str1:
62        even_sep_digits1 += "".join(i)
63
64    even_sep3 = split_char(even_sep_digits1)
65
66    even_str_to_int3 = list_str_to_int(even_sep3)
67    even_sum_last_add1 = sum(even_str_to_int3)
68
69    even_sum_last_add2 = sum(even_str_to_int2)
70
71    even_checksum = even_sum_last_add1 + even_sum_last_add2
72
73    #Printing the type of digital card based on user input
74    if even_checksum % 10 == 0:
75        if len(ccn_string) == 14 or 16 and ccn_string[:1] == "4":
76            print("VISA")
77        elif len(ccn_string) == 16 and ccn_string[:2] == "51" or "52" or "53" or "54" or "55":
78            print("MASTERCARD")
79        else:
80            print("INVALID")
81    else:
82        print("INVALID")
83
84# Condition for digital card number with odd length
85elif length_var % 2 == 1:
86    odd_num1 = " " + ccn
87    odd_num2 = ccn
88
89    odd_num3 = "".join(odd_num1[::2])
90    odd_num4 = "".join(odd_num2[::2])
91
92    odd_alternate1 = odd_num3.replace(" ", "")
93    odd_alternate2 = odd_num4.replace(" ", "")
94
95    odd_sep1 = split_char(odd_alternate1)
96    odd_sep2 = split_char(odd_alternate2)
97
98    odd_str_to_int1 = list_str_to_int(odd_sep1)
99    odd_str_to_int2 = list_str_to_int(odd_sep2)
100
101    odd_str_2_1 = list_multiplied_by_2(odd_str_to_int1)
102
103    odd_int_to_str1 = list_int_to_str(odd_str_2_1)
104    odd_sep_digits1 = ""
105    for i in odd_int_to_str1:
106        odd_sep_digits1 += "".join(i)
107
108    odd_sep3 = split_char(odd_sep_digits1)
109
110    odd_str_to_int3 = list_str_to_int(odd_sep3)
111    odd_sum_last_add1 = sum(odd_str_to_int3)
112
113    odd_sum_last_add2 = sum(odd_str_to_int2)
114
115    odd_checksum = odd_sum_last_add1 + odd_sum_last_add2
116
117    #Printing the type of digital card based on user input
118    if odd_checksum % 10 == 0:
119        if ccn_string[:2] == "34" or "37" and len(ccn_string) == 15:
120            print("AMEX")
121        elif len(ccn_string) == 13 or 15 and ccn_string[:1] == "4":
122            print("VISA")
123        else:
124            print("INVALID")
125    else:
126        print("INVALID")
Giorgio
03 Feb 2017
1$ pip install fast-luhn
2
Manuel
22 Sep 2016
1#Better
2#This is the shorter Luhn's algorithm with step-by-step instructions
3
4#Function to split a string into a list of all its constituent characters
5def split_char(number_string):
6    return [char for char in number_string]
7
8#Function to convert a string list to an integer list
9def list_str_to_int(test_list):
10    for i in range(0, len(test_list)):
11        test_list[i] = int(test_list[i])
12    return test_list
13
14#Function to convert an integer list to a string list
15def list_int_to_str(test_list):
16    for i in range(0, len(test_list)):
17        test_list[i] = str(test_list[i])
18    return test_list
19
20#Function to multiply all elements of a list by 2
21def list_multiplied_by_2(test_list):
22    for i in range(0, len(test_list)):
23        test_list[i] *= 2
24    return test_list
25
26#Prompt the user for Input
27ccn = int(input("Enter your digital card number: "))
28
29ccn = str(ccn)
30
31#Specifying a condition to exit the loop
32if 12 > len(ccn) < 17:
33    print("INVALID")
34    exit()
35
36num1 = " " + "".join(ccn[::-1])
37num2 = "".join(ccn[::-1])
38num3 = "".join(num1[::2])
39num4 = "".join(num2[::2])
40
41alternate_num3 = num3.replace(" ", "")
42
43sep1 = split_char(alternate_num3)
44sep2 = split_char(num4)
45
46str_to_int1 = list_str_to_int(sep1)
47str_to_int2 = list_str_to_int(sep2)
48
49str_2_1 = list_multiplied_by_2(str_to_int1)
50
51int_to_str1 = list_int_to_str(str_2_1)
52sep_digits1 = ""
53for i in int_to_str1:
54    sep_digits1 += "".join(i)
55
56sep3 = split_char(sep_digits1)
57
58str_to_int3 = list_str_to_int(sep3)
59sum_last_add1 = sum(str_to_int3)
60
61sum_last_add2 = sum(str_to_int2)
62
63checksum = sum_last_add1 + sum_last_add2
64
65#Printing the type of digital card based on user input
66if checksum % 10 == 0:
67    if ccn[:2] == "34" or "37" and len(ccn) == 15:
68        print("AMEX")
69    elif ccn[:1] == "4" :
70        if len(ccn) == 13 or 14 or 15 or 16:
71            print("VISA")
72    elif ccn[:2] == "51" or "52" or "53" or "54" or "55" :
73        if len(ccn) == 16:
74            print("MASTERCARD")
75    else:
76        print("INVALID")
77else:
78    print("INVALID")