Question

Background to this question Functions can return booleans, which is often convenient for hiding complicated tests...

Background to this question

Functions can return booleans, which is often convenient for hiding complicated tests inside functions. For example:

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

return True

else: return False

It is common to give boolean functions names that sound like yes/no questions; is_divisible returns either True or False to indicate whether x is divisible by y. Here is an example: >>> is_divisible(6, 4)

False

>>> is_divisible(6, 3)

True

The result of the == operator is a boolean, so we can write the function more concisely by returning it directly: def is_divisible(x, y): return x % y == 0 Boolean functions are often used in conditional statements: if is_divisible(x, y): print('x is divisible by y') It might be tempting to write something like: if is_divisible(x, y) == True: print('x is divisible by y') But the extra comparison is unnecessary.

As an exercise, write a function is_between(x, y, z) that returns True if x y z or False otherwise.

The QUESTION

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))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

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))

Add a comment
Know the answer?
Add Answer to:
Background to this question Functions can return booleans, which is often convenient for hiding complicated tests...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  •    /**    * Returns an array of booleans that are set true or    *...

       /**    * Returns an array of booleans that are set true or    * false based on the associated values in the array    * arr using the following rules:    * 1. If arr[i] is divisible by three then the boolean    * value in the the array returned at the same position    * should be true    * 2. Unless the values in arr[i] is also divisible by 5,    * then the value returned...

  • In this task you are asked to completely implement the following functions (ie. write both the...

    In this task you are asked to completely implement the following functions (ie. write both the function header and function body) 1) You have been tasked to write a Processing function called isleapYear, which receives an integer representing a year. The function should return true if the year is a leap year and false otherwise. (For your information, a year is leap year if it is divisible by 4 but not 100, or is divisible by 400) 2) You have...

  • Write a function in python that takes a set A and a relation R(x, y) on...

    Write a function in python that takes a set A and a relation R(x, y) on A (as a python function such that R(x, y) returns true if and only if the relation xRy holds), and returns True if and only if the relation R is reflexive. Here is the function signature you need to use. def is reflexive(A, R): You can test your code as follows. s = {1,2,3} def y(x, y): return x == y def n(x, y):...

  • PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give...

    PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define new functions or helper...

  • In python, please help me fill in these functions given their limitations. Please use for loops....

    In python, please help me fill in these functions given their limitations. Please use for loops. def palindrome(x): """ For a given string, determine if the string is a palindrome. (The same forward and back) Input: String of any size (including empty) Return: Boolean (True if palindrome, False otherwise) Limitation: You must use a loop. Only function allowed to be used is len (if needed). Cannot use "in" besides "for var in container" """ pass def getCount(char, strng): """ Get...

  • The purpose of this lab is to practice working with Python's higher order functions and ternary...

    The purpose of this lab is to practice working with Python's higher order functions and ternary operator. Write all of your code in one file named lab2.py. You can write the code for items 1 through 6 directly inside the main function. Let a be the list of values produced by list(range(1, 11)). [1 point] Using the map function and a lambda argument, write an expression that produces a list of the cubes of the values in a. Assign the...

  • PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP!...

    PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP! THANK YOU! RECURSIVE! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define...

  • Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven-...

    Write C++ Code in single source file (.cpp file) containing following user defined functions: 1. IsEven- takes an integer input (one argument of type int) and return true or false. 2. IsPositive- takes a float input (one argument of type double) and return a Boolean value. The function returns the true if its argument is positive and false if its argument is zero or negative. 3. IsPrime- takes an integer input and return true or false. 4. NumOfDigits- takes an...

  • Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to...

    Requirements Write functions isMemberR, a recursive function, and isMemberI, which will use an iterative approach, to implement a binary search algorithm to determine whether a given element is a member of a given sequence Each function will have two parameters, aseq, a sorted sequence, and target. isMemberR and isMemberI will return True if target is an element of the sequence, and False otherwise. Your implementations must implement the binary search algorithm described above. When function i sMemberR recursively invokes itself,...

  • BlockPy: #33.8) Maximum Odd Use the Min/Max pattern to write a function maximum_odd that finds the...

    BlockPy: #33.8) Maximum Odd Use the Min/Max pattern to write a function maximum_odd that finds the highest odd value in the list (consumes a list of numbers and returns a number). Use the provided helper function is_odd (that consumes a single number and returns a boolean indicating whether it is true or false). Do not change the helper function is odd. Call your maximum_odd function on your favorite list of numbers Console Feedback: Instructor Feedback You cannot use the builtin...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT