Python Pascal的三角递归程序

时间:2017-12-24 19:19:42

标签: python recursion pascals-triangle

我正在尝试编写一个打印pascal三角形的程序。这是我的代码:

def combination(n, k):
    if k == 0 or k == n:
        return str(1)
    else:
        return combination(str(n-1, k-1)) + combination(str(n-1,k))

def pascals_triangle(rows):
    for row in range(rows):
        answer = ""
        for column in range(row + 1):
            answer = answer + combination(row, column) + "\t"
        print(answer)

pascals_triangle(10)

这是我给予的工作(这是作业):

# To complete this assignment, replace the code for the
# combination function with the proper definition.

def combination(n, k):
    return "C(" + str(n) + "," + str(k) + ")"

def pascals_triangle(rows):
    for row in range(rows):
        answer = ""
        for column in range(row + 1):
            answer = answer + combination(row, column) + "\t"
        print(answer)

pascals_triangle(10)

应打印出来:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

我知道问题出在组合函数中,但每次我尝试修复时都会出现更多错误。这当前给我带来了这个错误:

TypeError:str()参数2必须是str,而不是int

我非常喜欢初学者,所以很可能我也错过了其他一些东西。我是否可以帮助修复此错误以及我遗漏的任何其他内容?

2 个答案:

答案 0 :(得分:0)

对于每个新级别,在每一侧插入一个,并在中间条目中插入上述级别的每一对的总和。

t = [[1]]
for _ in range(10):
   level = [1]
   for ii in range(len(t[-1]) - 1):
      level += [t[-1][ii] + t[-1][ii+1]]
   level += [1]
   t.append(level)

for level in t: print level

我真的不确定你在编写的代码中会发生什么。我试着去医生但是我不明白你想用这个组合功能实现什么。

def combination(n, k):
    if k == 0 or k == n:
        return 1
    else:
        return combination(n-1, k-1) + combination(n-1,k)

def pascals_triangle(rows):
    for row in range(rows):
        answer = 0
        for column in range(row + 1):
            answer = answer + combination(row, column)
        print(answer)

pascals_triangle(10)

答案 1 :(得分:0)

你的直接问题是这一行:

return combination(str(n-1, k-1)) + combination(str(n-1,k))

假设您想要保留大部分现有逻辑,最简单的解决方法是:

return str(int(combination(n - 1, k - 1)) + int(combination(n - 1, k)))

完整代码:

def combination(n, k):
    if k == 0 or k == n:
        return str(1)
    else:
        return str(int(combination(n - 1, k - 1)) + int(combination(n - 1, k)))

def pascals_triangle(rows):
    for row in range(rows):
        answer = ""

        for column in range(row + 1):
            answer += combination(row, column) + " "

        print(answer)

pascals_triangle(10)

<强>输出

1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 

更好的方法是意识到您不需要递归,也不需要combination()函数,而是重新计算从右到左行:

def pascals_triangle(rows):
    answer = []

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

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

        print(*answer)

输出

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1