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.20 tracks/python/exercises/luhn/example.py
trackler-2.0.6.19 tracks/python/exercises/luhn/example.py
trackler-2.0.6.18 tracks/python/exercises/luhn/example.py
trackler-2.0.6.17 tracks/python/exercises/luhn/example.py
trackler-2.0.6.16 tracks/python/exercises/luhn/example.py
trackler-2.0.6.15 tracks/python/exercises/luhn/example.py
trackler-2.0.6.14 tracks/python/exercises/luhn/example.py
trackler-2.0.6.13 tracks/python/exercises/luhn/example.py
trackler-2.0.6.12 tracks/python/exercises/luhn/example.py
trackler-2.0.6.11 tracks/python/exercises/luhn/example.py
trackler-2.0.6.10 tracks/python/exercises/luhn/example.py
trackler-2.0.6.9 tracks/python/exercises/luhn/example.py
trackler-2.0.6.8 tracks/python/exercises/luhn/example.py
trackler-2.0.6.7 tracks/python/exercises/luhn/example.py
trackler-2.0.6.6 tracks/python/exercises/luhn/example.py
trackler-2.0.6.5 tracks/python/exercises/luhn/example.py
trackler-2.0.6.4 tracks/python/exercises/luhn/example.py
trackler-2.0.6.3 tracks/python/exercises/luhn/example.py
trackler-2.0.6.2 tracks/python/exercises/luhn/example.py
trackler-2.0.6.1 tracks/python/exercises/luhn/example.py