hmm
hmm

Reputation: 47

Python - Formatting my pascal triangle

I have written a code to generate the pascal triangle. The following is a function from my code that deals with the spacing of the pascal triangle.

def print_pascals_triangle(triangle):
    largest_element = triangle[-1][len(triangle[-1]) // 2]
    element_width = len(str(largest_element))
    def format_row(row):
        return ' '.join([str(element).center(element_width) for element in row])
    triangle_width = len(format_row(triangle[-1]))
    for row in triangle:
        print(format_row(row).center(triangle_width))

this gives me the following output:

Enter the number of rows you want in your Pascal`s 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     

As you can see the spacing is not perfect and my pascal triangle is not centered. How do i perfectly center my pascal triangle. Any help/tips is much appreciated. Thanks!

THE PERFECT PASCAL TRIANGLE!

                     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

Upvotes: 1

Views: 1165

Answers (3)

Arman Memari
Arman Memari

Reputation: 1

a = int(input())
def print_pascals_triangle(n):
    for i in range(n):
        print('  '*(n-i-1), end='  ')
        c = 1
        for j in range(1, i+2):
            print(f'{c:3}', end=' ')
            c = c * (i + 1 - j) // j
        print()
rows = a
print_pascals_triangle(rows)

Upvotes: 0

Shima Fallah
Shima Fallah

Reputation: 66

If you pay attention, this triangle is obtained from the square of the number eleven

so you can use this code

n=5

for i in range(n):
    print(' '*(n-i), end='')
    print(' '.join(str(11**i)))

Upvotes: 1

Kamaljot Singh
Kamaljot Singh

Reputation: 31

Think of the length of a single output and the number of spaces before it as constant being equal to the twice the length of the lengthiest output.

You want 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

The lengthiest output is 10, and no. of spaces given to each output 4. 10 has 2 spaces before it, and 5 has 3, keeping it 'centered'.

Keeping this in mind, here is the code:

import math as mt

def combine(n,r):
    ncr=(mt.factorial(n))/((mt.factorial(r))*(mt.factorial(n-r)))
    return ncr

def pascal():
  i=int(input("Enter index: "))
  z=len(str(int(combine(i,(i//2)))))
  nosp=z*i

  for k in range(0,i+1):
    sttr=''
    for l in range(0,k+1):
        sttr+=(" "*(2*z-len(str(int(combine(k,l))))))+str(int(combine(k,l)))
    isp=len(str(i))-len(str(k))+1
    print(isp*" "+str(k)+": "+nosp*" "+sttr)
    nosp-=z

print("Pascal's Triangle.")
pascal()
print("Done!")

It will produce the following output:

Pascal's Triangle.
Enter index: 10
  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
Done!

You can modify this code if you do't want that extra index numbering print statements.

I know, my code is a mess and hard to understand. I'm new to programming :)

Upvotes: 3

Related Questions