首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pascal Triangle Python

Pascal Triangle Python
EN

Stack Overflow用户
提问于 2017-10-30 23:25:24
回答 3查看 10.2K关注 0票数 0

所以我一直在做一个pascal三角形,但是我试图在每一行上做一些标签,比如Row=0,Row=1,Row=2,我试着把这些标签放在Pascal三角形上的每一行开始之前。有没有人能帮我在这段代码的基础上实现它?谢谢。

代码语言:javascript
复制
x = int(input("Enter the desired height. "))
list=[1]
for i in range(x):
    print(list)
    newlist=[]
    newlist.append(list[0])
    for i in range(len(list)-1):
        newlist.append(list[i]+list[i+1])
    newlist.append(list[-1])
    list=newlist
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-30 23:44:02

首先,请避免使用内置函数的名称作为变量名(在本例中为list;我已将其更改为l)。除此之外,您提到的放置标签只是简单地引用您拥有的最外层循环的迭代。以下代码的行为应该与预期一致:

代码语言:javascript
复制
x = int(input("Enter the desired height. "))
l = [1]
for i in range(x):
    # Modified v
    print("Row", i + 1, l)
    newlist = []
    newlist.append(l[0])
    for i in range(len(l) - 1):
        newlist.append(l[i] + l[i+1])
    newlist.append(l[-1])
    l = newlist

下面是一个示例运行:

代码语言:javascript
复制
Enter the desired height. 4
Row 1 [1]
Row 2 [1, 1]
Row 3 [1, 2, 1]
Row 4 [1, 3, 3, 1]

希望这能有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2017-10-31 00:05:42

如果你从右到左重新考虑这个问题,而不是从左到右,它会简化很多:

代码语言:javascript
复制
rows = int(input("Enter the desired height: "))

array = []

for row in range(1, rows + 1):
    array.append(1)  # both widen the row and initialize last element

    for i in range(row - 2, 0, -1):  # fill in the row, right to left
        array[i] += array[i - 1]  # current computed from previous

    print("Row", row, array)

输出

代码语言:javascript
复制
Enter the desired height: 9
Row 1 [1]
Row 2 [1, 1]
Row 3 [1, 2, 1]
Row 4 [1, 3, 3, 1]
Row 5 [1, 4, 6, 4, 1]
Row 6 [1, 5, 10, 10, 5, 1]
Row 7 [1, 6, 15, 20, 15, 6, 1]
Row 8 [1, 7, 21, 35, 35, 21, 7, 1]
Row 9 [1, 8, 28, 56, 70, 56, 28, 8, 1]
票数 1
EN

Stack Overflow用户

发布于 2019-04-08 21:42:37

我认为代码是不言自明的。You can Visualise on it here.

代码语言:javascript
复制
def pascal_triangle(degree):
'''
Gives row and column wise enrtry for given degree
'''
Pascal_list =[[1]]   #FIrst entry Defined to start 
print(Pascal_list[0] ,'\n')
for i in range(1,degree+1): #+1 As we are starting from 1
    temp_list =[]
    for j in range(i+1):  #+1 As we are considering last element
        if(j==0):#First Element = 1
            temp_list.append(1)
            continue
        elif(j == i):#Last Element = 1
            temp_list.append(1)
            continue
        else:
            temp_list.append(Pascal_list[i-1][j]+Pascal_list[i-1][j-1]) # Addition of Upper Two Elements
    Pascal_list.append(temp_list)
    print(Pascal_list[i] ,'\n')

return Pascal_list

输入:

代码语言:javascript
复制
Pascal_Triangle = pascal_triangle(4)
print('Pascal_Triangle: ', Pascal_Triangle)

输出:

代码语言:javascript
复制
[1] 

[1, 1] 

[1, 2, 1] 

[1, 3, 3, 1] 

[1, 4, 6, 4, 1] 

Pascal_Triangle:  [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1]]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47018868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档