1

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
8
  • 1
    Your output looks pretty good to me, actually. Can you supply an example of what you want it to look like? Commented Apr 3, 2015 at 11:58
  • 1
    centered on what? what is your maximal/required line length? Please show us what would a "perfect" spacing would be :) Commented Apr 3, 2015 at 12:00
  • I think the bottom part of the triangle is shifting a bit to the right... Commented Apr 3, 2015 at 12:07
  • 2
    Do you have probem with 6 and 28? In monospaced font is not possible to center them - there is no "half" whitespace character to shift it. Commented Apr 3, 2015 at 12:09
  • I have a problem with 6, 10 , 15, 28. They are a bit right as compared to the triangle.. Commented Apr 3, 2015 at 12:11

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

if you want can use the library from python like math ...
This solution is only valid if n<=5, but it is still interesting that the math operations resemble each other.
If this solution is peformed in an alien arithmetic that carry will not be performed and each digit keeps glowing, I think the solution will hold even if n>5.
0
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)

1 Comment

Hello, and welcome to Stack Overflow. When posting an answer, try to not only post code, but rather an explanation of the code as well. Also, make sure that you look through the other answers as well before posting your answer to make sure you are contributing something new to the discussion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.