pascal triangle using lists

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Feb 2009
Posts: 2
Reputation: memphis1234 is an unknown quantity at this point 
Solved Threads: 0
memphis1234 memphis1234 is offline Offline
Newbie Poster

pascal triangle using lists

 
0
  #1
Feb 12th, 2009
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
  1. numStr = raw_input("Please input the height of Pascal's Triangle: ")
  2. row = int(numStr)
  3.  
  4. tri = []
  5.  
  6. row1 = [1]
  7. row2 = [1, 1]
  8.  
  9. tri.append(row1)
  10. tri.append(row2)
  11.  
  12. while len(tri) < row:
  13. print tri
  14. break
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 2
Reputation: memphis1234 is an unknown quantity at this point 
Solved Threads: 0
memphis1234 memphis1234 is offline Offline
Newbie Poster

Re: pascal triangle using lists

 
0
  #2
Feb 12th, 2009
updated. Not sure what I'm doing, though.

  1. numStr = raw_input("Please input the height of Pascal's Triangle: ")
  2. height = int(numStr)
  3.  
  4. tri = []
  5.  
  6. row1 = [1]
  7. row2 = [1, 1]
  8. tri.append(row1)
  9. tri.append(row2)
  10.  
  11. while len(tri) < height:
  12. newRow = [1]
  13. oldRow = tri[-1]
  14. start = 0
  15. while start < (3-2):
  16. newRow.append(oldRow[start] + oldRow[1])
  17. start += 1
  18. newRow.append(1)
  19.  
  20.  
  21. print tri
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,045
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 294
woooee woooee is offline Offline
Veteran Poster

Re: pascal triangle using lists

 
0
  #3
Feb 12th, 2009
You should increment height, otherwise it's an infinite loop. In general you want to use a for() or while() loop and append to a list on each loop. Add print statements to print what you don't get (especially 'height' and 'start').
Last edited by woooee; Feb 12th, 2009 at 11:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 48
Reputation: yilmazhuseyin is an unknown quantity at this point 
Solved Threads: 5
yilmazhuseyin's Avatar
yilmazhuseyin yilmazhuseyin is offline Offline
Light Poster

Re: pascal triangle using lists

 
0
  #4
Feb 13th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC