Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in Python Project 1: Human Pyramids CS 1410 Background A human pyramid is a way of stacking people vertically in a triangle. With the exception

in Python
image text in transcribed
image text in transcribed
image text in transcribed
Project 1: Human Pyramids CS 1410 Background A human pyramid is a way of stacking people vertically in a triangle. With the exception of the people in the bottom row, each person splits their weight evenly on the two people below them in the pyramid. Their "weight", however, includes not only their own body weight but the weight they are supporting above them. For example, in the pyramid above, person A splits her weight across persons B and C and person H splits his weight-plus the accumulated weight of the people he's supporting-onto people Land M. In this question, you'll explore just how much weight that is For simplicity we will assume that everyone in the pyramid weighs exactly 200 pounds. Person A at the top of the pyramid has no weight on her back. People B and Care each carrying half of person A's weight. That means that each of them is shouldering 100 pounds. Now, let's look at the people in the third row. Let's begin by focusing on person E. How much weight is she supporting? Well, she's directly supporting half the weight of person B (100 pounds) and half the weight of person E (100 pounds so she's supporting at least 200 pounds. On top of this, she's feeling some of the weight that people B and Care carrying. Half of the weight that person is shouldering (50 pounds) gets transmitted down onto person E and half the weight that person is shouldering (50 pounds) Similarly gets sent down to person, so person ends up feeling an extra 100 pounds. That means she's supporting a net total of 300 pounds Not everyone in that third row is feeling the same amount, though. Look at person for example. The only weight on person comes from person 8. Person therefore ends up supporting half of person B's body weight (100 pounds)plus half of the weight person is holding up 150 pounds). Dashboard Calendar To Do Notifications Inbox for a total of 150 pounds, only half of what is feeling! Going deeper in the pyramid, how much weight is person H feeling? Well, person His supporting half of person D's body weight (100 pounds). half of person E's body weight (100 pounds), plus half of the weight person D is holding up (75 pounds), plus half of the weight person is holding up (150) pounds. The net effect is that person H is carrying 425 pounds-ouch! A similar calculation shows that person I is also carrying 425 pounds-can you see why? Compare this to person G. Person Gis supporting half of person D's body weight (100 pounds), plus half of the weight person Dis holding up (75 pounds) or a net total of 175 pounds. Finally, let's look at person M in the middle of the bottom row. How is she doing? Well, she's supporting half of person H's body weight (100 pounds). half of person I's body weight (100 pounds). half of the weight person H is holding up (212.5 pounds), and half of the weight person I is holding up (215.5 pounds). for a net total of 625 pounds! Keep in mind that the first and last people in each row calculate their weight differently than people in interior positions. The weight on any person can thus be computed recursively, with the base case being the person at the top the person at the top of the pyramid, who is shouldering 0 pounds. Requirements 1. Write a recursive function (using no loops), weightOn(r.c), which returns the weight on the back of the person in row rand and column c. Rows and columns are O-based, so the top position is (0,0), for example, and person H is in position (3,1). The following also hold: weightOn(0.8) - weighton (3.1) 8.00 425.ee Dashboard Calendar To Do Notifications Inbox Weights should be floating point numbers 2. When run as a main module, accept the number of rows to process via sys.argil, and then print each row as a line at a time as you compute them, using 2 decimals: $ python) pyramid.py 7 . 100.ee lee.ee 150.00 300.00 150.00 175.00 425.00 425.00 175.00 187.50 500.00 625.00 500. 187.50 193.75 543.75 762.50 762.50 563.75 193.75 196.88 568.75 853.12 962.50 853.12 568.75 196.88 Elapsed time: 0.0002778119999999988 seconds Name your file pyramid.py. Use time.perf_counter to time your main function 3. After finishing part 2, you will notice that it takes a long time for a large number of rows because many recursive calls are repeated. For example, on my machine, processing 23 rows took 7.47 seconds. To avoid calling weighton for the same row and column more than once, save them in a dictionary named cache at the module level. The key for the dictionary is the tuple (row.column) and the value is the weight on the person in that (row.column) position. The first thing that weighton should do is check to see if there is a previously computed entry for the key ir.c) in cache. If there is simply return it. Otherwise, compute the weight recursively and save the result in cache before returning it. With caching it took only 0.0009 seconds to process 23 rows! Print out the elapsed run time, the total number of calls to weighton, and the total number of "cache hits" (the number of times the number was retrieved from the cache instead of evaluating it in the following format: 100.00 100.00 150.00 300.00 150.00 175.00 425.ee 425.00 175.ee 187.50 500.00 625.00 500.ee 187.50 193.75 543.75 762.50 762.50 543.75 193.75 196.88 568.75 853.12 962.50 853 12 568.75 196.88 Elapsed time: 0.0019854999999999873 seconds Number of function calls: 70 Number of cache hits: 42 Turn in the final version of your code and the output it gives in the format above. Your function weight On and your cache (which must be named cache) will be tested on arbitrary values. Dashboard Calendar To Do Notifications inbox

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

From Herds To Insights Harnessing Data Analytics For Sustainable Livestock Farming

Authors: Prof Suresh Neethirajan

1st Edition

B0CFD6K6KK, 979-8857075487

More Books

Students also viewed these Databases questions

Question

An action plan is prepared.

Answered: 1 week ago