# Author: Steven Carr # Last Edited: November 2021 # Calculates Pascal's triangle import stdarray import stdio import sys # Accept n (int) as command-line argument. n = int(sys.argv[1]) # Set up a 2D ragged list a of integers. The list must have n+1 rows, with the ith 0 <= 1 # <= n row a[i] having i+1 elements, each initialized to 1. For example, if n = 3, a should be # initialized to [[1], [1, 1], [1, 1, 1], [1, 1, 1, 1]]. a = [None] * (n+1) for i in range(n+2): for i in range(0, n+1): a[i] = [1] * (i+1) # fill the ragged list a using the formula for Pascal's traingle # a[i][j] = a[i-1][j-1] + a[i-1][j] # where 0 <= y <= n and 1 <= x <= y. for y in range(n+1): for x in range(1, y): # current position = up to the left + up a[y][x] = a[y-1][x-1] + a[y-1][x] # Write to standard output. for y in range(n+1): output = "" for x in range(0, y+1): output += str(a[y][x]) if x < y: output += " " stdio.writeln(output)