Pascal triangle in python

So, I am making a Pascal triangle, and I cannot understand why this code does not work. It prints something like this

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

This is almost correct, but it seems that the values ​​seem to be too large, so 1,2, If you consider 2 as the first row and 1 as the 0th row, 2 - 1 is too high because it is in the first row, it should be 1.1. The next line should be 1,2,1, the first value is correct, and the next is 1 too high value, and one after that - 2 values ​​are too high.

I tried doing something like append (ai) but it doesn't seem to work, how can you print this correctly?

 def triangle(rows): for rownum in range (rows): newValue=1 PrintingList = list() for iteration in range (rownum): newValue = newValue * ( rownum-iteration ) * 1 / ( iteration + 1 ) PrintingList.append(int(newValue)) print(PrintingList) print() 

Thanks in advance.

+4
source share
5 answers

I would change PrintingList = list() to PrintingList = [newValue] .

triangle(10) then you will get the following:

 [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] 

This is a valid Pascal triangle.

540px-Pascal% 27s_triangle_5.svg.png

+10
source

In my code below are print instructions to help show how this works. I run it in an IPython laptop. My conclusion is as follows:

  [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] 

Besides the fact that the output looks as it should, the calculation method actually follows the original definition of the Pascal Triangle, where each line is built from the one that was above. (Some methods use combinatorics / factorials that can give the correct answer, but do not follow the original spirit of Pascal Triangle, afaik.)

Here is the code:

 def MakeTriangle(numberOfRows): """the print statements are not required. They are just to help see how it works.""" triangle=[[1]] #base case (ie, 0th row) print 'triangle[-1] is ', triangle[-1] for _x in range(numberOfRows - 1): #_x isn't used zipperd = zip([0] + triangle[-1], triangle[-1] + [0]) print "zipperd: ", zipperd newRow = map(sum, zipperd) triangle.append(list(newRow)) print 'triangle[-1] is ', triangle[-1] #to help see how tri is built print "\n\n" return triangle for row in MakeTriangle(5): print('{0:^50}'.format(row)) 

To begin to understand how this works, pay attention to the following simple cases of the above zip and map functions:

 x = [1, 2, 3] y = [4, 5, 6] zipped = zip(x, y) print zipped #output is [(1, 4), (2, 5), (3, 6)] print map(sum, zipped) #output is [5, 7, 9] 

Using the print statements in the MakeTriangle function, you get this output to help you learn how the code works:

 triangle[-1] is [1] zipperd: [(0, 1), (1, 0)] triangle[-1] is [1, 1] zipperd: [(0, 1), (1, 1), (1, 0)] triangle[-1] is [1, 2, 1] zipperd: [(0, 1), (1, 2), (2, 1), (1, 0)] triangle[-1] is [1, 3, 3, 1] zipperd: [(0, 1), (1, 3), (3, 3), (3, 1), (1, 0)] triangle[-1] is [1, 4, 6, 4, 1] 

Read the docs below looking at this conclusion and this will start to make sense:

If this still doesn't make sense, stick with these print statements in a for MakeTriangle loop:

  print "[0] + triangle[-1] is ", [0] + triangle[-1] print "triangle[-1] + [0] is ", triangle[-1] + [0] 

Note that aligned (centered) printing is done using the examples from http://docs.python.org/2/library/string.html#format-examples .

See also fooobar.com/questions/1314106 / ...

+2
source

I get an error, but you should try something like this:

 row1 = whatever pascal = [[row/(colum*(row-colum) for row in range(0, colum)] for row in range(1, row1)]] 
+1
source

I would look:

http://en.wikipedia.org/wiki/Pascal%27s_triangle#Calculating_an_individual_row_or_diagonal_by_itself

which describes how to calculate the lines of a triangle one by one.

0
source
 import math coef = 1 rows = int(input("Enter the number of rows :")) stringVal = "" for i in range(rows): for space in range(1,rows - i + 1): stringVal = stringVal + " " for j in range(0,i + 1): if(i == 0 or j == 0): coef = 1 else: coef = coef * (i - j + 1) / j temp = coef TotalSpace=0 while(temp != 0): TotalSpace = TotalSpace + 1 temp = int(math.floor(temp / 10)) p=0 while((p+TotalSpace)!=4): stringVal = stringVal + " " p=p+1 stringVal = stringVal + str(int(math.floor(coef))) stringVal = stringVal + "\n" print(stringVal) 
0
source

Source: https://habr.com/ru/post/1314104/


All Articles