-1

The purpous of using recursion instead of using for i in range(11): is because its advantagous to start from the top trying to solve a specific mathematical problem. The function will be changed so that it only returns [n]that matches certain criteria.

print(numbers)=[[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]] Why is there extra brackets? print(numbers[7])=IndexError: list index out of range Has this anything to do with the extra brackets?

# A function that is supposed to help sorting numbers in a list
def sorting_numbers(n):
    if n > 1:
        return [n] + sorting_numbers(n-1)
    else:
        return [1]

numbers = []
n = 10
numbers = (sorting_numbers(n))
print(numbers)

print(numbers)=[[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]] Why is there extra brackets? print(numbers[7])=IndexError: list index out of range Has this anything to do with the extra brackets?

  • "So that it only returns [n] that matches certain criteria" use a list comprehension for this. There really isn't any advantage in using recursion for this problem. – Óscar López Aug 07 '13 at 22:29
  • If you have further questions, please post them as separate questions. You keep deleting / undeleting / editing and changing the scope of the question every time, that's not how SO works. And regarding the last question: see this [answer](http://stackoverflow.com/a/8177274/201359) to see how to increment the recursion limit - but _really_ that's not the solution, the solution is avoiding the use of recursion – Óscar López Aug 07 '13 at 22:53
  • Did you _read_ the answer? the correct way to use `sorting_numbers` is: `numbers = storing_numbers(10)`, don't use `append`! – Óscar López Aug 07 '13 at 23:00
  • 1
    Ok i will not change the question next time. I´m sorry. I will post new questions instead. Thank you for your time. It helped me alot. I did read it and tried it out. I missed to remove append. My bad. – Progrmming is fun Aug 07 '13 at 23:06

2 Answers2

0

Whenever you make a recursive call, you're returning a tuple: (number, recursive_result). Thus, each time you recurse, you embed another layer of tuples into your final result.

It looks like your intention was just to sum the numbers from 1 to the input number, so all you have to do to fix it is change line 3 to return the result of the recursive call plus the original number, rather than just returning them both outright.

def function(number):
    if number > 1:
        # return the recursive result added to the original number
        return number + function(number-1) 
    else:
        return number

As a side note, you ought to give your function a more descriptive name, so that its purpose is clear!

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
0

Well if you really want to use recursion for such a simple problem - be warned that it's terribly inefficient and it will cause a maximum recursion depth exceeded error for values of n close to 1000 (Python was not designed to handle recursion efficiently):

def storing_numbers(n):
    if n > 1:
        return [n] + storing_numbers(n-1)
    else:
        return [1]

Or a bit shorter:

def storing_numbers(n):
    return [] if n <= 0 else [n] + storing_numbers(n-1)

Notice how we build the list along the way, and that the base case also returns a list. Use it like this:

numbers = storing_numbers(10)
numbers
=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

Of course, it'd be unrealistic to use a recursive function for building a list for anything beyond an academic exercise. A real-life, practical implementation would do this instead:

list(range(10, 0, -1))
=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

And here's how you'd use a list comprehension for filtering numbers matching a certain criteria, let's say, only even numbers:

[x for x in range(10, 0, -1) if x % 2 == 0]
=> [10, 8, 6, 4, 2]
Óscar López
  • 232,561
  • 37
  • 312
  • 386