Implements the Luhn test succinctly.
1 2 3 | def luhn(n):
r = [int(ch) for ch in str(n)][::-1]
return (sum(r[0::2]) + sum(sum(divmod(d*2,10)) for d in r[1::2])) % 10 == 0
|
This solution was chosen as I had to factor the convoluted wikipedia description to find a better way to calculate the answer. I prefer this description, posted on Rosetta Code.
Tags: algorithms
Original wikipedia Python code is more readable than this recipe:
http://en.wikipedia.org/wiki/Luhn_algorithm#Implementation_of_standard_Mod_10