| | |
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: 273 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
anti approximation array avogadro beginner builtin cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format frange ftp function gui homework import input java lapse library line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect script scrolledtext singleton socket sqlite ssh stderr string strings subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wxpython





