Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Computing Science 1P Tutorial 17 - Simon Gay2 Announcement The Level 1 Staff-Student Committee meeting for this semester is on Monday 5 th March at 13.00 in F121, 14 Lilybank Gardens. If there is anything that you would like to be discussed at the meeting, please speak to the representative from your group.

3 2006/07Computing Science 1P Tutorial 17 - Simon Gay3 Question 1 How would you use a Python dictionary to represent a telephone directory? Write down a small example of such a dictionary in Python syntax.

4 2006/07Computing Science 1P Tutorial 17 - Simon Gay4 Answer 1 The obvious idea is to use a dictionary in which the keys are people's names and the values are the telephone numbers (represented by strings). Example: phonebook = { "John" : "01411234567", "Chloe" : "01312469876", "Jane" : "01419871234" }

5 2006/07Computing Science 1P Tutorial 17 - Simon Gay5 Question 2 This dictionary structure makes it easy to find the telephone number 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 number and returns the name of the person with that number, if there is one. If there is no-one with the given number, you should decide what your function does.

6 2006/07Computing Science 1P Tutorial 17 - Simon Gay6 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 is not found.

7 2006/07Computing Science 1P Tutorial 17 - Simon Gay7 What should we do? Nothing: leave the function as it is Return an empty string Return -1 Raise an exception

8 2006/07Computing Science 1P Tutorial 17 - Simon Gay8 What should we do? Leave the function as it is? It is likely to be confusing to have a function which sometimes returns a result and sometimes doesn't (in many languages this would be an error). Return an empty string? This seems reasonable if (as is likely) we know that the empty string cannot be a person's name.

9 2006/07Computing Science 1P Tutorial 17 - Simon Gay9 What should we do? Return -1 ? It is likely to be confusing to have a function which sometimes returns a string and sometimes returns an integer (in many languages this would be an error). Raise an exception? This is also a reasonable choice.

10 2006/07Computing Science 1P Tutorial 17 - Simon Gay10 How would I decide? I would probably try to do something that is consistent with the standard operations on dictionaries. When using dictionary[key] we get an exception if the key is not present. So it's reasonable for findPerson to raise an exception, perhaps a new one: PhoneNumberError Alternatively, we could follow the behaviour of the get method for dictionaries, and provide the error value as a parameter.

11 2006/07Computing Science 1P Tutorial 17 - Simon Gay11 Idea 1 def findPerson(directory,number): for name in directory: if directory[name] == number: return name return ""

12 2006/07Computing Science 1P Tutorial 17 - Simon Gay12 Idea 2 def findPerson(directory,number): for name in directory: if directory[name] == number: return name raise "PhoneNumberError"

13 2006/07Computing Science 1P Tutorial 17 - Simon Gay13 Idea 3 def findPerson(directory,number,error): for name in directory: if directory[name] == number: return name return error

14 2006/07Computing Science 1P Tutorial 17 - Simon Gay14 Question 3 A dictionary is designed for efficient look-up, but the function findPerson is much less efficient because it potentially has to look at every item in the dictionary. If we are going to use findPerson repeatedly, it could be worth inverting the telephone directory. This means constructing a new dictionary in which the keys are the telephone numbers and the values are the people's names. Define a function invert(directory) which is given a telephone directory and returns the inverse directory.

15 2006/07Computing Science 1P Tutorial 17 - Simon Gay15 Answer 3 def invert(directory): inverse = {} for name in directory: number = directory[name] inverse[number] = name return inverse

16 2006/07Computing Science 1P Tutorial 17 - Simon Gay16 Does "invert"... modify the existing dictionary ? create a new dictionary ? Don't know

17 2006/07Computing Science 1P Tutorial 17 - Simon Gay17 Comments on Unit 13 Remember the exercise to implement an interpreter for the "drawing language". define square line 20 0 line 0 20 line -20 0 line 0 -20 end position 20 10 loop 4 square move 50 0 end

18 2006/07Computing Science 1P Tutorial 17 - Simon Gay18 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 just read through the file once, interpreting commands as they are found; of course, for functions and loops it is necessary to store a list of commands. If the language gets much more complex it becomes very difficult to use a single pass interpreter. Even allowing loops within loops, or loops within functions, is difficult. For a realistic language (e.g. Python) it is normal to read the whole program and store it in a suitable data structure.

19 2006/07Computing Science 1P Tutorial 17 - Simon Gay19 Have you completed the first tick of Unit 13? Yes No

20 2006/07Computing Science 1P Tutorial 17 - Simon Gay20 Introducing Unit 15 The exercise for Unit 15 involves working with bank account details. 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 numbers are strings). So we use a dictionary in which the keys are account numbers.

21 2006/07Computing Science 1P Tutorial 17 - Simon Gay21 Introducing Unit 15 { "12345678" : # account details, "98765432" : # account details,... }

22 2006/07Computing Science 1P Tutorial 17 - Simon Gay22 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:

23 2006/07Computing Science 1P Tutorial 17 - Simon Gay23 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 6 transactions for each account. A transaction consists of the date (a string), the nature of the transaction ("w" for withdrawal, "d" for deposit), and the amount (a floating point number). Represent a transaction by a small dictionary:

24 2006/07Computing Science 1P Tutorial 17 - Simon Gay24 Introducing Unit 15 Storing the last 6 transactions for each account means that the dictionary 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":... } ] }, "98765432" : { "pin" : "5555", "balance" : 100.56, "transactions" : [... ] },... }

25 2006/07Computing Science 1P Tutorial 17 - Simon Gay25 Introducing Unit 15 To do this exercise it is essential to be very clear about this data structure, otherwise you will get hopelessly confused. It is also important to be clear about the order of the last 6 transactions: 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.

26 2006/07Computing Science 1P Tutorial 17 - Simon Gay26 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 read back later. import pickle f = open("file.pck","w") # d can be any data structure pickle.dump(d,f) f.close()

27 2006/07Computing Science 1P Tutorial 17 - Simon Gay27 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()


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

Similar presentations


Ads by Google