Sha256: f59e3ef67b4c36878bb0fb97f9ee48c4d4e963f893f7cadd79cb3a6c6b5bead0

Contents?: true

Size: 627 Bytes

Versions: 82

Compression:

Stored size: 627 Bytes

Contents

class Luhn(object):
    def __init__(self, card_num):
        self.card_num = card_num.replace(" ", "")

    def addends(self):
        def luhn_transform(n):
            return (2 * n - 9) if (n > 4) else (2 * n)
        old_digits = [int(d) for d in str(self.card_num)]
        return [(luhn_transform(n) if (i % 2 == 0) else n)
                for i, n in enumerate(old_digits, start=len(old_digits) % 2)]

    def checksum(self):
        return sum(self.addends())

    def is_valid(self):
        if len(self.card_num) <= 1 or not self.card_num.isdigit():
            return False
        return self.checksum() % 10 == 0

Version data entries

82 entries across 82 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/python/exercises/luhn/example.py
trackler-2.2.1.118 tracks/python/exercises/luhn/example.py
trackler-2.2.1.117 tracks/python/exercises/luhn/example.py
trackler-2.2.1.116 tracks/python/exercises/luhn/example.py
trackler-2.2.1.115 tracks/python/exercises/luhn/example.py
trackler-2.2.1.114 tracks/python/exercises/luhn/example.py
trackler-2.2.1.113 tracks/python/exercises/luhn/example.py
trackler-2.2.1.111 tracks/python/exercises/luhn/example.py
trackler-2.2.1.110 tracks/python/exercises/luhn/example.py
trackler-2.2.1.109 tracks/python/exercises/luhn/example.py
trackler-2.2.1.108 tracks/python/exercises/luhn/example.py
trackler-2.2.1.107 tracks/python/exercises/luhn/example.py
trackler-2.2.1.106 tracks/python/exercises/luhn/example.py
trackler-2.2.1.105 tracks/python/exercises/luhn/example.py
trackler-2.2.1.104 tracks/python/exercises/luhn/example.py
trackler-2.2.1.103 tracks/python/exercises/luhn/example.py
trackler-2.2.1.102 tracks/python/exercises/luhn/example.py
trackler-2.2.1.101 tracks/python/exercises/luhn/example.py
trackler-2.2.1.100 tracks/python/exercises/luhn/example.py
trackler-2.2.1.99 tracks/python/exercises/luhn/example.py