luhn 27s algorithm python

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

showing results for - "luhn 27s algorithm python"
Kenji
28 Oct 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")
Constanza
11 Nov 2018
1$ pip install fast-luhn
2