Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write reverse(text) by recursion. What are reverse("python") and reverse("a")?
Reverse the rest, then add the first letter at the end.
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, compare your working with the solution.
Base case. A text of one letter, or none, is its own reverse.
Recursive case. Reverse the text without its first letter, then put the first letter at the end: reverse(text[1:]) + text[0].
reverse("python") is nohtyp.reverse("a") is a.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.