Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[[ PYTHON ]] Write a program to create Pascal\'s triangle (but with a twist!). T

ID: 3857884 • Letter: #

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 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

Explanation / Answer

Please follow the below code to get triangle

pascals_triangle = []

def blank_list_gen(x):

while len(pascals_triangle) < x:

pascals_triangle.append([0])

def pascals_tri_gen(rows):

blank_list_gen(rows)

for element in range(rows):

count = 1

while count < rows - element:

pascals_triangle[count + element].append(0)

count += 1

for row in pascals_triangle:

row.insert(0, 1)

row.append(1)

pascals_triangle.insert(0, [1, 1])

pascals_triangle.insert(0, [1])

pascals_tri_gen(6)

for row in pascals_triangle:

print(row)

def gen(n,r=[]):

for x in range(n):

l = len(r)

r = [1 if i == 0 or i == l else r[i-1]+r[i] for i in range(l+1)]

yield

--------------------

# call the function ! Indent properly , everything should be inside the function

def triangle():

matrix=[[0 for i in range(0,20)]for e in range(0,10)] # This method assigns 0's to all Rows and Columns , the range is mentioned

div=20/2 # it give us the most middle columns

matrix[0][div]=1 # assigning 1 to the middle of first row

for i in range(1,len(matrix)-1): # it goes column by column

for j in range(1,20-1): # this loop goes row by row

matrix[i][j]=matrix[i-1][j-1]+matrix[i-1][j+1] # this is the formula , first element of the matrix gets , addition of i index (which is 0 at first ) with third value on the the related row

# replacing 0s with spaces :)

for i in range(0,len(matrix)):

for j in range(0,20):

if matrix[i][j]==0: # Replacing 0's with spaces

matrix[i][j]=" "

for i in range(0,len(matrix)-1): # using spaces , the triangle will printed beautifully

for j in range(0,20):

print 1*" ",matrix[i][j],1*" ", # giving some spaces in two sides of the printing numbers

triangle() # calling the function

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote