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.6.40 tracks/python/exercises/luhn/example.py
trackler-2.0.6.39 tracks/python/exercises/luhn/example.py
trackler-2.0.6.38 tracks/python/exercises/luhn/example.py
trackler-2.0.6.37 tracks/python/exercises/luhn/example.py
trackler-2.0.6.36 tracks/python/exercises/luhn/example.py
trackler-2.0.6.35 tracks/python/exercises/luhn/example.py
trackler-2.0.6.34 tracks/python/exercises/luhn/example.py
trackler-2.0.6.33 tracks/python/exercises/luhn/example.py
trackler-2.0.6.32 tracks/python/exercises/luhn/example.py
trackler-2.0.6.31 tracks/python/exercises/luhn/example.py
trackler-2.0.6.30 tracks/python/exercises/luhn/example.py
trackler-2.0.6.29 tracks/python/exercises/luhn/example.py
trackler-2.0.6.28 tracks/python/exercises/luhn/example.py
trackler-2.0.6.27 tracks/python/exercises/luhn/example.py
trackler-2.0.6.26 tracks/python/exercises/luhn/example.py
trackler-2.0.6.25 tracks/python/exercises/luhn/example.py
trackler-2.0.6.24 tracks/python/exercises/luhn/example.py
trackler-2.0.6.23 tracks/python/exercises/luhn/example.py
trackler-2.0.6.22 tracks/python/exercises/luhn/example.py
trackler-2.0.6.21 tracks/python/exercises/luhn/example.py