Found this awesome code for pascal's triangle ... can someone explain it line by line to me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Found this awesome code for pascal's triangle ... can someone explain it line by line to me?

n=int(input("Enter number of rows: ")) a=[] for i in range(n+1): a.append([]) a[i].append(1) for j in range(1,i): a[i].append(a[i-1][j-1]+a[i-1][j]) if(n!=0): a[i].append(1) for i in range(n+1): print(" "*((n+1)-i),end=" ",sep=" ") for j in range(0,i+1): print('{0:6}'.format(a[i][j]),end=" ",sep=" ") print()

28th Mar 2018, 4:26 PM
Nitay Eshed
Nitay Eshed - avatar
1 Answer
+ 2
It is basically as follows: put 1 in the line while the line needs more numbers add the two numbers from the previous line for the next number on this one. if we need more than one number on this line finish it with a 1
28th Mar 2018, 5:05 PM
John Wells
John Wells - avatar