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?

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
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by masterofpuppets
0

well I think you almost have it working...just a simple modification:

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]
>>>

Edited by masterofpuppets: n/a

0

:) just wanted to post another test result:

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]
>>>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.