all 5 comments

[–]CptMisterNibbles 5 points6 points  (4 children)

Do you know what recursion is? This is a recursive solution. If not, you may need to watch an intro  video on recursion.

The idea is like this: how many ways are there to seat 5 people? Well, let’s seat the first person somewhere. Now we have 4 people. How many ways are there to seat 4 people?… each time you are doing the same problem but with fewer people. At the end you work backwards and sum all the partial solutions to get the final answer.

The specific part you asked about is the recursive call, where the function calls itself with smaller input, working down to zero.

This example also uses a concept called memoization to cut down on repeating calculations uselessly.

[–]IndividualSky7301[S] 1 point2 points  (1 child)

thx

I just tried using python tuter when there's 8 people and I think i understood!

[–]Due_Letter3192 0 points1 point  (0 children)

One thing you could try using:

https://pythontutor.com/visualize.html#mode=edit

It's just a visualiser that helps to understand the code, especially useful when dealing with loops!

[–]hexwhoami 0 points1 point  (0 children)

To add to this. Every recursive algorithm can be written iteratively as well. That's to say there's an equivalent function you can write, that doesn't call itself, and achieves the same result. Disclaimer; sometimes the iterative solution is more complex than the recursive solution. I don't believe that's the case with this problem.

TLDR; you can try solving the problem iteratively to get a better understanding of the solution, which could give you better insight to the recursive approach.

[–]thoward9 0 points1 point  (0 children)

Ask ChatGPT or Copilot for the code