Computing Science 1P - PowerPoint PPT Presentation

About This Presentation
Title:

Computing Science 1P

Description:

telephone directory? Write down a small example of such a dictionary in ... What if we want to do the reverse? Define a function findPerson(directory,number) ... – PowerPoint PPT presentation

Number of Views:9
Avg rating:3.0/5.0
Slides: 28
Provided by: simo7
Category:

less

Transcript and Presenter's Notes

Title: Computing Science 1P


1
Computing Science 1P
Large Group Tutorial 17
Simon Gay Department of Computing
Science University of Glasgow
2006/07
2
Announcement
The Level 1 Staff-Student Committee meeting for
this semester is on Monday 5th 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
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
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
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) wh
ich 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
Answer 2
Ignoring the question of what to do if the number
is not found
def findPerson(directory,number) for name in
directory if directoryname 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
What should we do?
  • Nothing leave the function as it is
  • Return an empty string
  • Return -1
  • Raise an exception

8
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
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
How would I decide?
I would probably try to do something that is
consistent with the standard operations on
dictionaries.
When using dictionarykey 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
Idea 1
def findPerson(directory,number) for name in
directory if directoryname number
return name return ""
12
Idea 2
def findPerson(directory,number) for name in
directory if directoryname number
return name raise
"PhoneNumberError"
13
Idea 3
def findPerson(directory,number,error) for
name in directory if directoryname
number return name return error
14
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
Answer 3
def invert(directory) inverse for
name in directory number
directoryname inversenumber name
return inverse
16
Does "invert" ...
  • modify the existing dictionary ?
  • create a new dictionary ?
  • Don't know

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
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 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
Have you completed the first tick of Unit 13?
  • Yes
  • No

20
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
Introducing Unit 15
"12345678" account details, "98765432"
account details, ...
22
Introducing Unit 15
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
"12345678" "pin" "1234",
"balance" 23.14 , "98765432" "pin"
"5555", "balance" 100.56 ,
...
23
Introducing Unit 15
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
"date" "28-02-2007, 105205", "nature"
"d", "amount" 100.00
24
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"... ,
"date"... ,
"date"... ,
"date"...
, "98765432" "pin" "5555",
"balance" 100.56,
"transactions" ... , ...
25
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
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
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()
Write a Comment
User Comments (0)
About PowerShow.com