Recursive function problem


#1

Hey,

As a homework, I have to define a function that returns a frozenset of the positions who are going to explode (the function is_to_explode() is already defined and works). So what this function does, is, given a board, (defined as a list), I’d have to check each elements to know if in this position the element will explode, and that by using a recursive way, well I just don’t see a startpoint. Furthermore, what is a parameter with a default value ?

Well if anyone could help, it’d be really great

Thanks in advance.

ps, a screenshot of the function


#2
def foo(a, b="bar")
    # code

The call to the function needs only one argument, which will be assigned to a, and b will take the default value. To set b to any value, pass a second argument:

foo(6, "foo")

#3

Thank you so much,
I wrote this function, using the default value ( hoping I didn’t get it wrong), but it doesn’t work and I actually can’t find my mistake.


#4

You are creating a set and then discarding it, may as well not create it in the first place

If you’re using recursion to iterate over the board, then it’s rather strange that you’ve also got a for-loop iterating over the board. What’s the intention behind using recursion?

frozenset doesn’t seem like an appropriate value type for a recursive function to return, combining results will involve making a copy each time. Your teachers might not care. I feel it’s missing half the point.


#5

Thanks for your remarks,
-how can you see that I am discarding it ? How could I ensure that it won’t happen ?
-Using the recursion is someway part of the function, I just can’t find a way without mixing iterative and recursive…


#6

Well if you do this: (and nothing else)

a = 1

Then a is being discarded because nothing’s done with it once it’s created.

Each subsequent invocation should receive a slightly smaller problem. You could make that one position less each time. (In a way you’re already doing that since your loop and recursion kinda do the same thing)
(That still makes the whole thing very forced since two for-loops would make it all much simpler)


#7

The thing is that I am using it, I add elements who satisfies the if statement.
I often create a list and then add elements, and it actually works, why not here ?


#8

Yes you add elements. And what do you do with the set when that’s done?


#9

When I checked all the board, the set becomes a frozenset, and is returned, No ?


#10

I don’t see

return lst

What I do see is the function being exited (and nothing is done with the set)

After populating your set, there’s only one more line of code in the function, and that line doesn’t mention your set


#11

so doing new_set = frozenset(lst)
return new_set would solve the problem ?
Thanks for helping btw


#12

I’m not saying that. I’m saying you’re creating a value and then discarding it.

The general pattern for iterating with recursion is to process the first element and recurse for the rest
For example, the sum of a list is the sum of the first element and the sum of the rest of the list


#13

OK…
I am not that good in programming…
Maybe if have a more acurate question, I’ll come back


#14

To nearly spell out the solution:

The trivial case is where there are no more positions to check
Otherwise, you’d obtain the set for the remaining positions (by recursing) and consolidating that with the result for the current position

Compare that to my sum example. the current one, plus the sum of the rest. Same thing.

More than anything else, programming is to decide what should happen. Nothing falls into place by itself, it’s entirely up to you to make things fit together. The hard part of programming doesn’t involve knowing much programming at all, it’s about considering the problem and what should happen.


#15

I tried this, what more simple is, but I don’t see why it doesn’t check the hole board, it stops at (2,1) (star_pos[1] = 2)

22


#16

You’re still discarding the set. You need to combine the result of the current location, and the rest of the locations.
Being on the last location isn’t a special case, the next call should return an empty set since that call will have zero locations left to check. (The sum of a list with one element is the current element plus the sum of the rest. The sum of an empty list is 0)

As for why it stops, (and if it stops at all) I don’t know. You’ll just have to find what leads to that happening. Use prints or a debugger that can step through the code


#17

I found the solution, a big thanks


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.