Docsity

Programming Assignment Unit 4, Assignments of Computer Science

Toggle sidebar
bg1
Programming Assignment Unit 4
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()
pf2

Partial preview of the text

Download Programming Assignment Unit 4 and more Assignments Computer Science in PDF only on Docsity!

Programming Assignment Unit 4

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.