I was thinking of the following problem: List all sequences \$ a_1, a_2, a_3, a_4, a_5, a_6, a_7 \$ of positive integers where \$ 1 \le a_1 < a_2 < \ldots < a_7 \le 39 \$. For any sequence with this property, my code computes the next sequence in lexicographic order. Starting with the initial sequence \$ [1,2,3,4,5,6,7] \$, calling next_row(a)
repeatedly produces all such sequences.
Is there more elegant way to code the function that returns the next row than this?
def next_row(a):
if a[-1]<39:
a[-1]+=1
return a
elif a[-2]<38:
a[-2]+=1
a[-1]=a[-2]+1
return a
elif a[-3]<38:
a[-3]+=1
a[-2]=a[-3]+1
a[-1]=a[-2]+1
return a
elif a[-4]<37:
a[-4]+=1
a[-3]=a[-4]+1
a[-2]=a[-3]+1
a[-1]=a[-2]+1
return a
elif a[-5]<36:
a[-5]+=1
a[-4]=a[-5]+1
a[-3]=a[-4]+1
a[-2]=a[-3]+1
a[-1]=a[-2]+1
return a
elif a[-6]<35:
a[-6]+=1
a[-5]=a[-6]+1
a[-4]=a[-5]+1
a[-3]=a[-4]+1
a[-2]=a[-3]+1
a[-1]=a[-2]+1
return a
elif a[-7]<34:
a[-7]+=1
a[-6]=a[-7]+1
a[-5]=a[-6]+1
a[-4]=a[-5]+1
a[-3]=a[-4]+1
a[-2]=a[-3]+1
a[-1]=a[-2]+1
return a