From 58551699d240768cbef0dbe6491876b2e7eca644 Mon Sep 17 00:00:00 2001 From: Xevion Date: Sat, 13 Jul 2019 21:56:38 -0500 Subject: [PATCH] luhn exercise numeric check edit --- python/luhn/luhn.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/python/luhn/luhn.py b/python/luhn/luhn.py index a73db02..cd41480 100644 --- a/python/luhn/luhn.py +++ b/python/luhn/luhn.py @@ -1,8 +1,11 @@ class Luhn(object): def __init__(self, card_num): - self.card_num = ''.join(filter(lambda x : x in '0123456789', card_num)) + self.card_num = card_num def valid(self): - temp = [int(num[1]) * 2 if (num[0]+1) % 2 == 0 else int(num[1]) for num in enumerate(self.card_num[::-1])] + if len(self.card_num) < 2 or not self.card_num.isnumeric(): + return False + temp = ''.join(filter(lambda x : x in '0123456789', self.card_num)) + temp = [int(num[1]) * 2 if (num[0]+1) % 2 == 0 else int(num[1]) for num in enumerate(temp[::-1])] temp = [num - 9 if num > 9 else num for num in temp] return sum(temp) % 10 == 0 \ No newline at end of file