| | |
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 apache approximation array assignment backend beginner binary book builtin calculator chmod code converter countpasswordentry curved customdialog dan08 dictionaries dictionary drive dynamic examples excel file filename float format function gui heads homework import inches input java launcher library line lines linux list lists loop mouse mysql mysqlquery number numbers numeric output parsing path phonebook plugin pointer port prime programming progressbar projects py2exe pygame pysimplewizard python random recursion redirect scrolledtext software statictext statistics string strings sum table terminal text textarea thread threading time tkinter tlapse trick tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable windows wordgame write wxpython






