Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Computer Engineering Expert

Lab: Exploring Recursion and the Stack Frame

Laboratory Activities: Recursion the stack, and problem-solving

ACTIVITY: Recursion's stack frames

1. Download the file powers_rec.py from Moodle.

2. Run the program in PyCharm. It should display a single line of text.

3. Set a breakpoint on the first line of powers.

4. Use the debugger to Step into My Function to step through the program.

5. Observe how the stack grows with each function call, and shrinks with each return.

6. Observe how each frame has its own variable named n.

7. Watch how large the stack grows!

ACTIVITY: Questions for powers

1. When the stack is the largest, how many powers frames are there?

2. Using Big-O notation, express the number of powers frames as a function of n (the exponent)

ACTIVITY: Recursion's stack frames

1. Set a breakpoint on the first line of superpowers.

2. Use the debugger to Step into My Function to step through the program.

3. Observe how the stack grows with each function call, and shrinks with each return.

4. Observe how each frame has its own variable named n.

5. Watch how large the stack grows!

ACTIVITY: Questions for superpowers

1. When the stack is the largest, how many superpowers frames are there?

2. What's the best case? I.e., for a given n, what's the smallest number of stack frames required by superpowers?

3. What's the worst case? I.e., for a given n, what's the largest number of stack frames required by superpowers?

4. Using Big-O notation, express the number of superpowers frames as a function of n (the exponent).

ACTIVITY: Divide and conquer for max

Python has a built-in function named max, which returns the largest value of a list.

For practice, implement a recursive function called maximum that returns the maximum value of a list of numbers.

1. Implement maximum1 as a recursive function that divides the list into a problem one smaller than the original.

2. Implement maximum2 as a recursive function that divides the list into two sub-lists of roughly equal halves.

Use list slicing to obtain your sub-problems.

ACTIVITY: Divide and conquer for sum

On Slide 10, we started a derivation for a version of sum that reduces the problem of summing 1 + 2 + · · · + n into a problem that reduces n to n=2.

Continue the derivation, and implement a recursive function from it.

Name your function supersum, because Python already defines a built-in function named sum.

Your implementation should have the same time complexity as superpowers.

To check, you can modify the program timing.py.

Attachment:- Assignment Files.rar

Computer Engineering, Engineering

  • Category:- Computer Engineering
  • Reference No.:- M92740341
  • Price:- $120

Guranteed 48 Hours Delivery, In Price:- $120

Have any Question?


Related Questions in Computer Engineering

Based on land minerals and natural resources labor and

Based on land, minerals and natural resources, labor and entrepreneurial innovation, which country do you feel has the greatest long-term potential China or Russia.

1 a consumer has 300 to spend on goods x and y the market

1. A consumer has $300 to spend on goods X and Y. The market prices of these two goods are Px $15 and Py $5. a. What is the market rate of substitution between goods X and Y? b. Show how the consumer's opportunity set ch ...

A specialty software development firm is planning to offer

A specialty software development firm is planning to offer one of four new software products and wishes to maximize profit, minimize risk, and increase market share. If a weight of 65% is assigned to profit potential, 20 ...

Design a moore machine where the output y goes high 1 when

Design a Moore machine where the output Y goes high (=1) when the last four bits of the input X were 1110: 4th to last bit seen = 1 3rd to last bit seen = 1 2nd to last bit seen = 1 Last bit seen = 0 Your machine must be ...

The glen arboretum wants to start removing invasive norway

The Glen arboretum wants to start removing invasive Norway Maple trees. To determine just how bad the problem is you set up five plots that are three meters on each side. You find that your plots have 2, 5, 9, 1, and 3 N ...

Using the following dataa sex- 7 males 1 female height-

Using the following data, A.) (Sex)- 7 males, 1 female. (Height)- 72,67,72,64,66,68,68,70. (Left or right handed)- 7 right handed, 1 left handed. Let's assume our class is truly representative of the population at large. ...

You have been offered a contract worth 1 million per year

You have been offered a contract worth 1 million per year for five years. However, to take the contract, you will need to purchase some new equipment. Your discount rate for this project is 12%. You are still negotiating ...

After sal aurigemma received his phd from the university of

After Sal Aurigemma received his PhD from the University of Hawaii, he became an assistant professor at the University of Tulsa. There, he introduced the school to Aloha Friday, when people come to work in their colorful ...

Case description mama mias mm pizzeria offers pizzas

Case Description: Mama Mia's (MM) pizzeria offers pizzas, breadsticks, and Buffalo chicken wings for carry out customers only. Customers phone in the order and come by to pick up their order. When regular customers call ...

The monthly sales demand for a new product is uncertain but

The monthly sales demand for a new product is uncertain, but it is considered to be adequately described by a normal random variable with mean 50,000 units and variance 100,000,000. (a) A factory to manufacture the new p ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As