I'm trying to write a function to add the digits of a given number repeatedly until I have a single digit.
So addDigits(345) = 12, 1+2 = 3, so this should return 3
Here's my attempt:
class Solution(object):
def addDigits(self, num):
"""
:type num: int
:rtype: int
"""
if (num >= 0 and num < 10):
return num
else:
total = 0
while (num >= 10):
total += (num % 10)
num /= 10
total += num
self.addDigits(total)
On input 10, I'm getting null back and I have no idea why. I've traced through the code and it looks right to me...
return self.addDigits(total)