-
Notifications
You must be signed in to change notification settings - Fork 3
/
luhn.py
55 lines (38 loc) · 1.41 KB
/
luhn.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
import unittest
# regras
# 1 - Números em em casa ímpar multiplica-se por x2
# 2 - Números maiores que nove, subtrai-se 9
# 3 - (sum(numeros) % 10) == 0
def double(string):
nums = [int(i) for i in string]
return [n if i % 2 == 0 else 2 * n
for i, n in enumerate(nums)]
def minus9(numbers):
return (n if n <= 9 else n-9 for n in numbers)
def gensum(code):
digits = minus9(double(code))
return (sum(digits) * 9) % 10
def checksum(number):
return gensum(number[:-1]) == int(number[-1])
class TestDojoMethods(unittest.TestCase):
def test_double(self):
self.assertEqual(double("0"), [0])
self.assertEqual(double("0123"), [0, 2, 2, 6])
self.assertEqual(double("3752"), [3, 14, 5, 4])
def test_maior9(self):
self.assertEqual(list(minus9([0])), [0])
self.assertEqual(list(minus9([0, 2, 2, 6])), [0, 2, 2, 6])
self.assertEqual(list(minus9([3, 14, 5, 4])), [3, 5, 5, 4])
def test_gensum(self):
self.assertEqual(gensum("0"), 0)
self.assertEqual(gensum("7992739871"), 3)
self.assertEqual(gensum("201773"), 9)
def test_checksum(self):
self.assertTrue(checksum("79927398713"))
self.assertFalse(checksum("79927398712"))
def test_generic(self):
self.assertEqual(True, True)
self.assertFalse(0==1)
self.assertTrue(0==0)
if __name__ == '__main__':
unittest.main()