#Python 2, 96 bytes
Python 2, 96 bytes
def g(s):u=7<len(s)<10and sum(x*int(('0'+s)[-x])for x in range(2,10))-int(s[-1]);print(u%11<1)*u
Takes a string as input. The function adds a '0'
to the front of the string whether it needs it or not, and uses Python's negative indices to add elements, starting from the end of the string and working back-to-front.
The -1xI
is handled separately, using a second call to int()
. I couldn't figure out how to avoid this without costing more bytes than I saved.
def g(s):u=7<len(s)<10and sum(x*int(('0'+s)[-x])for x in range(10))-2*int(s[-1]);print(u%11<1)*u
would work just as well, since it would add 1
times s[-1]
but then subtract it twice, and it would also add 0
times (something) which of course wouldn't affect the sum.