Sha256: 46873a19cc749f36a91aaf5bb4e3878044aeee62137c6668d71ac1d4c1278951

Contents?: true

Size: 628 Bytes

Versions: 116

Compression:

Stored size: 628 Bytes

Contents

class Luhn(object):
    def __init__(self, number):
        self.number = number

    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.number)]
        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):
        return self.checksum() % 10 == 0

    @staticmethod
    def create(n):
        diff = (10 - Luhn(n * 10).checksum()) % 10
        return 10 * n + diff

Version data entries

116 entries across 116 versions & 1 rubygems

Version Path
trackler-2.0.8.15 tracks/python/exercises/luhn/example.py
trackler-2.0.8.14 tracks/python/exercises/luhn/example.py
trackler-2.0.8.13 tracks/python/exercises/luhn/example.py
trackler-2.0.8.12 tracks/python/exercises/luhn/example.py
trackler-2.0.8.11 tracks/python/exercises/luhn/example.py
trackler-2.0.8.10 tracks/python/exercises/luhn/example.py
trackler-2.0.8.9 tracks/python/exercises/luhn/example.py
trackler-2.0.8.8 tracks/python/exercises/luhn/example.py
trackler-2.0.8.7 tracks/python/exercises/luhn/example.py
trackler-2.0.8.6 tracks/python/exercises/luhn/example.py
trackler-2.0.8.5 tracks/python/exercises/luhn/example.py
trackler-2.0.8.4 tracks/python/exercises/luhn/example.py
trackler-2.0.8.3 tracks/python/exercises/luhn/example.py
trackler-2.0.8.2 tracks/python/exercises/luhn/example.py
trackler-2.0.8.1 tracks/python/exercises/luhn/example.py
trackler-2.0.7.0 tracks/python/exercises/luhn/example.py
trackler-2.0.6.44 tracks/python/exercises/luhn/example.py
trackler-2.0.6.43 tracks/python/exercises/luhn/example.py
trackler-2.0.6.42 tracks/python/exercises/luhn/example.py
trackler-2.0.6.41 tracks/python/exercises/luhn/example.py