Question

4. Pascal Triangle. Write a program pascal.py that builds and prints a two-dimensional list (a list of lists) a such that a[n][k contains the coefficient of the k-th term in the binomial expansion of (ty) These coefficients can be organized in a triangle, famously known as Pascals triangle. Every row in the triangle may be computed from the previous row by adding adjacent pairs of values together. Below is a sample invocation of the program s python pascal py 7 1 2 1 1 3 3 1 1:4 6 41 1 5:10 10 51 16 15 20 15 6 1 17 21 35 35 21 7 1 You program should contain the following finctions generate_pascal (n) wlich retunis a list of lists of the coelficients, ad print triangle(coeffs) which prints the corefficients one set per line


write this python program

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def generate_pascal(n):
line = [1]
a = []
for i in range(n):
a.append([])
a[i].append(1)
for j in range(1,i):
a[i].append(a[i-1][j-1]+a[i-1][j])
if(n>1):
a[i].append(1)
a[0]=[1]
return a


def print_triangle(coeffs):
for line in coeffs:
for num in line:
print(num,end=' ')
print()

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
write this python program 4. Pascal Triangle. Write a program pascal.py that builds and prints a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • How to write this code in python? please don't print the space for each row last number.

    how to write this code in python? please don't print the space for each row last number. Pascal's Triangle Pascal's Triangle is a number pattern with many interesting and useful properties. For example: Each number in the triangle is the sum of the two numbers above it. The number at row n and column k is equal to the binomial coefficient () "n choose k." 1 21 133 1 1 4 6 4 1 Write a program that prints Pascal's...

  • Write a program (in C or in any other language) that prints the first 64 rows...

    Write a program (in C or in any other language) that prints the first 64 rows of the Pascal triangle modulo 2. Specifically, the entry in row n and column k of the printout should be 0 if (_k^n) is even, and 1 otherwise. Submit both the program and the printout it produces.

  • use scheme program The following pattern of numbers is called Pascal's triangle. The numbers at the...

    use scheme program The following pattern of numbers is called Pascal's triangle. The numbers at the edge of the triangle are all 1, and each number inside the triangle is the sum of the two numbers above it. (pascals 0 0) → 1 (pascals 2 0) → 1 (pascals 2 1) → 2 (pascals 4 2) → 6 (printTriangle 5) 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 [5 marks] Write a procedure...

  • 1) Write a Python program that prompts the user to enter the current month name and...

    1) Write a Python program that prompts the user to enter the current month name and prints the season for that month. Hint: If the user enters March, the output should be "Spring"; if the user enters June, the output should be "Summer". 2 )Write a Python program using the recursive/loop structure to print out an equilateral triangle below (double spacing and one space between any two adjacent asterisks in the same row).          *      * * * *...

  • Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting...

    Using Python Please Question 1: Pine Tree Write a program that, prints a 'pine tree' consisting of triangles of increasing sizes, filled with a character (' oror '$' etc). Your program should consist of three functions: 1. A function print_shifted_triangle (n, m, symbol). It prints an n-line triangle, filled with symbol characters, shifted m spaces from the left margin. For example, if we call print_shifted_triangle (3, 4, +"), the expected output is: +++ Left margin +++++ 4 spaces 2. A...

  • // use "for i in range(): //use len. //basic python //discrete mathematics Write a program in...

    // use "for i in range(): //use len. //basic python //discrete mathematics Write a program in Python (in a .py file) that: Makes two lists of 50 numbers each, where the first list is multiples of 5, and the second list is the first 50 squares: 1, 4, 9, 16, 25, 36, ....up to 2500. then I want you to make a third list by multiplying each individual pair in the two lists, so for example: 5 times 1 is...

  • In python 3 What you need to do? 1. 2. 3. Create a function implementing the...

    In python 3 What you need to do? 1. 2. 3. Create a function implementing the logic to a Pascal triangle. (You can use any logic of your choice to generate the triangle) Read an integer input from the user, to determine the number of rows to be generated in a Pascal triangle. Call the function by passing the input as an argument to the function. Hint: You can use a list of lists to store the values of a...

  • Write a Python program that prints a name as shown in the image below. The program...

    Write a Python program that prints a name as shown in the image below. The program asks the user to enter a name and how many time the user wants to display the name.. You need two to input two integer values. First, for repeating name in a row. Second, for number of rows. In total, your program will have three inputs. Add three comment lines at start of the program, which shows program purpose, your name and date. Hint:...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • Python: Write a code to ask a number (one digit) from the user and prints the...

    Python: Write a code to ask a number (one digit) from the user and prints the respective row of the multiplication table. For example, if the user enters 5, the following lines will be printed. Note that, if the user enters any number outside the range of 1-9, the program should display an error message. 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT