Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of...

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

Transcript of Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of...

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

Computing Science 1P

Large Group Tutorial 14

Simon GayDepartment of Computing Science

University of Glasgow

2006/07

Page 2: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 2

If d = { "John" : 5, "Helen" : 7 }, what do we call "John" ?

• A key• A value• Don't know

Page 3: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 3

Question

Suppose we define a dictionary by

d = { "Fred":10, "Joe":15, "Anne": 23, "Clare":14 }

and then run the following code:

for x in d.keys():print x

In which order will the keys of the dictionary appear?

Page 4: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 4

What is the order?

• Alphabetical order of keys• Numerical order of values• We can't tell; it looks random• The order in the definition• Don't know

Page 5: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 5

Calculating the mode

In statistics, the most frequently occurring element of a list of data is called the mode. It's a kind of average which does notrequire the data to have any structure at all.

e.g. mode([1,2,1,1,3,4,3,2]) should return 1.

We want to define a function to calculate the mode of a list ofnumbers.

We can do this by combining two ideas we already know:- frequency analysis- finding the largest value among data

Page 6: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 6

Calculating the mode

To calculate the mode, we need to calculate how many timeseach number occurs in the data.

How should we do this? (Example: [1,2,1,1,3,4,3,2] )

1. Use a list in which position i contains the number of occurrences of i

Example: [ 0,3,2,2,1 ]

2. Use a dictionary in which an item with key i has a valuewhich is the number of occurrences of i

Example: { 1:3, 2:2, 3:2, 4:1 }

Page 7: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 7

What is your preferred data structure for this problem?

• The list• The dictionary• Don't know

Page 8: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 8

Discussion

Think about what will happen in the following function call:

mode([1,2,1,1,3,4,3,2,1000000])

Do you still like your choice of data structure?

Discuss it with your neighbours.

Page 9: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 9

When a dictionary is better

When keys are integers and the set of keys is sparse (i.e. there is a large range of possible keys but relatively few are actually used), a dictionary is better than a list.

When keys are some other type (e.g. strings), so that we can'tuse positional lookup, a dictionary is better than a nested listbecause lookup is faster.

Page 10: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 10

Mode: counting occurrences

def mode(x):d = {}for n in x:

if d.has_key(n):d[n] = d[n] + 1

else:d[n] = 1

# now find the most common number

Page 11: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 11

What do you think of this code?

• It's correct: in fact, ideal• It's correct but can be simplified• It contains errors• Don't know

Page 12: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 12

Mode: counting occurrences

def mode(x):d = {}for n in x:

d[n] = d.get(n,0)+1# now find the most common number

The code is correct but it can be simplified a little by usingthe technique from page 76 of the book:

Exercise: complete the definition so that the most commonnumber is returned.

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

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 13

Using nested lists to represent a matrix

A matrix (plural: matrices) is a rectangular grid of numbers.

1 2 32 4 53 1 2

Represent it by a list of lists:

[ [1,2,3], [2,4,5], [3,1,2] ]

Let's think about an n n matrix: n rows and n columns,represented by a list of n lists, each of n elements.

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

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 14

Opposite diagonal of an n n matrix

Which of the following code fragments prints the "opposite"diagonal of an n n matrix m ? 1 3 2 5

4 1 0 2

2 2 3 4

5 1 2 3

n = len(m)for i in range(n):

print m[i][i]

n = len(m)for i in range(n):

print m[i][n-i]

n = len(m)for i in range(n):

print m[i][n-i-1]

n = len(m)for i in range(n):

print m[i][n-i+1]

1

2

3

4

Page 15: Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Tutorial 14 - Simon Gay 15

Which piece of code?

• 1• 2• 3• 4• None of them• Don't know