Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
What do show_up(3) and show_down(3) print? Say it before you run it.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. A line before the recursive call runs on the way down. A line after it runs on the way back up.
show_up, the print is after the call. The call show_up(3) has to wait for show_up(2), which waits for show_up(1), which waits for show_up(0), the base case. Then the answers come back: print(1), then print(2), then print(3). The output is 1, 2, 3.show_down, the print is before the call. So 3 is shown first, then the call to show_down(2), and so on. The output is 3, 2, 1.Check. Run it. The first three lines are 1, 2, 3, and the next three are 3, 2, 1.
Answer: 1 2 3 and then 3 2 1, each number on its own line.
Log in to join the discussion.
Loading…
With Standard, you also open 10 more problems, 2 Python labs and 1 extra course. Everything you have used here stays free, always.