I am working on this seemingly simple problem, where I need to add one to every digit of a number. Example: number = 1234 ; output = 2345
That's simple, but when 9 is one of those digits, then by the law of addition, that 9 will be replaced by 0 and 1 will be added to the number on the left (9 + 1 = 10, hence, place value = 0 & carry over = 1) Example: number = 1239 ; output = 2350
number = 1234
s = str(number)
l = []
for num in s:
num = int(num)
num += 1
if num > 9:
num = 0
l.append(num)
else:
l.append(num)
print int(''.join(str(v) for v in l))
Can someone please explain to me, what logic should I use? I can see something on the lines of modular arithmetic, but not really sure how to implement that. Thanks :)