Python Programming Section 1736 & 1737 Deadline April 14, 2019 by 23:59 PM PST
Programming Assignment 4 (50 Points): More Fun With Functions: 1D & 2D Lists
Assignment Description:
The fourth assignment will use Python lists and the def keyword to create functions that work on 1D and 2D lists. In mathematics, a 2D list of numbers is called a matrix which is assembled into a table of rows and columns. In Python, a matrix can be created using 2D lists, also called a two-dimensional table.
For this assignment, we will create five functions. One to compute the prefix sum of a 1D list and four more that operate on 2D lists. Note that all five functions return a value and are described below with their respective example usage.
Program 1: Computing the Prefix Sum
The prefix sum of a 1D list of numbers is a new list where each entry is computed by adding adjacent elements of the input list. For example, the prefix sum of a list of numbers a1,a2,a3,… is the list b where:
b0 = a0
b1 = a0 + a1
b2 = a0 + a1 + a2
…
For example, the prefix sum of A is shown below:
A=⇥0 1 2 3 4 5 6⇤ prefixsum(A)=⇥0 1 3 6 10 15 21⇤
Write a function called prefix sum(A) that returns a new list with the prefix sum of list A. The original list A must not be modified. See the usage for examples of how prefix sum(. . . ) works.
Example usage of prefix sum(. . . ):
>>> x = prefix_sum([0,1,2,3,4,5,6]) [0, 1, 3, 6, 10, 15, 21]
>>> x = prefix_sum([0,10,20,30,40,50]) [0, 10, 30, 60, 100, 150]
>>> x = prefix_sum([0,-10,10,20,-20,100]) [0, -10, 0, 20, 0, 100]
Reference: https://en.wikipedia.org/wiki/Prefix_sum
Assistant Professor: Scott Bishop, PhD SMC CS87A, Spring 2019 1
Program 2: The Diagonal of a Matrix (2D List)
The diagonal of a square matrix (same number of rows and columns) are the values contained where the row index i and column index j are the same. For example, in matrix A below, the diagonal is shown as
diag(A):
261 2 3 437 ⇥
A=645 6 7 875diag(A)= 1 6 11 16
9 101112 13 14 15 16
⇤
Write a function called diagonal that accepts one argument, a square 2D matrix, and returns the diagonal of the matrix.
Example usage of diagonal:
>>> m8 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] >>> m9 = [[1, 2], [3, 4]]
>>> diagonal(m8)
[1, 5, 9]
>>> diagonal(m9) [1, 4]
Program 3: Matrix (2D List) Symmetry
A symmetric matrix is a square matrix (same number of rows and columns) where the values at row index i and column index j are the same as the values at row index j and column index i. For example, matrix A below is symmetric, where as matrix B is not because the value 2 at index (1,0) does not equal the value 4 at index (0, 1).
241 2 735 241 4 335 A=239B=258 795 389
Write a function called symmetric that will accept one argument, a matrix m, and returns true if the matrix is symmetric or false otherwise.
Example usage of symmetric:
>>> m1 = [[3, 5, 9], [5, 7, 1], [9, 1, 5]] >>> m2 = [[3, 5, 10], [5, 0, 6], [10, 7, 1]]
>>> m3 = [[0, 0], >>> m4 = [[0, 0], >>> symmetric(m1) True
>>> symmetric(m2) False
>>> symmetric(m3) True
>>> symmetric(m4) False
>>>
[0, 0]] [1, 0]]
Assistant Professor: Scott Bishop, PhD SMC CS87A, Spring 2019 2
Program 4: The Transpose of a Matrix (2D List)
The transpose of matrix A is a new matrix At = transpose(A) where the rows of A are the columns of At and the columns of A are the rows of At. The matrix A does not need to be a square matrix. For example, the transpose of the matrix A defined below is:
26 1 2 3 37 2 1 4 7 1 0 3 A=644 5 675transpose(A)=42 5 8 115
7 8 9 3 6 9 12 10 11 12
Write a function called transpose that will return a new matrix with the transpose matrix. The original matrix must not be modified. See the usage for examples of how transpose works.
Example usage of transpose:
>>> m5 =[[1, 2, 3, 4], [5, 6, 7, 8]] >>> m6 = [[1, 2, 3, 4]]
>>> m7 = [[1, 2], [3, 4], [5, 6]] >>> transpose(m5)
[[1, 5], [2, 6], [3, 7], [4, 8]] >>> transpose(m6)
[[1], [2], [3], [4]]
>>> transpose(m7)
>>>[[1, 3, 5], [2, 4, 6]]
Program 5: Adding Two Matrices (2D Lists)
We can add two matrices A and B to create a new matrix C = addition(A, B) where each row element of A is added to the corresponding row element of B. A and B need to have the same number of rows and columns, but does not need to be square.
For example, we can add matrix A + B as defined below to compute C:
261 2 337 261 2 337 262 4 637
A = 64 4 5 6 75 B = 64 4 5 6 75 addition(A,B) = 64 8 10 1275 7 8 9 7 8 9 14 16 18
10 11 12 10 11 12 20 22 24
Write a function called addition that returns a new matrix with sum of matrix A + B. The original
matrices (A and B) must not be modified. See the usage for examples of how addition works. Example usage of addition:
>>> m = [[1, 2],[3,4]] >>> n = [[5,6],[7,8]] >>> addition(m,n)
[[6, 8], [10, 12]]
>>> m = [[0,1,2],[3,4,5]] >>> n = [[6,7,8],[9,10,11]] >>> addition(m,n)
[[6, 8, 10], [12, 14, 16]]
Assistant Professor: Scott Bishop, PhD SMC CS87A, Spring 2019 3
Where to do the assignment
You can do this assignment on your own computer, or in the SMC labs. In either case, ensure the code runs on Windows and in IDLE. Submit one .py file named A04.py. Do not use any other name or else points will be deducted.
Submitting the Assignment
Include your name, your student id, the assignment number, the submission date, and a program descrip- tion in comments at the top of your files. Submit the assignment on Canvas (https://online.smc. edu) by uploading your .py file to the Assignment 4 entry as an attachment. Do not cut-and-paste your script into a text window. Do not hand in a screenshot of your program’s output. Do not hand in a text file containing the output of your program. Do not save and turn in the interpreter session (e.g. the one with >>>).
Saving your work
Save your work often on a flash-drive or to the cloud (e.g., GoogleDrive, Microsoft OneDrive, Canvas, etc.). Always save a personal copy of your files (e.g. .py, etc.). Do not store files on the lab computers.
Assistant Professor: Scott Bishop, PhD SMC CS87A, Spring 2019 4