Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Generate Pascal's Triangle for the number of rows given
def gen_pascals_triangle(rows):
pascals_triangle = [[1, 1]]
for row in range(1, rows):
curr_row = [1, 1]
prev_row = pascals_triangle[row - 1]
for num in range(0, len(prev_row) - 1):
num_above = prev_row[num]
other_num_above = prev_row[num + 1]
curr_num = num_above + other_num_above
curr_row.insert(num + 1, curr_num)
pascals_triangle.append(curr_row)
return pascals_triangle
def main():
pascals_triangle = gen_pascals_triangle(10)
for row in pascals_triangle:
print(row)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment