Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Similar presentations


Presentation on theme: "Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07."— Presentation transcript:

1 Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07

2 Computing Science 1P Tutorial 13 - Simon Gay2 What is len( [ 1, 2, [3, 4] ] ) ? 2 3 4 Something else Don't know

3 2006/07Computing Science 1P Tutorial 13 - Simon Gay3 If x = [1, 2, [3, 4] ], what is x[2] ? 2 [3, 4] Something else Don't know

4 2006/07Computing Science 1P Tutorial 13 - Simon Gay4 If x = [ ["a", 3], ["b", 4] ], what is x[1][1] ? "a" 3 "b" 4 Something else Don't know

5 2006/07Computing Science 1P Tutorial 13 - Simon Gay5 If you type [0][0] into the Python shell, what is the result? Error message 0 [0] Something else Don't know

6 2006/07Computing Science 1P Tutorial 13 - Simon Gay6 Using nested lists to represent a matrix A matrix (plural: matrices) is a rectangular grid of numbers. 123245312123245312 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).

7 2006/07Computing Science 1P Tutorial 13 - Simon Gay7 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] 123245312123245312 [ [1,2,3], [2,4,5], [3,1,2] ]

8 2006/07Computing Science 1P Tutorial 13 - Simon Gay8 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

9 2006/07Computing Science 1P Tutorial 13 - Simon Gay9 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.

10 2006/07Computing Science 1P Tutorial 13 - Simon Gay10 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).

11 2006/07Computing Science 1P Tutorial 13 - Simon Gay11 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.

12 2006/07Computing Science 1P Tutorial 13 - Simon Gay12 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.

13 2006/07Computing Science 1P Tutorial 13 - Simon Gay13 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.

14 2006/07Computing Science 1P Tutorial 13 - Simon Gay14 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.

15 2006/07Computing Science 1P Tutorial 13 - Simon Gay15 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 ).


Download ppt "Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07."

Similar presentations


Ads by Google