Question

Programming Exercise 11.4 m | Instructions expo.py + An alternative strategy for the expo function uses the following recursi

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

CODE:

def expo(base, exponent):

expo.calls +=1 #Used to track recursive call counts

#recursive expo function

#if exponent == 0, then return 1 (as per the constraint)

if exponent == 0:

return 1

#if exponent == 1, then returing base as base^1 is base itself

if exponent == 1:

return base

# if exponent > 1, then using recursion function (as per the constraint)

# you can use the following two recursive methods to get the exponent

# if exponent != 1:

# return base * expo(base, exponent - 1)

if exponent != 1:

return expo(base, exponent // 2) ** 2

expo.calls = 0

def main():

for exponent in range(5):

print(exponent, expo(2,exponent))

print("Expo calls: ", expo.calls)

if __name__ == "__main__":

main()

CODE SCREENSHOTS:

1 def expo(base, exponent): expo.calls +=1 #Used to track recursive call counts 2. 3 4 5 #recursive expo function #if exponen

OUTPUT SCREENSHOT

0 1 Expo calls: 1 1 2 Expo calls: 2 24 Expo calls: 4 38 Expo calls: 7 4 16 Expo calls: 11

Hope this helps.

Add a comment
Know the answer?
Add Answer to:
Programming Exercise 11.4 m | Instructions expo.py + An alternative strategy for the expo function uses...
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
  • Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result...

    Hem 1 def expo(base, exponent): # Write your function here Python's pow function returns the result of raising a number to a given power. Define a function expo that performs this task, and state its computational complexity using big-o notation. The first argument of this function is the number, and the second argument is the exponent (nonnegative numbers only) 4 def main(): ***Tests with powers of 2.*** for exponent in range (5): print(exponent, expo(2, exponent)) 000 9 if _name__ ==...

  • Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n):...

    Programming Exercise 11.6 Х + | Instructions fib.py >_ Terminal + iit B 1 def fib(n): 2 "*"Returns the nth Fibonacci number. " 3 if n < 3: lil 4 return 1 Modify the recursive Fibonacci function to employ the memoization technique discussed in this chapter. The function creates a dictionary and then defines a nested recursive helper function named memoizedFib You will need to create a dictionary to cache the sum of the fib function. The base case of...

  • Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_...

    Python pls CENGAGE MINDTAP Q Search this course Programming Exercise 11.1 x Instructions search.py + >_ Terminal + A sequential search of a sorted list can halt when the target is less than a given element in the list. 1 " 2 File: search.py 3 Project 11.1 4 5 Optimizes sequential search for sorted lists. 6 The complexity is O(n) in the worst case and 0(1) in the best case. 7 The average case is less than n / 2,...

  • Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature...

    Programming Exercise 8.3 | Instructions Write a GUI-based program that allows the user to convert temperature values between degrees Fahrenheit and degrees Celsius. The interface should have labeled entry fields for these two values. breezypythongui.py temperatureconvert... + 1 2 File: temperatureconverter.py 3 Project 8.3 4 Temperature conversion between Fahrenheit and Celsius. 5 Illustrates the use of numeric data fields. 6 • These components should be arranged in a grid where the labels occupy the first row and the corresponding fields...

  • X Programming Exercise 8.6 | Instructions breezypythongui.py taxformwithgui.py + Q Desktop + ve Add radio button...

    X Programming Exercise 8.6 | Instructions breezypythongui.py taxformwithgui.py + Q Desktop + ve Add radio button options for filing status to the tax calculator program of Project 1. The user selects one of these options to determine the tax rate. The Single option's rate is 20%. The Married option is 15%. The Divorced option is 10%. The default option is Single. 1 2 File: taxformwithgui.py 3 Project 8.6 4 A GUI-based tax calculator. 5 6 Computes and prints the total...

  • PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end...

    PLEASE HELP!!! C PROGRAMMING CODE!!! Please solve these functions using the prototypes provided. At the end please include a main function that tests the functions that will go into a separate driver file. Prototypes int R_get_int (void); int R_pow void R Jarvis int start); void R_fill_array(int arrayll, int len); void R_prt_array (int arrayl, int len) void R-copy-back (int from[], int to [], int len); int R_count_num (int num, int arrayll, int len) (int base, int ex) 3. Build and run...

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • Please answer ALL parts completely-- answer with clear explanation will get a thumbs up! Problem 2:...

    Please answer ALL parts completely-- answer with clear explanation will get a thumbs up! Problem 2: Programming [2 points] Consider the following pseudo code where a function has been defined for a non negative integer x. Note that the function calls itself (within the while loop) making it a recursive function. def func (x) print x while i<x: i = i+1 func (x-1) (a) How many times does the program print if we call func (0) and func (1), respectively?...

  • C++ Quesetion In this exercise, you are to modify the Classify Numbers programming example in this...

    C++ Quesetion In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an unspecified length; that is, the program does not know in advance how many...

  • Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for...

    Exercise 1 Use Top-Down Design to “design” a set of instructions to write an algorithm for “travel arrangement”. For example, at a high level of abstraction, the algorithm for “travel arrangement” is: book a hotel buy a plane ticket rent a car Using the principle of stepwise refinement, write more detailed pseudocode for each of these three steps at a lower level of abstraction. Exercise 2 Asymptotic Complexity (3 pts) Determine the Big-O notation for the following growth functions: 1....

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