Reputation: 53
I'm making a Pascal's triangle and I can't figure out why this code isn't working. It prints out 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]
Which is almost correct, however it seems that the values seem to be the row number too high, so 1,2 , If you count 2 as the first row and 1 as the 0th row, 2 is 1 value too high since it is in the first row, it should be 1,1. The next row should be 1,2,1, the first value is correct but the next one is 1 value too high and the one after that is 2 values too high.
I tried doing something like append(a-i), but it doesn't seem to work. How would I get this to print 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()
Upvotes: 4
Views: 27704
Reputation: 11
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)]]
Upvotes: 1
Reputation:
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)
Upvotes: 0
Reputation: 6337
My code below has some print statements that help show how it works. I'm running it in an IPython notebook. My output looks like this:
[1]
[1, 1]
[1, 2, 1]
[1, 3, 3, 1]
[1, 4, 6, 4, 1]
Beyond the fact that the output looks like it should, the computation method actually follows the original definition of Pascal's Triangle where each row is built from the one above. (Some methods use combinatorics/factorials, which might give the right answer, but don't follow the original spirit of Pascal's Triangle, afaik.)
Here's the code:
def MakeTriangle(numberOfRows):
"""the print statements are not required. They are just to help see how it works."""
triangle=[[1]] #base case (i.e., 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 it works, note the following simple cases of the above functions zip
and map
:
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]
With the print statements in place in the MakeTriangle function, you will get this output to help you further see 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 while looking at this output and it will begin to make sense:
If it still doesn't make sense, stick these print statements in the for
loop of MakeTriangle:
print "[0] + triangle[-1] is ", [0] + triangle[-1]
print "triangle[-1] + [0] is ", triangle[-1] + [0]
Note that the aligned (centered) printing is accomplished using the examples from http://docs.python.org/2/library/string.html#format-examples.
See also https://stackoverflow.com/a/19895685/463994
Upvotes: 2
Reputation: 21089
I would change PrintingList = list()
to PrintingList = [newValue]
.
triangle(10)
then gives you 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]
Which is a valid Pascal's triangle.
Upvotes: 10