logo CodeStepByStep logo

power_concepts

Language/Type: Python recursion

Answer the following questions about the following implementations of a power function:

# implementation 1
def power(x, y):
    return x * power(x, y - 1)
# implementation 2
def power(x, y):
    if y == 0:
        return 1
    else:
        return x * power(x, y - 1)
# implementation 3
def power(x, y):
    if y == 0:
        return 1
    elif y % 2 == 0:
        return x * x * power(x, y // 2)
    else:
        return x * power(x, y - 1)
Which option best describes Implementation #1?
(order shuffled)
What advantage does implementation #3 have over #2?
(order shuffled)
Are all versions recursive?
(order shuffled)

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.