Most credit cards are assigned a unique number that can be validated by an algorithm developed by Hans Peter Luhn from IBM. The Luhn algorithm can detect almost any single-letter, almost all transpositions of adjacent digits except 09 and 90, and many other errors. In 1960 a patent on the Luhn algorithm was granted to IBM (U.S. Patent 29500481), but nowadays it can be freely used by everyone.

The Luhn algorithm verifies a credit card number against its included check digit, which is appended to a partial credit card number to generate the full credit card number. This credit card number must pass the following test:

According to the Luhn algorithm, the credit card number 49927398716 is valid. The sum is calculated as follows: \[ 6 + (2) + 7 + (1 + 6) + 9 + (6) + 7 + (4) + 9 + (1 + 8) + 4 = 70 \] and this result is divisible by 10. In this example, we have enclosed the digits of the numbers that have been doubled in between brackets.

Assignment

Your task is to write the following three functions, that each take a credit card number as a string argument.

Make sure to make optimal reuse of your source code when implementing these three functions.

Example

>>> luhn('49927398716')
70
>>> luhn('49927938716')
67
>>> luhn('79927398716')
73

>>> valid('49927398716')
True
>>> valid('49927938716')
False
>>> valid('79927398716')
False

>>> addcheck('4992739871')
'49927398716'
>>> addcheck('4992793871')
'49927938719'
>>> addcheck('7992739871')
'79927398713'