-2

I'm trying to write a program to print a pascal's triangle. Here's my code:

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)

This is what I was given to work with (this is for an assignment):

# 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)

It is supposed to print this:

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

I know the problem is in the combination function, but every time I try to fix something I get more errors. This currently gets me this error:

TypeError: str() argument 2 must be str, not int

I'm very much a beginner, so it's likely I'm missing some other stuff too. Can I have help fixing this error and anything else I'm missing?

Mikona
  • 13
  • 1
  • 5
  • 1
    What's `str(n-1, k-1)`?? in `combination`? – ForceBru Dec 24 '17 at 19:25
  • What specific problem(s) are you having with your code? –  Dec 24 '17 at 19:25
  • @ForceBru It's supposed to be part of the equation for the Pascal's triangle. – Mikona Dec 24 '17 at 19:29
  • @Mikona, yeah, but what is it doing exactly? – ForceBru Dec 24 '17 at 19:29
  • @Blurp I added what I'm having trouble with. – Mikona Dec 24 '17 at 19:30
  • @ForceBru Yeah, it doesn't make much sense now that I'm looking at it. I guess I need another way to get n and k for the equation? – Mikona Dec 24 '17 at 19:31
  • @Mikona, the error message is pretty clear: `TypeError: str() argument 2 must be str, not int`. Please __read the documentation__ about `str` in Python. Is this really what you want to do? Isn't this just a _typo_? – ForceBru Dec 24 '17 at 19:32
  • @ForceBru I'm sorry, I'm just a high school student, I really don't know much. We were given a base code to work off of for this. def pascals_triangle is how it was given to me, but we are supposed to write the code for def combination. Would it help if I posted the base code? – Mikona Dec 24 '17 at 19:37

2 Answers2

0

For every new level, insert one on each side and insert the sum of each pair of the above level in the middle entries.

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

I'm really not sure what you're going for in the code you wrote. I tried to doctor it but I don't understand what you want to achieve with this combination function.

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)
kilojoules
  • 9,768
  • 18
  • 77
  • 149
0

Your immediate problem is this line:

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

Assuming you want to preserve most of your existing logic, the simplest fix is to instead do:

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

Complete code:

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)

OUTPUT

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 

A better approach is to realize that you don't need recursion, nor the combination() function, but instead recompute the line right to left:

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)

OUTPUT

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
cdlane
  • 40,441
  • 5
  • 32
  • 81