t_10.4up

# t_10.4up - Engineering Computation Tutorial 10 Problem...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering Computation: Tutorial 10 Problem Solving with Computing, using the Python Language What’s in your toolbox? Print statement, string literals Basic calculator, integer division Variables Built-in functions Math library functions Straight-line programs Branching programs Python lists For loops Nested for loops Strings While loops Converting between number system using Python Matplotlib Program Design File Processing Graphics Functions Scope of Variables Comments Complex Conditions Testing and Debugging Exception Handling Common Loop Patterns Boolean Operators Unit Testing Classes and Objects 2 Today’s Topics: 1 Data Collections 2 Searching 3 Sorting 3 Designing with Lists and Classes Our algorithm from Tutorial 09 found ”the highest GPA”. Actually, it found the first student in the file with the highest GPA. What if we need to know all students that tied for the highest GPA? What if we wanted to know the stats of how many students received each GPA score (perhaps we would want to make a graph of the first year engineering GPA distribution at McMaster). 4 Designing with Lists and Classes The program from last tutorial only remembers the current student object, and only the highest GPA (one). infoString = infile.readline() name, stnumber, credit, points = string.split(infoString,"\t") best = Student(name, stnumber, credit, points) for line in infile: name, stnumber, credit, points = string.split(line,"\t") student = Student(name, stnumber, credit, points) #if this student is best so far, remember it. if student.gpa() > best.gpa(): best = student 5 Designing with Lists and Classes Lists and classes taken together give us powerful tools for structuring the data in our programs. Let’s put these tools to work in a concrete example by improving our GPA program. 6 Designing with Lists and Classes Algorithm to get list of top GPA students 1 Create a list of all students in the file. 2 Find the highest GPA score in the list . 3 Create a list of students that have this GPA. We can define the following functions def createStudentList ( ) : def highGPA( s t L i s t ) : def hiGPAstudentList (hiGPA , s t L i s t ) : 7 Designing with Lists and Classes createStudentList from Student import * #why? import string #why? def createStudentList(): #get filename from user filename = raw_input("Enter the name of the grade file: ") #open the file for reading infile = open(filename, ’r’) #create an empty list of students students = #fill the list of students for line in infile: name, stnumber, credit, points = string.split(line, "\t") student = Student(name, stnumber, credit, points) #add the student to the list students.append(student) return students 8 Designing with Lists and Classes highGPA def highGPA(stList): #set the best to be the first gpa bestGPA = stList[0].gpa() for student in stList: if student.gpa() > bestGPA: bestGPA = student.gpa() return bestGPA 9 Designing with Lists and Classes hiGPAstudentList def hiGPAstudentList(hiGPA, stList):...
View Full Document

{[ snackBarMessage ]}

### Page1 / 11

t_10.4up - Engineering Computation Tutorial 10 Problem...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online