omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / luhn-test-of-credit-card-numbers / oracle.py
| 1 | # Independent reference for "Luhn test of credit card numbers" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | def luhn(s): |
| 4 | total = 0 |
| 5 | for i, ch in enumerate(reversed(s)): |
| 6 | d = int(ch) |
| 7 | if i % 2 == 1: |
| 8 | d = sum(divmod(2 * d, 10)) |
| 9 | total += d |
| 10 | return total % 10 == 0 |
| 11 | for s in ["49927398716", "49927398717", "1234567812345678", "1234567812345670"]: |
| 12 | print(s, "valid" if luhn(s) else "invalid") |