Exercise 2. (Pascal's Triangle) Pascal's triangle Pn is a triangular array with n+1l rows, each listing the coefficients of the binomial expansion (x+ y)', where 0 sisn. For example, P4 is the triangular array: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 The term Pn(i, j) is calculated as Pn(i – 1, j – 1) +Pn(i – 1, j), where 0

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
**Exercise 2. (Pascal’s Triangle)**

Pascal’s triangle \( P_n \) is a triangular array with \( n+1 \) rows, each listing the coefficients of the binomial expansion \((x + y)^i\), where \( 0 \leq i \leq n \). For example, \( P_4 \) is the triangular array:

```
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
```

The term \( P_n(i, j) \) is calculated as \( P_n(i-1, j-1) + P_n(i-1, j) \), where \( 0 \leq i \leq n \) and \( 1 \leq j \leq i \), with \( P_n(i, 0) = P_n(i, i) = 1 \) for all \( i \). Write a program called `pascal.py` that accepts \( n \) (int) as a command-line argument, and writes \( P_n \) to standard output.

---

**Project 3 (Mozart Waltz Generator)**

```
> ~/workspace/project3
$ python3 pascal.py 10
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
```

```python
# pascal.py

import stdarray
import stdio
import sys

# Accept n (int) as command-line argument.
...

# Setup a 2D ragged list a of integers. The list must have n + 1 rows, with the ith (0 <= i <= n) row
# having i + 1 elements, each initialized to 1. For example, if n = 3, a should be initialized to
#
Transcribed Image Text:**Exercise 2. (Pascal’s Triangle)** Pascal’s triangle \( P_n \) is a triangular array with \( n+1 \) rows, each listing the coefficients of the binomial expansion \((x + y)^i\), where \( 0 \leq i \leq n \). For example, \( P_4 \) is the triangular array: ``` 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 ``` The term \( P_n(i, j) \) is calculated as \( P_n(i-1, j-1) + P_n(i-1, j) \), where \( 0 \leq i \leq n \) and \( 1 \leq j \leq i \), with \( P_n(i, 0) = P_n(i, i) = 1 \) for all \( i \). Write a program called `pascal.py` that accepts \( n \) (int) as a command-line argument, and writes \( P_n \) to standard output. --- **Project 3 (Mozart Waltz Generator)** ``` > ~/workspace/project3 $ python3 pascal.py 10 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1 1 10 45 120 210 252 210 120 45 10 1 ``` ```python # pascal.py import stdarray import stdio import sys # Accept n (int) as command-line argument. ... # Setup a 2D ragged list a of integers. The list must have n + 1 rows, with the ith (0 <= i <= n) row # having i + 1 elements, each initialized to 1. For example, if n = 3, a should be initialized to #
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education