-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalcc.py
59 lines (51 loc) · 1.22 KB
/
valcc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import re
''' Test Data:
8
4253625879615786
4424424424442444
5122-2368-7954-3214
42536258796157867
4424444424442444
5122-2368-7954 - 3214
44244x4424442444
0525362587961578
'''
def valid(cc):
'''Valid Credit Card:
1) Must start with 4-6
2) Must contain exactly 16 digits
3) Must only contain digits (0-9)
4) May have digits in groups of 4 seperated by a hyphen
5) No other separators allowed
6) Must not have 4 or more consecutive repeated digits
'''
# 1:
if not re.match(r'^[4-6]', cc):
return False
# 2:
if len(re.findall(r'\d', cc)) != 16:
return False
# 3, 5:
if re.search(r'[^-\d]', cc):
return False
# 6:
if re.search(r'(\d)-?\1-?\1-?\1', cc):
return False
# 4:
if re.match(r'(\d{4}-?){3}\d{4}', cc):
return True
# Alternatively validate 1-5 with:
# r'^[456]\d{3}(-?)\d{4}\1\d{4}\1\d{4}$'
# Alternatively validate 6 by first removing hyphens and then using
# simple regex
return False
def main():
count = int(input())
for _ in range(count):
cc = input()
if valid(cc):
print('Valid')
else:
print('Invalid')
if __name__ == '__main__':
main()