1 / 15

Computing Science 1P

Learn about nested lists, matrix representation, printing matrices, calculating trace, and testing programs.

staceyd
Download Presentation

Computing Science 1P

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

More Related