

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


def main(): pass if name == 'main': main() def is_divisible(x, y): # calling function is_divisible() if x % y == 0: return True else: return False def is_power(a, b): #Calling function is_power() if a == b: # base case of the two arguments being equal return True elif b == 1: # base case of the second argument being "1" return False elif is_divisible(a,b) == False: #function call is_divisible return False else: return is_power(a / b, b) # divide a by b and recursively call is_power()
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)) Output 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 Reference: Downey, A. (2015). Think Python: How to think like a computer scientist. Needham, Massachusetts: Green Tree Press.