Answer:
# stack counter
def sc(n):
sum = 0
for d in str(n):
sum += int(d)
if sum>9:
return sc(sum)
return sum
# double stack count
def dsc(n):
return sc(2*n)
def isValidSIN(s):
if len(s) != 9:
return False
sum = dsc(s[1]) + dsc(s[3]) + dsc(s[5]) + dsc(s[7])
sum += int(s[0]) + int(s[2]) + int(s[4]) + int(s[6])
checkDigit = (10 - (sum % 10))%10
return int(s[-1]) == checkDigit
SINS = [
'518759097',
'066600271',
'666062989',
'299675595',
'022147292',
'321122210',
'553277039',
'398148031',
'392430286',
'548504232',
'463665182',
'217427954',
'204305551',
'000071647']
for sin in SINS:
if isValidSIN(sin): print(sin + " is valid")
else: print(sin + " is INVALID")
Explanation:
I do believe there is a mistake in step e. If the sum has a units digit of 0, the subtraction would give you 10 for the check digit and that will fail. Therefore I added another modulo 10.