150 likes | 162 Views
Learn about nested lists, matrix representation, printing matrices, calculating trace, and testing programs.
E N D
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07
What is len( [ 1, 2, [3, 4] ] ) ? • 2 • 3 • 4 • Something else • Don't know Computing Science 1P Tutorial 13 - Simon Gay
If x = [1, 2, [3, 4] ], what is x[2] ? • 2 • [3, 4] • Something else • Don't know Computing Science 1P Tutorial 13 - Simon Gay
If x = [ ["a", 3], ["b", 4] ], what is x[1][1] ? • "a" • 3 • "b" • 4 • Something else • Don't know Computing Science 1P Tutorial 13 - Simon Gay
If you type [0][0] into the Python shell, what is the result? • Error message • 0 • [0] • Something else • Don't know Computing Science 1P Tutorial 13 - Simon Gay
Using nested lists to represent a matrix A matrix (plural: matrices) is a rectangular grid of numbers. • 2 3 • 4 5 • 1 2 Represent it by a list of lists: [ [1,2,3], [2,4,5], [3,1,2] ] Matrices are ubiquitous in science and engineering (and computer graphics). Computing Science 1P Tutorial 13 - Simon Gay
Working with a matrix If m is a 3 by 3 matrix, what does the following code print? for i in range(3): print m[i][0] • 2 3 • 4 5 • 1 2 [ [1,2,3], [2,4,5], [3,1,2] ] Computing Science 1P Tutorial 13 - Simon Gay
What does the code print? • The top row of the matrix • The leftmost column of the matrix • The diagonal of the matrix • Something else • Don't know Computing Science 1P Tutorial 13 - Simon Gay
Printing a matrix Define a function printMatrix which is given a parameter m, representing a 3 by 3 matrix, and prints it as a grid of numbers (no square brackets, no commas). Don’t worry about the spacing if the numbers have different numbers of digits. Computing Science 1P Tutorial 13 - Simon Gay
Printing a matrix def printMatrix(m): for row in m: for x in row: print x, print Key point: nested loops (to go with the nested lists). Computing Science 1P Tutorial 13 - Simon Gay
Trace of a matrix The trace of a matrix is the sum of the diagonal elements. 1 2 6 2 4 3 1 3 2 has trace 7 Define a function to calculate the trace of a given matrix. Computing Science 1P Tutorial 13 - Simon Gay
Trace of a matrix def trace(m): tr = 0 for i in range(len(m)): tr = tr + m[i][i] return tr Key point:m[i][i] is an element from the diagonal. Computing Science 1P Tutorial 13 - Simon Gay
Trace of a matrix: alternative def trace(m): tr = 0 i = 0 while i < len(m): tr = tr + m[i][i] i = i + 1 return tr But using for i in range(len(m)) seems nicer. Subtle point: for large numbers of iterations, range is less good because it builds a large list. Very subtle point: investigate xrange. Computing Science 1P Tutorial 13 - Simon Gay
Testing Someone has written a program which inputs a person’s age and is supposed to output a classification according to the following ranges: 17 or less: child 18 – 64: adult 65 or more: pensioner If the age is less than zero then an error message is supposed to be produced. Suggest input values which would be useful tests of the program. Computing Science 1P Tutorial 13 - Simon Gay
Testing Someone has defined a function which is supposed to return the most frequently occurring element of a list of numbers (in statistics this is called the mode). e.g. mode([1,2,1,1,3,4,3,2]) should be 1. Suggest useful test cases. (Further exercise: define mode). Computing Science 1P Tutorial 13 - Simon Gay