Title | CS1101 Assignment - Homework help. |
---|---|
Author | Badri Bista |
Course | Programming Fundamentals |
Institution | University of the People |
Pages | 2 |
File Size | 67.5 KB |
File Type | |
Total Downloads | 84 |
Total Views | 156 |
Homework help....
Exercise 6.4 from your textbook using recursion and the is_divisible function from Section 6.4. 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)) You should submit a script file and a plain text output file (.txt) that contains the test output. Multiple file uploads are permitted. Don’t forget to include descriptive comments in your Python code. Your submission will be assessed using the following Aspects. 1. Does the submission include the is_divisible function from Section 6.4 of the textbook? 2. Does the submission implement an is_power function that takes two arguments? 3. Does the is_power function call is_divisible? 4. Does the is_power function call itself recursively? 5. Does the is_power function include code for the base case of the two arguments being equal? 6. Does the is_power function include code for the base case of the second argument being "1"? 7. Does the submission include correct output for the five test cases? def is_divisible(x, y): if x % y == 0: return True else: return False
# note modulus division of integer
def is_power(x, y): if x == y == 1: # power of 1 is 1 integer(base case) return x == 1 or y == 1 return is_divisible(x, y) or is_power(x/y, y-1)#is_divisible and recursion is_power(27,3) 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))
*** Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32. *** >>> *** Remote Interpreter Reinitialized *** is_power(10, 2) returns: True is_power(27, 3) returns: True is_power(1, 1) returns: True is_power(10, 1) returns: True is_power(3, 3) returns: True >>>...