Top Banner
Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07
28

Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

Jan 03, 2016

Download

Documents

Britney Page
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

Computing Science 1P

Lecture 15: Friday 9th February

Simon GayDepartment of Computing Science

University of Glasgow

2006/07

Page 2: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 2

Using Files (Chapter 11)

The unit of long-term data storage is the file. Most computerprograms work with data stored in files, to some extent.

Python provides functions for using files:

the open function makes a file available for reading or writing

file1 = open("data.txt","r")

file2 = open("output.txt","w")

Page 3: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 3

Using Files

If we have a file that is open for reading, there are severalways of getting data from it.

file1.read() returns the entire contents, as a string

file1.read(10) returns the next 10 characters

file1.readline() returns the next line, as a string

file1.readlines() returns all of the remaining lines,as a list of strings

When we have finished using the file:file1.close()

Page 4: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 4

What is a file?

A file is a sequence of bytes. We will mostly work withtext files. A text file can be thought of as a sequence ofcharacters. Some of these characters will be newlines, so thatthe file is separated into lines.

Examples

We're going to look at examples in which data is stored in atext file, with a specified format for each line. A convenient wayof working is to read one line at a time, then work out how toprocess the line.

Page 5: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 5

Case Study: Planning / Problem Solving

This is a problem from last year's version of CS1P. It's aboutcalculating students' grade point averages (it's a dirty job, but someone has to do it…)

Data on students, their modules and grades is stored in a file:

0606559 VBN2 30 B FGH9 50 A SDF2 50 N DFG5 60 C0500300 HHJ3 40 D FGH3 10 E JHG3 10 F0605456 LKJ1 10 G CDS2 30 C0601234 XDX2 40 A ABC1 40 B

matric numbermodule code

credits

grade

Page 6: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 6

Grade Point Averages

For each student, the following calculation must be done.

Each grade is converted into grade points using the followingtable:

A B C D E F G

16 14 12 10 8 6 2

all other results (H, N, CR, …) have 0 grade points.

For each module, the number of credits is multiplied by thegrade points corresponding to the grade. These are added upto give the total grade points for the student.

The grade point average (GPA) is the total grade pointsdivided by the total number of credits.

Page 7: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 7

Grade Point Average: Example

The student whose details are

0605456 LKJ1 10 G CDS2 30 C

has 10 * 2 + 30 * 12 = 380 grade points

and a GPA of 380 / 40 = 9.5

Page 8: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 8

The Problem

Write a program to read in students' data from a file, calculatethe GPA for each student, and then print the GPA of eachstudent, separated into two categories: students with a GPA ofat least 10.0, and students with a GPA of less than 10.0.

Students with GPA >= 10.0

0606559 190 10.20601234 80 15.0

Students with GPA < 10.0

0500300 60 9.00605456 40 9.5

GPA

total credits

Page 9: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 9

Planning

How do we begin to write the program? We need a plan.

To develop a plan, we need to think about the important data structures for the problem, and the necessary algorithms(i.e. computational processes).

In other words: what data do we need to store, and how do weneed to process it?

Algorithms + Data Structures = ProgramsNiklaus Wirth (1976)

Page 10: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 10

Do we need to store anything at all?

Do we need to store information about students and theirGPAs, for example in a list or dictionary, or can we just dothe processing and produce the output while we are readingthe original data in?

(Remember that in the lab exam bin-packing problem, therewas no need to store information about which bin eachobject went into.)

Page 11: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 11

Do we need to store information about students?

• Yes• No• Don't know

Page 12: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 12

Storing information about students

Because the output has to be separated into two groups(high GPAs and then low GPAs), we need to store the data.

We have information about each student, so we'll use adictionary in which the keys are matric numbers, represented bystrings.

What do we store for each student? The total credits and the GPA. Put them in a dictionary.

{ "0605456": { "credits":40, "gpa":9.5 }, ...}

Page 13: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 13

Top-level Plan

1. Read data from the file into the dictionary2. Print data from the dictionary in the required format

Is this enough?

If someone gave you this plan and told you to complete theprogram, would it be straightforward?

Or would you still have to do some problem-solving and makedesign decisions?

Page 14: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

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

Refining step 1 of the plan

1. For each line in the file:1.1 Calculate the total credits and GPA1.2 Store them in the dictionary, with the matric no. as key

1. Read data from the file into the dictionary

1.2 seems straightforward now, but 1.1 is still unclear.

Page 15: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 15

Refining step 1.1 of the plan

1.1.1 Split the current line into a list of strings1.1.2 For each module, add the credits to a running total, and add the grade points to a running total1.1.3 Calculate the GPA

1.1 Calculate the total credits and GPA

This is almost enough, but there is one detail: converting thegrade into the corresponding grade points.

The word FUNCTION should be leaping into your mind!

Page 16: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 16

Converting grades to grade points

A B C D E F G

16 14 12 10 8 6 2

There are various ways to consider implementing this table:

Quite a nice way is to use a dictionary:

def gp(g): table = { 'A':16, 'B':14, 'C':12, 'D':10, 'E':8, 'F':6, 'G':2 } return table.get(g,0)

Page 17: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

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

Refining step 2 of the top-level plan

2. Print data from the dictionary in the required format

2.1 For each student in the dictionary:If GPA >= 10.0: print details

2.2 For each student in the dictionary:If GPA < 10.0: print details

Page 18: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 18

The complete plan

1. Read data from the file into the dictionary For each line in the file:

1.1 Calculate the total credits and GPA 1.1.1 Split the current line into a list of strings 1.1.2 For each module, add the credits to a running total; likewise the grade points 1.1.3 Calculate the GPA

1.2 Store them in the dictionary, with the matric no. as key2. Print data from the dictionary in the required format 2.1 For each student in the dictionary:

If GPA >= 10.0: print details 2.2 For each student in the dictionary:

If GPA < 10.0: print details

Page 19: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 19

Developing the program

f = open("grades.txt","r")record = process(f) # Step 1f.close()output(record) # Step 2

Now we need to define the functions process and output.

Page 20: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 20

The function processdef process(f): record = {} # For each line in the file f, do something return record

Page 21: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 21

The function processdef process(f): record = {} line = f.readline() while line != '': # Process the information in line line = f.readline() return record

Page 22: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 22

The function processdef process(f): record = {} line = f.readline() while line != '': line = line[:-1] # Remove the final newline character # Process the information in line line = f.readline() return record

Page 23: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 23

The function processdef process(f): record = {} line = f.readline() while line != '': line = line[:-1] data = split(line,' ') # Step 1.1.1 matric = data[0] credits = 0 # Initialise running totals points = 0 # Step 1.1.2: for each module... line = f.readline() return record

0606559 VBN2 30 B FGH9 50 A SDF2 50 N DFG5 60 C

data = [ "0606559", "VBN2", "30", "B", "FGH9", "50", … ]

Page 24: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 24

The function processdef process(f): record = {} line = f.readline() while line != '': line = line[:-1] data = split(line,' ') matric = data[0] credits = 0 points = 0 modules = (len(data)-1)/3 # for each for i in range(modules): # module ... credit = int(data[i*3+2]) # credits = credits + credit # add credit grade = data[i*3+3] # points = points + gp(grade)*credit # add points # Step 1.1.3: calculate GPA line = f.readline() return record

Page 25: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 25

The function processdef process(f): record = {} line = f.readline() while line != '': line = line[:-1] data = split(line,' ') matric = data[0] credits = 0 points = 0 modules = (len(data)-1)/3 for i in range(modules): credit = int(data[i*3+2]) credits = credits + credit grade = data[i*3+3] points = points + gp(grade)*credit gpa = float(points)/credits record[matric] = { "credits":credits, "gpa":gpa } line = f.readline() return record

Page 26: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 26

The function outputdef output(r): print "Students with GPA >= 10.0" print for m in r: # m is each matric number in turn if r[m]["gpa"] >= 10.0: print m, r[m]["credits"], r[m]["gpa"] print print "Students with GPA < 10.0" print for m in r: # m is each matric number in turn if r[m]["gpa"] < 10.0: print m, r[m]["credits"], r[m]["gpa"]

Page 27: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 27

The function outputdef output(r): print "Students with GPA >= 10.0" print for m in r: if r[m]["gpa"] >= 10.0: print m, "%3d" % r[m]["credits"], "%4.1f" % r[m]["gpa"] print print "Students with GPA < 10.0" print for m in r: if r[m]["gpa"] < 10.0: print m, "%3d" % r[m]["credits"], "%4.1f" % r[m]["gpa"]

read Section 11.2

Page 28: Computing Science 1P Lecture 15: Friday 9 th February Simon Gay Department of Computing Science University of Glasgow 2006/07.

2006/07 Computing Science 1P Lecture 15 - Simon Gay 28

One more ideaThe following outline program will be useful for Unit 12.

def getChoice(): print "Choose from the following options:" print "a: operation 1" print "b: operation 2" print "q: quit" choice = raw_input("Enter your choice: ") return choice

c = getChoice()while c != "q": if c == "a": print "You chose operation 1" elif c == "b": print "You chose operation 2" else: print "You did not choose a valid option" c = getChoice()