Question #114210

using recursion and the is_divisible function, Your program may assume that both arguments to is_power are positive integers. Note that the only positive integer that is a power of "1" is "1" itself.


After writing your is_power function, include the following test cases in your script to exercise the function and print the results:


print("is_power(10, 2) returns: ", is_power(10, 2))

print("is_power(27, 3) returns: ", is_power(27, 3))

print("is_power(1, 1) returns: ", is_power(1, 1))

print("is_power(10, 1) returns: ", is_power(10, 1))

print("is_power(3, 3) returns: ", is_power(3, 3))

Expert's answer

Code:

def is_divisible(x, y):
    return x % y == 0

def is_power(x, y):
    if x == 1:
        return True
    if y == 1:
        return False
                
    if is_divisible(x, y):
        return is_power(x//y, y)
    return False  

Result:

$python is_power.py
is_power(10, 2) returns:  False
is_power(27, 3) returns:  True
is_power(1, 1) returns:  True
is_power(10, 1) returns:  False
is_power(3, 3) returns:  True

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS