-1
def recursion_list(number_list):
    if number_list == []:
        return []
    if number_list[0] > 100:
        return number_list[0]
    else:
        return recursion_list(number_list[1:])

If I have a list, [1, 3, 152, 100, 200]. I want to iterate through all the values, and return all the values that are over 100. But so far this function only returns 152, which is the first value that is over 100, but it does not iterate through the rest. How would I iterate over the rest of the list, and then return it as a list? So [152, 100, 200].

Natch
  • 41
  • 1
  • 5
  • This question is similar: https://stackoverflow.com/questions/64865631/why-is-this-recursive-map-function-using-all-available-memory/64865759#64865759 – Barmar Nov 22 '20 at 02:20
  • 2
    The function should always return a list. What does it return when the `if` is true? – Barmar Nov 22 '20 at 02:21
  • `if number_list[0] > 100`, considering that `number_list[0]` is only the value that you're currently looking at, what values should you `return`? This one, of course, but perhaps some others? Perhaps ones that you determine via recursion? – Karl Knechtel Nov 22 '20 at 02:51

5 Answers5

3

You still need to call the recursion even if the first element meets your condition:

def recursion_list(number_list):
    if not number_list:
        return number_list
    
    if number_list[0] > 100:
        return [number_list[0]] + recursion_list(number_list[1:])
    else:
        return recursion_list(number_list[1:])

    
recursion_list([1, 3, 152, 100, 200])
# [152, 200]

For these kind of exercises a generator is sometimes clearer:

def recursion_list(number_list):
    if not number_list:
        return
    
    first, *rest = number_list
    
    if first > 100:
        yield first

    yield from recursion_list(rest)

    
list(recursion_list([1, 3, 152, 100, 200]))
Mark
  • 90,562
  • 7
  • 108
  • 148
0

Try this, you are missing a recursive call:

def recursion_list(number_list):
    if not number_list:
        return []
    if number_list[0] > 100:
        return [number_list[0]] + recursion_list(number_list[1:])
    else:
        return recursion_list(number_list[1:])

print(recursion_list([102,23,12,205]))

Recursion is not definitely the way to do this, can't you just [x for x in number_list if x > 100]

Wasif
  • 14,755
  • 3
  • 14
  • 34
0

The easiest way would just be with iteration:

def iteration_list(number_list):
    over_hundred = []
    for i in number_list:
        if i > 100:
            over_hundred.append(i)
    return over_hundred

This would return [152, 200] because 100 is not bigger than 100.

Josh C
  • 1
  • 1
0

The better option is to run threw your array of values (e.g. [1, 3, 152, 100, 200]) using the controller 'for'

def recursion_list(number_list):
    sorted_list = []
    for value in number_list:
        if number_list == []:
            return number_list
        if value > 100:  # Considere using   >=   to integrate 100 in your array
            sorted_list.append(value)
    return sorted_list
    # Will give you [152, 200]
Toykato
  • 89
  • 5
-1

Here is one way

def recursion_list(number_list):
    list = []
    for i in (i for i in range(len(number_list)) if number_list[i] > 100):
        list.append(number_list[i])
    
    return list
etch_45
  • 792
  • 1
  • 6
  • 21