| | |
With with Pascals Triangle Program
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
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)
def pascnext(L): currentrow = [] if L == []: return [1] if L == [1]: return [1,1] else: for i in L: currentrow[i] = L[i] + L[i-1] return currentrow
0
#2 Nov 3rd, 2009
well I think you almost have it working...just a simple modification:
Python Syntax (Toggle Plain Text)
def pascnext( l ): currentrow = [] #if L == []: if len( l ) == 0: return [1] #if L == [1]: if len( l ) == 1 and l[ 0 ] == 1: return [ 1, 1 ] else: #for i in L: #currentrow[i] = L[i] + L[i-1] #return currentrow #Note that here you cannot select currentrow[ i ] because it has no elements yet currentrow += [ 1 ] for i in range( 0, len( l ) - 1 ): currentrow += [ l[ i ] + l[ i + 1 ] ] currentrow += [ 1 ] return currentrow print pascnext( [ 1, 5, 10, 10, 5, 1 ] ) >>> [1, 6, 15, 20, 15, 6, 1] >>>
Last edited by masterofpuppets; Nov 3rd, 2009 at 6:34 pm.
0
#3 Nov 3rd, 2009
just wanted to post another test result: Python Syntax (Toggle Plain Text)
l = [] for n in range( 10 ): next = pascnext( l ) print next l = next >>> [1] [1, 1] [1, 2, 1] [1, 3, 3, 1] [1, 4, 6, 4, 1] [1, 5, 10, 10, 5, 1] [1, 6, 15, 20, 15, 6, 1] [1, 7, 21, 35, 35, 21, 7, 1] [1, 8, 28, 56, 70, 56, 28, 8, 1] [1, 9, 36, 84, 126, 126, 84, 36, 9, 1] >>>
![]() |
Similar Threads
- Sierpinski triangle program (Java)
- Pascal Triangle program that needs to convert from C to C# (C)
- Pascals triangle (Functions) (C++)
- pascal triangle program (C++)
- Triangle Program Help! (C++)
- pascals triangle (C++)
Other Threads in the Python Forum
- Previous Thread: Pygame question2
- Next Thread: File to dictionary
Views: 271 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
advanced anydbm app bash beginner bits calling chmod cmd code data dictionary directory dynamic edit examples excel external feet file float format ftp function gui homework http i/o images import info input ip itunes java keycontrol line linux list lists loan loop maintain millimeter mouse newb number numbers output panel parsing path port prime print program programming projects push py-mailer py2exe pygame pyqt python queue random rational recursion recursive scrolledtext smtp split ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable variables ventrilo web webservice whileloop windows wxpython xlib





