944,030 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 707
  • Python RSS
Nov 3rd, 2009
0

With with Pascals Triangle Program

Expand Post »
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?

Python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
frank_albers123 is offline Offline
2 posts
since Nov 2009
Nov 3rd, 2009
0
Re: With with Pascals Triangle Program
well I think you almost have it working...just a simple modification:

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009
Nov 3rd, 2009
0
Re: With with Pascals Triangle Program
just wanted to post another test result:

Python Syntax (Toggle Plain Text)
  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. >>>
Reputation Points: 20
Solved Threads: 74
Posting Whiz in Training
masterofpuppets is offline Offline
272 posts
since Jul 2009

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: Pygame question2
Next Thread in Python Forum Timeline: File to dictionary





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


Follow us on Twitter


© 2011 DaniWeb® LLC