logo CodeStepByStep logo

recursion_mystery7

Language/Type: Python recursion

For each call to the following recursive function, write the output that is produced as it would appear on the console. Recall that relational operators like < and > compare strings by alphabetical order for example, "a" is less than "b".

def recursion_mystery7(s):
    if len(s) <= 1:
        print(s, end="")
    else:
        first = s[0]
        last  = s[-1]
        mid   = s[1:-1]
        if first < last:
            recursion_mystery7(mid)
            print(last + first.upper(), end="")
        else:
            print("[" + first + "]", end="")
            recursion_mystery7(mid)
            print(last, end="")
recursion_mystery7("abcd")
recursion_mystery7("leonard")
recursion_mystery7("breakfast")

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.