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.159 tracks/python/exercises/luhn/example.py
trackler-2.2.1.158 tracks/python/exercises/luhn/example.py
trackler-2.2.1.157 tracks/python/exercises/luhn/example.py
trackler-2.2.1.156 tracks/python/exercises/luhn/example.py
trackler-2.2.1.155 tracks/python/exercises/luhn/example.py
trackler-2.2.1.154 tracks/python/exercises/luhn/example.py
trackler-2.2.1.153 tracks/python/exercises/luhn/example.py
trackler-2.2.1.152 tracks/python/exercises/luhn/example.py
trackler-2.2.1.151 tracks/python/exercises/luhn/example.py
trackler-2.2.1.150 tracks/python/exercises/luhn/example.py
trackler-2.2.1.149 tracks/python/exercises/luhn/example.py
trackler-2.2.1.148 tracks/python/exercises/luhn/example.py
trackler-2.2.1.147 tracks/python/exercises/luhn/example.py
trackler-2.2.1.146 tracks/python/exercises/luhn/example.py
trackler-2.2.1.145 tracks/python/exercises/luhn/example.py
trackler-2.2.1.144 tracks/python/exercises/luhn/example.py
trackler-2.2.1.143 tracks/python/exercises/luhn/example.py
trackler-2.2.1.142 tracks/python/exercises/luhn/example.py
trackler-2.2.1.141 tracks/python/exercises/luhn/example.py
trackler-2.2.1.140 tracks/python/exercises/luhn/example.py