Back to questions
JuniorTheoryCommon
What does the program print?
def countdown(n):
if n == 0:
return "go"
return str(n) + countdown(n - 1)
print(countdown(3))321go
- Expecting
123go, as if the deepest call's digit came first: each call puts its ownstr(n)in front of the result of the next call, so3leads. - Expecting
3210go: atn == 0the base case returns"go"at once, and the line that addsstr(n)never runs, so0is not added.
- What happens if you call
countdown(-1)? - How would you change the function so that it returns
go123?