适合此操作的最佳代码是什么?

2024-06-12 03:20:18 发布

您现在位置:Python中文网/ 问答频道 /正文

刚开始使用python,不到一周。这个练习开始了。我设法在网上找到了这段代码,并开始掌握它:

“一只蜗牛掉进125厘米的井底。蜗牛每天上升30厘米。但是晚上睡觉的时候,因为墙壁湿滑了20厘米。从井里逃出来要多少天?你知道吗

-将问题数据分配给具有代表性名称的变量 井高、日进、夜退、累计距离

well_height = 125

daily_advance = 30

night_retreat = -20

accumulated_distance = daily_advance + night_retreat

totalcms = 0

-将0赋给表示解决方案的变量

days = 0

-编写解决问题的代码

snailhasnotescaped = True
while snailhasnotescaped:
    totalcms += accumulated_distance
    days += 1
if totalcms >= well_height:
snailhasnotescaped = False

-使用Print打印结果('Days=',Days)

print("Days = ", days, "days")"

现在是问我这些问题:

蜗牛走过的距离现在由一个列表定义。你知道吗

advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]

升井需要多长时间? 它一天的最大排水量是多少?最小值是多少? 它白天的平均速度是多少? 白天位移的标准差是多少?你知道吗

有人能帮我吗? 先谢谢你


Tags: 代码距离daysdistancedailyheightwellnight
3条回答

首先,想象一下,如果蛇在第X天到达顶端之前还有15厘米的距离,它会到达井的顶端吗?显然,因为他前进了30厘米,然后又后退了20厘米。小心,你的代码没有考虑到这一点。我会这样做:

day = 1
while True:
    totalcms += daily_advance

    if totalcms >= well_height:
        break

    totalcms -= night_retreat
    day += 1

print(day)

请注意while True是一种坏的做法。只有当你确定你会打破循环时才是好的。您可以在这里添加一个条件来验证snake在进入循环之前是否前进。相反,你可以决定先下楼再上楼,然后做while totalcms < well_height。你知道吗

您还可以围绕一个函数包装所有内容,以便能够更有效地使用它。你知道吗

下一个问题模棱两可。是每天的预付费吗?如果是的话,不用while循环,只需遍历列表(它甚至可以解决我们的问题)。至于其他问题,我相信你能解决!你知道吗

祝你好运!你知道吗

well_height = 125

daily_advance = 30

night_retreat = -20

accumulated_distance = daily_advance + night_retreat

totalcms = 0

days=0

snailhasnotescaped = True
while snailhasnotescaped:
    totalcms += daily_advance
    if totalcms >= well_height:
        snailhasnotescaped = False
        break

    days += 1

    totalcms += night_retreat
    if totalcms >= well_height:
        snailhasnotescaped = False
        break


print(days)

您可以尝试:

from statistics import mean,stdev
well_height = 125

daily_advance = 30

night_retreat = -20


advance_cm = [30, 21, 33, 77, 44, 45, 23, 45, 12, 34, 55]

accumulated_distance = daily_advance + night_retreat

totalcms = 0

days=0
count=0

snailhasnotescaped = True
while snailhasnotescaped:
    totalcms += advance_cm[count]
    if totalcms >= well_height:
        snailhasnotescaped = False
        break

    days += 1

    totalcms += night_retreat
    if totalcms >= well_height:
        snailhasnotescaped = False
        break
    count+=1



print(days)

print("max distance in day=",max(advance_cm))
print("min distance in day=",min(advance_cm))
print("avg distance in day=",mean(advance_cm))
print("standard deviation distance in day=",stdev(advance_cm))

下面是你的代码:

snailhasnotescaped = True
while snailhasnotescaped:
    totalcms += accumulated_distance
    days += 1
    if totalcms >= well_height:
        snailhasnotescaped = False

现在,accumulated_distance是常量。您可以预先将其设置为accumulated_distance = daily_advance + night_retreat。有了新问题,你就没办法了。因此,以下是如何调整解决方案以适应新问题,顺序如下:


How long does it take to raise the well?

不是一次计算accumulated_distance,而是需要为每一天计算它:

for day_cm in advance_cm:
    days += 1
    totalcms += day_cm  # add however much it travels that day
    if totalcms >= well_height:
        break  # just stop the for loop immediately - we've escaped, therefore we already know what `days` is supposed to be
    totalcms -= night_retreat  # If we didn't escape, then we'll lose 20cm overnight

如果advance_cm中的天数不够,则可以在循环完成后检查:

if totalcms < well_height:
    # do something

What is its maximum displacement in one day?

好吧,你知道它每天走多远-你只需要找到一天中最远的距离。Python有一个内置函数,可以方便地完成这项工作:

max_displacement = max(advance_cm)

或者,如果在夜间向后摔倒算作总位移,您可能需要更改回路以生成新的位移列表:

displacements = []
for day_cm in advance_cm:
    days += 1
    totalcms += day_cm
    displacements.append(day_cm)  # how much did we move during the day
    if totalcms >= well_height:
        break
    totalcms -= night_retreat
    displacements[-1] -= night_retreat  # remove our fall distance from the last element of the list, where we put how much we moved during the day

然后我们可以从中得到最大位移:

max_displacement = max(displacements)

What is its average speed during the day?

What is the standard deviation of its displacement during the day?

你应该知道怎么计算平均数。Python有一个内置的sum()函数来获取一个列表中所有元素的总和,这使得这变得很容易:

avg_speed = sum(displacements) / days  # unit: cm per day

至于标准差,这有点棘手-你需要知道标准差公式,并计算它。谢天谢地,python的标准库包含了statistics包,它可以为您做到这一点!你知道吗

import statistics
...
std_dev = statistics.stdev(displacements)

相关问题 更多 >