logo CodeStepByStep logo

recursion_mystery6

Language/Type: Python recursion

For each of the calls to the following recursive function below, indicate what value is returned:

def recursion_mystery6(s):
    if len(s) == 0:
        return s
    elif len(s) % 2 == 0:
        rest = s[:-1]
        last = s[-1]
        return last + recursion_mystery6(rest)
    else:
        first = s[0]
        rest = s[1:]
        return "(" + first + ")" + recursion_mystery6(rest)
recursion_mystery6("hi")
recursion_mystery6("quirk")
recursion_mystery6("computer")

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.