Skip to content

Instantly share code, notes, and snippets.

@gEndelf
Created January 7, 2016 10:16
Show Gist options
  • Save gEndelf/5f2aaa691a361b216f80 to your computer and use it in GitHub Desktop.
Save gEndelf/5f2aaa691a361b216f80 to your computer and use it in GitHub Desktop.
anlgorithm for credit card validation
import re
def is_credit_card_valid(n):
credit_card_str = str(n)
credit_card_as_list = map(int, credit_card_str)
if not re.match('^\d{1,16}$', credit_card_str):
return False
double_pos = 0 if len(credit_card_str) % 2 == 0 else 1
for pos in xrange(double_pos, len(credit_card_as_list), 2):
value = credit_card_as_list[pos] * 2
if value > 9:
value = sum(map(int, str(value)))
credit_card_as_list[pos] = value
return bool(sum(credit_card_as_list) % 10 == 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment