Answer to Question #276906 in Python for solokin

Question #276906

Do 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.



1
Expert's answer
2021-12-08T16:30:16-0500
# Python 3.9.5

def is_divisible(a, b):                # divisibility check function a by b
    return a % b == 0


def is_power(a, b):                    # power of number check function
    if a == 1:                         # check if we could get to 1
        return True
    if b == 1:                         # if equals 1 further division does not make sense return False
        return False
    if not is_divisible(a, b):         # if it cannot be divided entirely, return False
        return False
    return is_power(a / b, b)          # recursively call the function with a/b and b


print("is_power(10, 2) returns: ", is_power(10, 2))
print("is_power(27, 3) returns: ", is_power(81, 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))

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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS