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

Slides:



Advertisements
Similar presentations
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

CHAPTER 9 DEFINING CLASSES & CREATING OBJECTS Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Python, CGI November 23, Unit 8. So Far We can write programs in Python (in theory at least) –Conditionals –Variables –While loops We can create a form.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
Introduction to a Programming Environment
Main task -write me a program
Chapter 1 Program Design
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Binary “There are 10 types of people in the world… those that understand binary and those that don’t.”
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Group practice in problem design and problem solving
Lilian Blot CORE ELEMENTS COLLECTIONS & REPETITION Lecture 4 Autumn 2014 TPOP 1.
Activity 1 - WBs 5 mins Go online and spend a moment trying to find out the difference between: HIGH LEVEL programming languages and LOW LEVEL programming.
1 Spidering the Web in Python CSC 161: The Art of Programming Prof. Henry Kautz 11/23/2009.
1 Shawlands Academy Higher Computing Software Development Unit.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Nachos Phase 1 Code -Hints and Comments
Lists in Python.
Computing Science 1P Lecture 21: Friday 20 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
General Programming Introduction to Computing Science and Programming I.
CS1Q Computer Systems Lecture 8
Computing Science 1P Lecture 13: Friday 26 th January Simon Gay Department of Computing Science University of Glasgow 2006/07.
COMPSCI 101 Principles of Programming Lecture 28 – Docstrings & Doctests.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
1 The Software Development Process  Systems analysis  Systems design  Implementation  Testing  Documentation  Evaluation  Maintenance.
Computing Science 1P Lecture 18: Friday 2 nd March Simon Gay Department of Computing Science University of Glasgow 2006/07.
Lecture 21 CS110 Lecture 2 January 29, 2004 Announcements –hw1 part 1 – due right now –hw1 part 2 – due Tuesday night Questions Agenda –turnin –Object.
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Intermediate 2 Software Development Process. Software You should already know that any computer system is made up of hardware and software. The term hardware.
Python Programming Graphical User Interfaces Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Computing Science 1P Lecture 17: Friday 23 rd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 20 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Recap form last time How to do for loops map, filter, reduce Next up: dictionaries.
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
Computing Science 1P Lecture 14: Friday 2 nd February Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial: Lab Exam & Class Test Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Lecture 19: Friday 9 th March Simon Gay Department of Computing Science University of Glasgow 2006/07.
The Software Development Process
I Power Higher Computing Software Development High Level Language Constructs.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
1 CS161 Introduction to Computer Science Topic #9.
Copyright 2010, The World Bank Group. All Rights Reserved. Testing and Documentation Part II.
Guide to Programming with Python Chapter Seven Files and Exceptions: The Trivia Challenge Game.
Computing Science 1P Large Group Tutorial 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
SEG 4110 – Advanced Software Design and Reengineering Topic T Introduction to Refactoring.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
CS1Q Computer Systems Lecture 8
Types and Programming Languages
1 The Software Development Process ► Systems analysis ► Systems design ► Implementation ► Testing ► Documentation ► Evaluation ► Maintenance.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Functions Part I (Syntax). What is a function? A function is a set of statements which is split off into a separate entity that can be used like a “new.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
First Program  Open a file  In Shell  Type into the file: 3  You did it!!! You wrote your first instruction, or code, in python!
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Coupling and Cohesion Schach, S, R. Object-Oriented and Classical Software Engineering. McGraw-Hill, 2002.
Introduction to Computing Science and Programming I
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Department of Computer Science,
Lecture 2 Introduction to Programming
CDA 3100 Summer 2013.
CISC101 Reminders Slides have changed from those posted last night…
A First Program.
Department of Computing Science
Introduction to the Lab
Introduction to Computer Science
Presentation transcript:

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

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

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.

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" : " ", "Chloe" : " ", "Jane" : " " }

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.

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.

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

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.

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.

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.

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

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"

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

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.

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

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

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 line end position loop 4 square move 50 0 end

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.

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

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.

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

2006/07Computing Science 1P Tutorial 17 - Simon Gay22 Introducing Unit 15 { " " : { "pin" : "1234", "balance" : }, " " : { "pin" : "5555", "balance" : },... } 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:

2006/07Computing Science 1P Tutorial 17 - Simon Gay23 Introducing Unit 15 { "date" : " , 10:52:05", "nature" : "d", "amount" : } 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:

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. { " " : { "pin" : "1234", "balance" : 23.14, "transactions" : [ { "date":... }, { "date":... }, { "date":... } ] }, " " : { "pin" : "5555", "balance" : , "transactions" : [... ] },... }

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.

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()

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()