Project 2 Human Pyramid Calculations

偶尔善良 提交于 2020-05-17 07:52:50

问题


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 C are 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 C are carrying. Half of the weight that person B is shouldering (50 pounds) gets transmitted down onto person E and half the weight that person C is shouldering (50 pounds) similarly gets sent down to person E, so person E ends up feeling an extra 100 pounds. That means she’s supporting a net total of 300 pounds.

Write a recursive function (using no loops), weightOn(r,c), which returns the weight on the back of the person in row r and and column c. Rows and columns are 0-based, so the top position is (0,0), for example, and person H is in position (3,1). The following also hold: weightOn(0,0) == 0.00 weightOn(3,1) == 425.00 Weights should be floating-point numbers.

I have already tried a lot. I will include my most recent code below.

t = 0.0
x = 0.0

def weightOn(r, c):

    global t
    if r < 0:
        print('Not valid')
    elif r == 0 and c == 0:
        return t
    elif r > 0 and c == 0:
        t += 200 / (2 ** r)
        return weightOn(r - 1, 0)
    elif r > 0 and c == r:
        t += 200 / (2 ** r)
        return weightOn(r - 1, 0)
    elif r > c > 0:
        mid(r, c)
        return t

def mid(r, c):

    global x
    x = weightOn(r - 1, c - 1) + weightOn(r - 1, c)
'''I have also tried: x = (((weightOn(r - 1, c - 1) + 200) / 2) + ((weightOn(r - 1, c) + 200) / 2))'''
    return x

r = int(input('r: '))
c = int(input('c: '))
weightOn(r, c)
if r > c > 0:
    print(x)
else:
    print(t)

It always brings up the wrong output. I can correctly pull up all of the edges (when c == 0 or c == r). But other than that it won't work.

Ex. Input (3, 1) outputs 500 (3, 2) outputs 550


回答1:


Using global variables suggests that you haven't considered this recursively.

Each person shoulders half the weight of the persons on each shoulder. The effective weight of each person is what they shoulder, plus 200 pounds. If a person is on the edge, then the "person" on the other shoulder has 0 weight.

So ...

def weight(r, c):
# Code base cases
if r < 0:            # Past the pyramid top; no such person
    return 0
if c < 0 or c > r:   # Off the edge; no such person
    return 0
return 200 + (weight(r - 1, c - 1) + weight(r - 1, c)) / 2

Then weightOn is simply the above routine without the 200 +.

That's your outline; can you take it from there?



来源:https://stackoverflow.com/questions/57982663/project-2-human-pyramid-calculations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!