| | |
pascal triangle using lists
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
I have to write a program which generates a pascal's triangle.
The user inputs the height of the triangle and it supposed to generate one.
This is what I have so far and I don't know how you would keep
adding rows after the second one. I'm so lost
The user inputs the height of the triangle and it supposed to generate one.
This is what I have so far and I don't know how you would keep
adding rows after the second one. I'm so lost
python Syntax (Toggle Plain Text)
numStr = raw_input("Please input the height of Pascal's Triangle: ") row = int(numStr) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) < row: print tri break
•
•
Join Date: Feb 2009
Posts: 2
Reputation:
Solved Threads: 0
updated. Not sure what I'm doing, though.
python Syntax (Toggle Plain Text)
numStr = raw_input("Please input the height of Pascal's Triangle: ") height = int(numStr) tri = [] row1 = [1] row2 = [1, 1] tri.append(row1) tri.append(row2) while len(tri) < height: newRow = [1] oldRow = tri[-1] start = 0 while start < (3-2): newRow.append(oldRow[start] + oldRow[1]) start += 1 newRow.append(1) print tri
I can see two way to solve the problem
1) use two dimentional list.
2) use two one dimentional list to store current row and previous row.
(that is what you are doing.)
in your case you use only previous row to calculate current row. so in start, your previous row will be row 1 which stores only 1
and you calculate current row by using currentrow[i] = previousrow [i] + previous row[i-1] in a loop. (first element and last element in list are special cases.) after the loop you add currentrow to triangle list and make copy currentrow to previousrow and calculate next row.
I hope that would help.
1) use two dimentional list.
2) use two one dimentional list to store current row and previous row.
(that is what you are doing.)
in your case you use only previous row to calculate current row. so in start, your previous row will be row 1 which stores only 1
and you calculate current row by using currentrow[i] = previousrow [i] + previous row[i-1] in a loop. (first element and last element in list are special cases.) after the loop you add currentrow to triangle list and make copy currentrow to previousrow and calculate next row.
I hope that would help.
![]() |
Other Threads in the Python Forum
- Previous Thread: 'file' object is not callable
- Next Thread: Designing a gui library for Python
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary drive dynamic error examples excel exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial twoup ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






