Question

In: Computer Science

THIS IS IN PYTHON 3.0 Let's call the first function power(a,b). Use the built-in power function...

THIS IS IN PYTHON 3.0

Let's call the first function power(a,b). Use the built-in power function a**b in python to write a second function called testing(c) that tests if the first function is working or not.

I already have the function power(a,b), which is as follows:

def power(a, b):

    if (b == 0): return 1

    elif (int(b % 2) == 0):

        return (power(a, int(b / 2)) *

               power(a, int(b / 2)))

    else:

        return (a * power(a, int(a / 2)) *

                   power(a, int(b / 2)))

Solutions

Expert Solution

Here is the answer for your question in Python Programming Language.

CODE :

"""Declaring a and b values as global."""
a = 3
b = 4
def power(a, b):
if b == 0:
return 1
elif b%2 == 0:
return power(a, int(b/2))*power(a, int(b/2))
else:
return a*power(a, int(b/2))*power(a, int(b/2))

def result(c):
"""Description : Checks if the result returned by power(a, b) is correct or not."""
if (a**b) == c:
"""If a power of b equals c returns true"""
return True
else:
"""Otherwise returns false"""
return False

def main():
"""Description : Calls power and result functions."""
global a,b
c = power(a,b)
print("Result returned by power function for {0} power of {1} is {2} which is {3}".format(a,b,c,result(c)))

"""Calling main()"""
main()

SCREENSHOTS :

Please see the screenshots of the code below for indentations of the code. As python is a Language of indentations kindly check the indentations before execution.

OUTPUT :

Any doubts regarding this can be explained with pleasure :)


Related Solutions

Directly using mathematical expression, DO NOT USE the built-in Python functions, evaluate the binomial function for...
Directly using mathematical expression, DO NOT USE the built-in Python functions, evaluate the binomial function for n = 20, p = 3/8. Make sure you plot your results (Remember the the binomial distribution is discrete)
In the Python: Note 1: You may not use these python built-in functions: sorted(), min(), max(),...
In the Python: Note 1: You may not use these python built-in functions: sorted(), min(), max(), sum(), pow(), zip(), map(), append(), count() and counter(). (7 points) unique.py: A number in a list is unique if it appears only once. Given a list of random numbers, print the unique numbers and their count. Print the duplicate numbers and their count. Sample runs: Enter the size of the list : 7 [8, 2, 6, 5, 2, 4, 5] There are 3 unique...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
language python use a functions and write a python of a computer with three power operations:...
language python use a functions and write a python of a computer with three power operations: power by two, power by three, and power by four. input a number and operation (^2,^3,or^4). output: the result of the power operation
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
which statements are true about Python functions? a)Different functions cannot use same function name b)a function...
which statements are true about Python functions? a)Different functions cannot use same function name b)a function always returns some value c)different function cannot use the same variable names d) function must use the same parameter names as the corresponding variables in the caller what benefits does structuring a program through defining functions bring? a) there is a possibility of reducing the number of variables and/or objects that must be managed at any cost at any one point b)the program is...
Using Python 1. #Now, let's improve our steps() function to take one parameter #that represents the...
Using Python 1. #Now, let's improve our steps() function to take one parameter #that represents the number of 'steps' to print. It should #then return a string that, when printed, shows output like #the following: # #print(steps(3)) #111 #   222 #       333 # #print(steps(6)) #111 #   222 #       333 #           444 #               555 #                   666 # #Specifically, it should start with 1, and show three of each #number from...
The coding must be formatted in Python. Write a function matrix_power(A, n) that computes the power...
The coding must be formatted in Python. Write a function matrix_power(A, n) that computes the power An using Boolean arithmetic and returns the result. You may assume that A is a 2D list containing only 0s and 1s, A is square (same number of rows and columns), and n is an integer ≥ 1. You should call your previously written matrix multiply boolean function. Example: Let R = [ [0, 0, 0, 1], [0, 1, 1, 0], [0, 0, 0,...
Using Python create a script called create_notes_drs.py. In the file, define and call a function called...
Using Python create a script called create_notes_drs.py. In the file, define and call a function called main that does the following: Creates a directory called CyberSecurity-Notes in the current working directory Within the CyberSecurity-Notes directory, creates 24 sub-directories (sub-folders), called Week 1, Week 2, Week 3, and so on until up through Week 24 Within each week directory, create 3 sub-directories, called Day 1, Day 2, and Day 3 Bonus Challenge: Add a conditional statement to abort the script if...
in python: (can use ntlk) Given the following documents: • Can we go to Disney??!!!!!! Let's...
in python: (can use ntlk) Given the following documents: • Can we go to Disney??!!!!!! Let's go on a plane! • The New England Patriots won the Super Bowl.. • I HATE going to school so early • When will I be considered an adult? • I want to go to A&M, Baylor, or the University of Texas. Conduct punctuation removal, stop word removal, casefolding, lemmatization, stemming on the documents.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT