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

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

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

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

Computing Science 1P

Large Group Tutorial 17

Simon GayDepartment of Computing Science

University of Glasgow

2006/07

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

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

Announcement

The Level 1 Staff-Student Committee meeting for this semesteris on Monday 5th March at 13.00 in F121, 14 Lilybank Gardens.

If there is anything that you would like to be discussed at themeeting, please speak to the representative from your group.

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

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

Question 1

How would you use a Python dictionary to represent a telephone directory?

Write down a small example of such a dictionary in Pythonsyntax.

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

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

Answer 1

The obvious idea is to use a dictionary in which the keys arepeople's names and the values are the telephone numbers(represented by strings).

Example:

phonebook = { "John" : "01411234567", "Chloe" : "01312469876", "Jane" : "01419871234" }

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

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

Question 2

This dictionary structure makes it easy to find the telephonenumber of a particular person. What if we want to do the reverse?

Define a function findPerson(directory,number)which is given a telephone directory and a telephone numberand returns the name of the person with that number, if thereis one.

If there is no-one with the given number, you should decidewhat your function does.

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

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

Answer 2

Ignoring the question of what to do if the number is not found:

def findPerson(directory,number): for name in directory: if directory[name] == number: return name # number not found: what do we do?

There are several possibilities for what to do if the number isnot found.

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

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

What should we do?

• Nothing: leave the function as it is• Return an empty string• Return -1• Raise an exception

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

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

What should we do?

Leave the function as it is?

It is likely to be confusing to have a function which sometimesreturns a result and sometimes doesn't (in many languagesthis would be an error).

Return an empty string?

This seems reasonable if (as is likely) we know that theempty string cannot be a person's name.

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

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

What should we do?

Return -1 ?

It is likely to be confusing to have a function which sometimesreturns a string and sometimes returns an integer(in many languages this would be an error).

Raise an exception?

This is also a reasonable choice.

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

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

How would I decide?

I would probably try to do something that is consistent with thestandard operations on dictionaries.

When using dictionary[key] we get an exception if the key is not present. So it's reasonable for findPersonto raise an exception, perhaps a new one: PhoneNumberError

Alternatively, we could follow the behaviour of the get methodfor dictionaries, and provide the error value as a parameter.

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

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

Idea 1

def findPerson(directory,number): for name in directory: if directory[name] == number: return name return ""

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

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

Idea 2

def findPerson(directory,number): for name in directory: if directory[name] == number: return name raise "PhoneNumberError"

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

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

Idea 3

def findPerson(directory,number,error): for name in directory: if directory[name] == number: return name return error

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

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

Question 3

A dictionary is designed for efficient look-up, but the functionfindPerson is much less efficient because it potentially has tolook at every item in the dictionary.

If we are going to use findPerson repeatedly, it could beworth inverting the telephone directory. This means constructing a new dictionary in which the keys are thetelephone numbers and the values are the people's names.

Define a function invert(directory) which is given atelephone directory and returns the inverse directory.

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

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

Answer 3

def invert(directory): inverse = {} for name in directory: number = directory[name] inverse[number] = name return inverse

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 16

Does "invert" ...

• modify the existing dictionary ?• create a new dictionary ?• Don't know

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

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

Comments on Unit 13

Remember the exercise to implement an interpreter for the"drawing language".

define squareline 20 0line 0 20line -20 0line 0 -20endposition 20 10loop 4squaremove 50 0end

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 18

Comments on Unit 13

The exercise was meant to illustrate, in a simplified form,what is going on inside the Python interpreter.

The drawing language is so simple that it is possible to justread through the file once, interpreting commands as they arefound; of course, for functions and loops it is necessary to store a list of commands.

If the language gets much more complex it becomes verydifficult to use a single pass interpreter. Even allowingloops within loops, or loops within functions, is difficult.

For a realistic language (e.g. Python) it is normal to read thewhole program and store it in a suitable data structure.

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 19

Have you completed the first tick of Unit 13?

• Yes• No

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 20

Introducing Unit 15

The exercise for Unit 15 involves working with bank accountdetails. Suitable data structures are described on the worksheet,but let's make sure we understand them.

At the top level we have a collection of bank accounts,identified by account numbers (actually the account numbersare strings).

So we use a dictionary in which the keys are account numbers.

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 21

Introducing Unit 15

{ "12345678" : # account details,

"98765432" : # account details,

...}

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 22

Introducing Unit 15

{ "12345678" : { "pin" : "1234", "balance" : 23.14 },

"98765432" : { "pin" : "5555", "balance" : 100.56 },

...}

The details of a particular account are the PIN (a string)and the current balance (a floating point number).We use a small dictionary for each account:

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 23

Introducing Unit 15

{ "date" : "28-02-2007, 10:52:05", "nature" : "d", "amount" : 100.00 }

Later we introduce the idea of storing a list of the last 6transactions for each account.

A transaction consists of the date (a string), the nature of thetransaction ("w" for withdrawal, "d" for deposit), and the amount(a floating point number).

Represent a transaction by a small dictionary:

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 24

Introducing Unit 15

Storing the last 6 transactions for each account means that thedictionary representing an account has a new key: "transactions"whose value is a list of dictionaries.

{ "12345678" : { "pin" : "1234", "balance" : 23.14, "transactions" : [ { "date":... }, { "date":... }, { "date":... }, { "date":... }, { "date":... }, { "date":... } ] },

"98765432" : { "pin" : "5555", "balance" : 100.56, "transactions" : [ ... ] },

...}

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 25

Introducing Unit 15

To do this exercise it is essential to be very clear about thisdata structure, otherwise you will get hopelessly confused.

It is also important to be clear about the order of the last 6transactions: is the most recent transaction in position 0 of the list or position 5 ?

The first tick of Unit 15 is for refining a top-level plan for the problem.

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 26

Introducing Unit 15

The exercise also makes use of the pickle module (book p81).This allows any data structure to be written to a file and readback later.

import pickle

f = open("file.pck","w")

# d can be any data structure

pickle.dump(d,f)

f.close()

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

2006/07 Computing Science 1P Tutorial 17 - Simon Gay 27

Introducing Unit 15

Reading a pickled data structure:

import pickle

f = open("file.pck","r")

d = pickle.load(f)

# d is now the original data structure

f.close()