With with Pascals Triangle Program

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

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

With with Pascals Triangle Program

 
0
  #1
Nov 3rd, 2009
I am writing a program in python where in the function I input a row in Pascals triangle and the function returns the NEXT row in the triangle. Below is what I have so far and am getting really frustrate...I'm new at this and bad at iterating things. Can you help me finish my program?

  1. def pascnext(L):
  2. currentrow = []
  3. if L == []:
  4. return [1]
  5. if L == [1]:
  6. return [1,1]
  7. else:
  8. for i in L:
  9. currentrow[i] = L[i] + L[i-1]
  10. return currentrow
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 60
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #2
Nov 3rd, 2009
well I think you almost have it working...just a simple modification:

  1. def pascnext( l ):
  2. currentrow = []
  3. #if L == []:
  4. if len( l ) == 0:
  5. return [1]
  6. #if L == [1]:
  7. if len( l ) == 1 and l[ 0 ] == 1:
  8. return [ 1, 1 ]
  9. else:
  10. #for i in L:
  11. #currentrow[i] = L[i] + L[i-1]
  12. #return currentrow
  13. #Note that here you cannot select currentrow[ i ] because it has no elements yet
  14. currentrow += [ 1 ]
  15. for i in range( 0, len( l ) - 1 ):
  16. currentrow += [ l[ i ] + l[ i + 1 ] ]
  17. currentrow += [ 1 ]
  18.  
  19. return currentrow
  20.  
  21. print pascnext( [ 1, 5, 10, 10, 5, 1 ] )
  22.  
  23. >>>
  24. [1, 6, 15, 20, 15, 6, 1]
  25. >>>
Last edited by masterofpuppets; Nov 3rd, 2009 at 6:34 pm.
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 243
Reputation: masterofpuppets is an unknown quantity at this point 
Solved Threads: 60
masterofpuppets's Avatar
masterofpuppets masterofpuppets is offline Offline
Posting Whiz in Training
 
0
  #3
Nov 3rd, 2009
just wanted to post another test result:

  1. l = []
  2. for n in range( 10 ):
  3. next = pascnext( l )
  4. print next
  5. l = next
  6.  
  7. >>>
  8. [1]
  9. [1, 1]
  10. [1, 2, 1]
  11. [1, 3, 3, 1]
  12. [1, 4, 6, 4, 1]
  13. [1, 5, 10, 10, 5, 1]
  14. [1, 6, 15, 20, 15, 6, 1]
  15. [1, 7, 21, 35, 35, 21, 7, 1]
  16. [1, 8, 28, 56, 70, 56, 28, 8, 1]
  17. [1, 9, 36, 84, 126, 126, 84, 36, 9, 1]
  18. >>>
My site ->> http://8masterofpuppets8.webs.com/
"My belief is stronger than your doubt."
Reply With Quote Quick reply to this message  
Reply

Message:




Views: 271 | Replies: 2
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC