I'm trying to participate in a coding challenge, and I have part of the code complete, but now I need a function to list all multiples of 3 in the previous output in descending order.
Is there a function you could do that would run all the previous numbers printed in the triangle and divide it by 3,and then print the numbers that came out as a whole number from that calculation? Or some other similar task to come up with all the multiples of 3 up to line 50 in Pascal's Triangle?
def generate_pascals_triangle(rows):
triangle = [[1], [1, 1]]
if rows == 1:
return triangle[0]
else:
for row_number in range(2, rows):
triangle.append([1]*row_number)
for number in range(1, row_number):
triangle[row_number][number] = (triangle[row_number-1][number-1]+triangle[row_number-1][number])
triangle[row_number].append(1)
return triangle
def difference_between_rows(row, next_row):
row_len = 0
next_row_len = 0
for number in row:
string_number = str(number)
row_len += (len(string_number)+1)
for number in next_row:
string_number = str(number)
next_row_len += (len(string_number)+1)
return (next_row_len-1) - (row_len-1)
def print_pascals_triangle(triangle):
largest_element = triangle[-1][len(triangle[-1]) // 2]
element_width = len(str(largest_element))
def format_row(row):
return ' '.join([str(element).center(element_width) for element in row])
triangle_width = len(format_row(triangle[-1]))
for row in triangle:
print(format_row(row).center(triangle_width))
if __name__ == '__main__':
rows = 51
print_pascals_triangle(generate_pascals_triangle(rows))