3

I need to write a a recursive function getdigits(n) that returns the list of digits in the positive integer n.

Example getdigits(124) => [1,2,4]

So far what I've got is:

def getdigits(n):
    if n<10:
        return [n]
    else:
        return [getdigits(n/10)]+[n%10]

But for 124 instead of [1, 2, 4] I get [[[1], 2], 4]

Working that in in my head it goes like:

getdigits(124) = [getdigits(12)] + [4]
getdigits(12) = [getdigits(1)] + [2]
get digits(1) = [1]

Therefore getdigits(124) = [1] + [2] + [4] = [1, 2, 4]

I figure there's something wrong in the second part of it as I can't see anything wrong with the condition, any suggestions without giving the entire solution away?

papanito
  • 2,349
  • 2
  • 32
  • 60
Markaj
  • 173
  • 2
  • 2
  • 7
  • Does it have to be recursion? The simple solution would simply be `return list(str(n))` – Glider May 13 '12 at 12:47
  • 1
    So, then, why don't you return just 'getdigits(n/10) + [n%10]' instead of '[getdigits(n/10)] + [n%10]'? – user1227804 May 13 '12 at 12:51
  • 2
    possible duplicate of [How to rewrite this function as a recursive function?](http://stackoverflow.com/questions/10568848/how-to-rewrite-this-function-as-a-recursive-function) – Óscar López May 13 '12 at 13:25
  • Added the homework tab - the only reason you would *need* a function to be recursive is homework. It's fine to post it on here, but tag it as such. – Gareth Latty May 13 '12 at 13:38
  • None of the functions posted so far is able to convert a 1000-digit long number. Can anyone write `getdigits` so that 1) it's recursive, 2) it doesn't exhaust the stack? – georg May 13 '12 at 14:28
  • @thg435: those requirements are incompatible in python. If you are using recursion, then you are consuming stack space. It will run out. – Ned Batchelder May 13 '12 at 15:25
  • @NedBatchelder: still, I think it's possible. You're going to need a decorator though. – georg May 13 '12 at 15:32
  • @thg435: you think it's possible to write a recursive function in Python without consuming stack space? I don't see how, and I don't see what a decorator would do for you. – Ned Batchelder May 13 '12 at 15:39
  • @Ned: we can make it tail-recursive, and then apply a decorator that converts tail recursion to iteration. – georg May 13 '12 at 15:48
  • @thg435: I don't think a decorator will be able to do that, they typically don't deal with the internals of the functions they wrap. But if you manage it, lots of people would like to see it! – Ned Batchelder May 13 '12 at 15:54
  • @Ned: someone already did that: http://code.activestate.com/recipes/496691-new-tail-recursion-decorator – georg May 13 '12 at 16:03
  • @thg435: ok, it's possible, but, ick. – Ned Batchelder May 13 '12 at 17:05
  • @Ned: yes, this is a rather ugly hack, and unfortunately, [TRO/TCO is not going to be added to Python](http://neopythonic.blogspot.de/2009/04/final-words-on-tail-calls.html). – georg May 13 '12 at 17:21

7 Answers7

2

getDigits return a list, so why do you wrap the list into another one (last line)?

Vito De Tullio
  • 2,332
  • 3
  • 33
  • 52
1

Your problem is either that you're returning [n] instead of n or using [getDigits(n / 10)] instead of getDigits(n / 10).

For instance, with your example:

getDigits(124) = [getDigits(12)] + [4] = [getDigits(12), 4]
getDigits(12) = [getDigits(1)] + [2] = [getDigits(1), 2]
getDigits(1) = [1]

Therefore it follows:

getDigits(12) = [[1], 2]
getDigits(124) = [[[1], 2], 4]
1

The same question has been answered before, see the link for more details.

def getdigits(n):
    if n < 10:
        return [n]
    return getdigits(n // 10) + [n % 10]

getdigits(123)
> [1, 2, 3]

The above will work for an integer n >= 0, notice that you don't have to wrap the result of getdigits(n // 10) inside another list.

Aadi Shah
  • 99
  • 1
  • 8
Óscar López
  • 232,561
  • 37
  • 312
  • 386
0

Your question sounds like homework (recursive requirement), but this could be done with a list comprehension

>>> def getdigits(n):
...    return [int(y) for y in str(n)]
... 
>>> getdigits(12345)
[1, 2, 3, 4, 5]
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
0

You can simply use this:

>>> num=124
>>>list(map(int,str(num)))
[1,2,4]
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
0

Using Lambda

(lambda x: [int(a) for a in str(y)])(number)
fthiella
  • 48,073
  • 15
  • 90
  • 106
Mirage
  • 30,868
  • 62
  • 166
  • 261
0
def getdigits(n):
    if n<10:
        return [n]
    else:
        return getdigits(n//10)+[n%10]

your answer is in float. You need to use floor division to make it [1] instead of 1.2

M_S_N
  • 2,764
  • 1
  • 17
  • 38
Ngoc
  • 1