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

## All 2 Replies

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

:) 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]
>>>``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.