Sha256: 860f55a79f16ccbc21ffc763e66b624a6353ca547e7b56634e556083278d03c9

Contents?: true

Size: 615 Bytes

Versions: 198

Compression:

Stored size: 615 Bytes

Contents

class Luhn(object):
    def __init__(self, string):
        self.string = string.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.string)]
        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.string) <= 1 or not self.string.isdigit():
            return False
        return self.checksum() % 10 == 0

Version data entries

198 entries across 198 versions & 1 rubygems

Version Path
trackler-2.2.1.96 tracks/python/exercises/luhn/example.py
trackler-2.2.1.95 tracks/python/exercises/luhn/example.py
trackler-2.2.1.94 tracks/python/exercises/luhn/example.py
trackler-2.2.1.93 tracks/python/exercises/luhn/example.py
trackler-2.2.1.92 tracks/python/exercises/luhn/example.py
trackler-2.2.1.91 tracks/python/exercises/luhn/example.py
trackler-2.2.1.90 tracks/python/exercises/luhn/example.py
trackler-2.2.1.89 tracks/python/exercises/luhn/example.py
trackler-2.2.1.88 tracks/python/exercises/luhn/example.py
trackler-2.2.1.87 tracks/python/exercises/luhn/example.py
trackler-2.2.1.86 tracks/python/exercises/luhn/example.py
trackler-2.2.1.85 tracks/python/exercises/luhn/example.py
trackler-2.2.1.84 tracks/python/exercises/luhn/example.py
trackler-2.2.1.83 tracks/python/exercises/luhn/example.py
trackler-2.2.1.82 tracks/python/exercises/luhn/example.py
trackler-2.2.1.81 tracks/python/exercises/luhn/example.py
trackler-2.2.1.80 tracks/python/exercises/luhn/example.py
trackler-2.2.1.79 tracks/python/exercises/luhn/example.py
trackler-2.2.1.78 tracks/python/exercises/luhn/example.py
trackler-2.2.1.77 tracks/python/exercises/luhn/example.py