943,923 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 3551
  • Python RSS
Feb 12th, 2009
0

pascal triangle using lists

Expand Post »
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
python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
memphis1234 is offline Offline
2 posts
since Feb 2009
Feb 12th, 2009
0

Re: pascal triangle using lists

updated. Not sure what I'm doing, though.

python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
memphis1234 is offline Offline
2 posts
since Feb 2009
Feb 12th, 2009
0

Re: pascal triangle using lists

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.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Feb 13th, 2009
0

Re: pascal triangle using lists

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.
Reputation Points: 31
Solved Threads: 5
Light Poster
yilmazhuseyin is offline Offline
48 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: 'file' object is not callable
Next Thread in Python Forum Timeline: Designing a gui library for Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC