TTN
TTN

Reputation: 25

My Python Pascal triangle (using binomial coefficients) code returns 2 terms per line. How do I fix this?

This is my code:

import math
def binomial(n,k):
if k == n:
    print(1) 
elif k == 1:         
    print(n)
elif k > n:          
    print(0)
else:
    a=math.factorial(n)
    b=math.factorial(k)
    c=math.factorial(n-k)
    BIN = int(a/(b*c))
    print(BIN, end='  ')
    #print ("Binomial of (n,k) is : ",BIN)
    return

def pascal(n):
list=[n]
for n in range(1,n+1):
    #print (list)
    list.append([])
    list[n].append(1)
    for k in range(0,n+1):
        list[n].append(binomial(n,k))
    #newlist.append(list[-1]) 
return

pascal(20)

'''for x in range (1,n+1):
    for y in range (0,x+1):
        print (binomial(x,y),)
    #print (' ')'''

x=int(input("enter n: "))
y=int(input("enter k: "))

binomial(n=x,k=y)

The terms returned are technically correct, but instead of forming a triangle shape, they were printed two terms each line. I would like to know how to fix the shape as appropriate.

Upvotes: 1

Views: 1723

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36682

It is not entirely trivial to construct a nice representation of Pascal triangle: Not only you need to get the correct calculations, but the justification and pagination is a bit tricky.

Here is a simple attempt, that maybe will satisfy you:

    import math

    def binomial(n, k):
        if k == n:
            return '1'
        elif k == 1:         
            return str(n)
        elif k > n:          
            return '0'
        else:
            a = math.factorial(n)
            b = math.factorial(k)
            c = math.factorial(n-k)
            return a // (b * c)

    def pascal(n):
        triangle = {}
        for n in range(n+1):
            triangle[n] = ['1']
            for k in range(1, n+1):
                triangle[n].append(str(binomial(n, k)))
        return triangle

    def find_max_width(triangle):
        size = 0
        k = None
        for key, val in triangle.items():
            if len(val) > size:
                k = key
        return sum([len(val) for val in triangle[k]]) + len(triangle[k])

    def print_triangle(triangle):
        max_width = find_max_width(triangle)
        for key in range(len(triangle)-1):
            line = ' '.join(triangle[key])
            k = ' ' + str(key) if len(str(key)) == 1 else str(key)
            print(k + ' ' * ((max_width - len(line)) // 2) + line)


    print_triangle(pascal(20))

The result looks like this:

 0                                                      1
 1                                                     1 1
 2                                                    1 2 1
 3                                                   1 3 3 1
 4                                                  1 4 6 4 1
 5                                                1 5 10 10 5 1
 6                                              1 6 15 20 15 6 1
 7                                             1 7 21 35 35 21 7 1
 8                                           1 8 28 56 70 56 28 8 1
 9                                         1 9 36 84 126 126 84 36 9 1
10                                     1 10 45 120 210 252 210 120 45 10 1
11                                   1 11 55 165 330 462 462 330 165 55 11 1
12                                 1 12 66 220 495 792 924 792 495 220 66 12 1
13                             1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1
14                         1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1
15                      1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1
16                  1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1
17              1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1
18           1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1
19       1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1

Upvotes: 1

Related Questions