Question: [[ PYTHON ]] Write a program to create Pascal's triangle (but with a twist!). The first five rows are shown below. On row 0, write

[[ PYTHON ]] Write a program to create Pascal's triangle (but with a twist!). The first five rows are shown below.

On row 0, write only the number 1. Then, for the next row, add the number directly above and to the left with the number directly above and to the right to find the new value. If the number to the right or left is not present, substitute a 0 in its place. For example, the first number in row 1 is 0 + 1 = 1, whereas the numbers 1 and 3 in row 3 are added to give the number 4 in row 4.

Hint: Use a list for each row, and use a list of lists to hold the whole triangle. The position of each number in the triangle can be translated to indices in the lists. Keep placeholders in the list to represent "empty" cells in each row. Only every second entry would actually contain a number. This makes it easy to find the left and right parent of each cell. The output of each line must be centered.

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!