I need to write a recursive function getdigits(n)that returns a list of digits in a positive integer n.
Example getdigits(124) => [1,2,4]
So far I have:
def getdigits(n):
if n<10:
return [n]
else:
return [getdigits(n/10)]+[n%10]
But for 124 instead [1, 2, 4]I get[[[1], 2], 4]
Working so that in my head it looks like this:
getdigits(124) = [getdigits(12)] + [4]
getdigits(12) = [getdigits(1)] + [2]
get digits(1) = [1]
therefore getdigits(124) = [1] + [2] + [4] = [1, 2, 4]
I believe that something is wrong in the second part, since I do not see anything wrong with the condition, any proposals, without giving out the whole solution?
source
share