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.180 tracks/python/exercises/luhn/example.py
trackler-2.2.1.179 tracks/python/exercises/luhn/example.py
trackler-2.2.1.178 tracks/python/exercises/luhn/example.py
trackler-2.2.1.177 tracks/python/exercises/luhn/example.py
trackler-2.2.1.176 tracks/python/exercises/luhn/example.py
trackler-2.2.1.175 tracks/python/exercises/luhn/example.py
trackler-2.2.1.174 tracks/python/exercises/luhn/example.py
trackler-2.2.1.173 tracks/python/exercises/luhn/example.py
trackler-2.2.1.172 tracks/python/exercises/luhn/example.py
trackler-2.2.1.171 tracks/python/exercises/luhn/example.py
trackler-2.2.1.170 tracks/python/exercises/luhn/example.py
trackler-2.2.1.169 tracks/python/exercises/luhn/example.py
trackler-2.2.1.167 tracks/python/exercises/luhn/example.py
trackler-2.2.1.166 tracks/python/exercises/luhn/example.py
trackler-2.2.1.165 tracks/python/exercises/luhn/example.py
trackler-2.2.1.164 tracks/python/exercises/luhn/example.py
trackler-2.2.1.163 tracks/python/exercises/luhn/example.py
trackler-2.2.1.162 tracks/python/exercises/luhn/example.py
trackler-2.2.1.161 tracks/python/exercises/luhn/example.py
trackler-2.2.1.160 tracks/python/exercises/luhn/example.py