Bean机器函数Python

2024-05-13 11:29:26 发布

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

豆子机,也被称为梅花形或高尔顿盒,是一种统计学实验设备,以英国科学家弗朗西斯·高尔顿爵士的名字命名。它由一块竖直的木板组成,钉子(或钉子)间隔均匀,呈三角形:

总体思路见下图

enter image description here

我很难想出一个方法来打印(bean的组合)的柱状图,给出一个列中bean数量的列表。在

end_location = [1, 0, 3, 5, 7, 0, 0, 3]

请注意,end_location列表可能并不总是8列,它会有所不同,因此如果方法不依赖于只有8列,那就太棒了。解决这个问题的一种可能的方法是类似num_column = len(end_location),它将为num_column变量分配所需的列数。在

最后,应该输出如下内容(返回或打印,最好返回):

end_location = [1, 0, 3, 5, 7, 0, 0, 3]

    0 
    0
   00
   00  
  000  0  
  000  0
0 000  0

第一个位置有一个bean,第二个位置有0个,第三个位置有3个,第四个位置有5个,第五个位置有7个,等等。。。在

谢谢


Tags: 方法列表间隔columnlocation名字命名num
1条回答
网友
1楼 · 发布于 2024-05-13 11:29:26

给我这个:

end_location = [1, 0, 3, 5, 7, 0, 0, 3]

最多将有max(end_location)行。在

每行可通过以下方式计算:

^{pr2}$

其中v是从max(end_location)到{}。在

所以打印行的代码是:

>>> end_location = [1, 0, 3, 5, 7, 0, 0, 3]
>>> for v in reversed(range(1, max(end_location)+1)):
...     print(''.join('0' if x >= v else ' ' for x in end_location))
... 
    0   
    0   
   00   
   00   
  000  0
  000  0
0 000  0

相关问题 更多 >