We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Validating Credit Card Numbers
Validating Credit Card Numbers
+ 0 comments I really prefer regex solutions that you guys posted, but i couldn't come up with my own at the beginning
import re def max_consecutive_digits(digits): max_count = 0 counter = 1 for ch1, ch2 in zip(digits[:-1], digits[1:]): if ch2==ch1: counter+=1 if max_count < counter: max_count = counter else: counter=1 return max_count def is_card_number_valid(s): pat = r'[456]\d{3}(-?\d{4}){3}' if not re.fullmatch(pat, s): return False digits = ''.join(filter(str.isdigit, s)) if max_consecutive_digits(digits) >= 4: return False return True n = int(input()) for _ in range(n): print('Valid' if is_card_number_valid(input()) else 'Invalid')
+ 0 comments import re p = re.compile(r""" (?!.*(\d)(-?\1){3,}) #followed pattern not have repeated 4 digits also through dash (?=(?:^[456])) #followed pattern should start from [456] \d{4}-?\d{4}-?\d{4}-?\d{4}$ #followed pattern should match pattern """,re.X) [print("Valid") if p.search(input()) else print("Invalid") for _ in range(int(input()))]
+ 0 comments import re pattern=re.compile(r'^(?!.*(\d)(-?\1){3,})[456]\d{3}(?:(-?[\d]{4})){3}$') [print("Valid") if re.search(pattern, input()) else print("Invalid") for _ in range(int(input()))]
+ 0 comments I want to use that algorithm on my website! Is it open source? I have a website related to gutachter mainz, we in that company basically analysis the cars and sell them, But some of out customers pay us online and sometime they use fake credit cards. So if it help us that would be great help.
Thanks.
+ 0 comments I used groupby from itertools since I wasnt able to form regex for second pattern here
import re from itertools import groupby pat = r'^[456]\d{3}(-?\d{4}){3}$' for _ in range(int(input())): flag = True inp = input() if re.fullmatch(pat, inp): inp = inp.replace('-','') grp = groupby(inp) for k, g in grp: if len(list(g))>=4: flag = False break else: flag=False if flag: print("Valid") else: print("Invalid")
Load more conversations
Sort 399 Discussions, By:
Please Login in order to post a comment