Hackbright Code Challenges

Reverse a String Recursively

Reverse a String Recursively

Whiteboard

Medium

Challenge

Easier

Concepts

Recursion, Strings

Download

rev-string-recursively.zip

Solution

Reverse a String Recursively: Solution


Given a string, return a new string that is the original string, reversed. Do this with recursion, not a for loop.

For example, as “porcupine” would reverse to “enipucrop”:

>>> rev_string("porcupine")
'enipucrop'

Don’t Use Built-In Functions

You may not use the Python built-in functions reverse or reversed to solve this.

We’ve provided a file, reverse.py, with a stub implementation of the function rev_string:

reverse.py
def rev_string(astring):
    """Return reverse of string using recursion.

    You may NOT use the reversed() function!
    """