Sorting a class of students? class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname.

Slides:



Advertisements
Similar presentations
File Handle and conditional Lecture 2. A typical bioinformatics file: FASTA format Name of the following file is: DNA_sequence.fasta >gi|34529|emb|Y |
Advertisements

IF statement (i) Single statement. IF ( logical expression ) statement Example: read(*,*) a if (a. lt. 0) a = -a write(*,*) a Or read(*,*) a if (a < 0)
What gets printed out? def f(n): if (n == 0): return (" ") else: print(“blug ") return f(n-1) f(3)
Computer Science 112 Fundamentals of Programming II Queues and Priority Queues.
Python Mini-Course University of Oklahoma Department of Psychology Lesson 28 Classes and Methods 6/17/09 Python Mini-Course: Lesson 28 1.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
Classes 2 COMPSCI 105 SS 2015 Principles of Computer Science.
Rapid GUI Programming with Python and Qt Classes and Modules By Raed S. Rasheed Classes and Modules By Raed S. Rasheed 1.
Classes and Inheritance. 2 As the building blocks of more complex systems, objects can be designed to interact with each other in one of three ways: Association:
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Angela Des Jardins.
Recitation 6 Programming for Engineers in Python.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Lab 8 Shell Script Reference:
Chapter 9 IF Statement Bernard Chen. If Statement The main statement used for selecting from alternative actions based on test results It’s the primary.
Floating point numbers in Python Floats in Python are platform dependent, but usually equivalent to an IEEE bit C “double” However, because the significand.
Classes 3 COMPSCI 105 S Principles of Computer Science.
1 System Administration Introduction to Scripting, Perl Session 3 – Sat 10 Nov 2007 References:  chapter 1, The Unix Programming Environment, Kernighan.
Priority Queues and Binary Heaps Chapter Trees Some animals are more equal than others A queue is a FIFO data structure the first element.
Perl Practical(?)‏ Extraction and Report Language.
More On Classes UW CSE 160 Spring Classes define objects What are objects we've seen? 2.
Shells. Variables MESSAGE="HELLO WORLD" echo $MESSAGE MESSAGE="HELLO WORLD $LOGNAME" echo $MESSAGE MESSAGE="HELLO WORLD $USER" echo $MESSAGE.
IDL Tutorials: Day 4 Goal: Learn some programming techniques: Relational operators, conditional statements, boolean operators, loops Maria Kazachenko
OOP Lecture 06. OOP? Stands for object oriented programming. You’ve been using it all along. Anything you use the dot ‘.’ on is an object.
Shell Script2 Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
Chapter 17 Q and A Victor Norman, et al. CS104. What is Object-oriented Programming? Q: What is object-oriented programming? A: It means defining classes/objects,
Lab 8 Shell Script Reference: Linux Shell Scripting Tutorial v1.05r3 A Beginner's handbook
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Shell script – part 2 CS 302. Special shell variable $0.. $9  Positional parameters or command line arguments  For example, a script myscript take 2.
Making our own Classes and objects  As in real life, we’re now creating classes of objects.  a class that defines basic characteristics and functions.
Algorithms JPC and JWD © 2002 McGraw-Hill, Inc. 2 Algorithms 2 An Algorithm is a finite set of precise instructions for performing a computation or for.
Data Structures Arrays and Lists Part 2 More List Operations.
File Handle and conditional Lecture 2. File Handling The Files associated with Perl are often text files: e.g. text1.txt Files need to be “opened for.
Computational Astronomy 제 5 장 프로그래밍 기초 전산천문학 봄.
Object-Oriented Programming (OOP) in Python References: Chapter 8.
1 Lecture 8 Shell Programming – Control Constructs COP 3353 Introduction to UNIX.
Indentations makes the scope/block Function definition def print_message (): print “hello” Function usages print_message () hubo.move ()// hubo is a class.
Python Fundamentals: If Statements and Functions Eric Shook Department of Geography Kent State University.
GCSE COMPUTER SCIENCE Computers 1.5 Assembly Language.
Object Oriented Programming
More On Classes UW CSE 160 Winter
Classes and Objects – digging deeper
Mathematical operators overload
Software Development Java Classes and Methods
Copyright (c) 2017 by Dr. E. Horvath
Fundamentals of Programming I More Data Modeling
Object Oriented Programming in Python
Instruction List - an assembler type of language
Introduction to Object-Oriented Programming (OOP) II
Python Classes Python has Classes, Methods, Overloaded operators, as well as Inheritance. Its somewhat 'loose' in the since it does not have true encapsulation,
Subsetting Rows with the WHERE clause
Control Structures: for & while Loops
Program Flow Control Selection & repetition
Suggested Layout ** Designed to be printed on white A3 paper.
Fundamentals of Programming I Commonly Used Methods More Modeling
Fundamentals of Programming I More Data Modeling
CS3220 Web and Internet Programming Expression Language (EL)
Presented by, Mr. Satish Pise
Nate Brunelle Today: Conditional Decision Statements
CS3220 Web and Internet Programming Expression Language (EL)
The Selection Structure
Making our own Classes and objects
DATA TYPES AND OPERATIONS
Instruction List - an assembler type of language
INTRODUCTION to PERL PART 1.
Introduction to Object-Oriented Programming (OOP) II
REPETITION Why Repetition?
Control Structures.
Introduction to PYTHON
Presentation transcript:

Sorting a class of students? class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)]

Sorting on Last name: def sortstudentslname(liststu): for i in range(len(liststu)): smallstudent = liststu[i].lastname smallindex = i for j in range(i+1,len(liststu)): if liststu[j].lastname < smallstudent: smallstudent = liststu[j].lastname smallindex = j temp = liststu[i] liststu[i] = liststu[smallindex] liststu[smallindex] = temp return(liststu)

class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore def __str__(self): strvar = "Student ( \n“ + "Lastname: " + self.lastname + "\n" strvar += "Firstname: " + self.firstname + "\n" strvar += "Testscore: " + str(self.testscore)+"\n“+"Labscore: "+str(self.labscore)+"\n" strvar += ")\n\n" return(strvar) liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)] def sortstudentslname(liststu): for i in range(len(liststu)): smallstudent = liststu[i].lastname smallindex = i for j in range(i+1,len(liststu)): if liststu[j].lastname < smallstudent: smallstudent = liststu[j].lastname smallindex = j temp = liststu[i] liststu[i] = liststu[smallindex] liststu[smallindex] = temp return(liststu) def printstu(liststu): for i in range(len(liststu)): print liststu[i] printstu(liststu) sortstudentslname(liststu) printstu(liststu)

Operator Overloading That was a lot of work! Easier way??? Of course! Writing our own definition of what an operator should do – E.g., defining student1<student2 to mean: Does student1’s last name property come before student 2’s last name property?

In class: class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore self.totalscore = self.calcscore(200) def __lt__(self, student2): if self.lastname < student2.lastname: return True else: return False def __add__(self,student2): return(self.totalscore + student2.totalscore) def __str__(self): str1 = self.lastname + " " + self.firstname + " " + self.totalscore return(str1)

Now: liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)] def sortstudentslname(liststu): for i in range(len(liststu)): smallstudent = liststu[i] smallindex = i for j in range(i+1,len(liststu)): if liststu[j] < smallstudent: smallstudent = liststu[j] smallindex = j temp = liststu[i] liststu[i] = liststu[smallindex] liststu[smallindex] = temp return(liststu) def printstu(liststu): for i in range(len(liststu)): print liststu[i] printstu(liststu) sortstudentslname(liststu) printstu(liststu) Even better! liststu.sort() printstu(liststu) class Student(object): def __init__(self, lastname, firstname, testscore, labscore): self.lastname = lastname self.firstname = firstname self.testscore = testscore self.labscore = labscore self.totalscore = self.calcscore(200) def __lt__(self, student2): if self.lastname < student2.lastname: return True else: return False def __str__(self): str1 = self.lastname + " " + self.firstname str += " " + self.totalscore return(str1) def calcscore(self,totalpoints): return((self.testscore + self.labscore)/totalpoints)

list of Operators you can overload: __lt__ ( less than ) __le__ (less or equal to) __eq__ (equal to) __ne__ (not equal to) __gt__ (greater than) __ge__ (greater or equal to) __cmp__ (compare): This one gets called if the previous ones are not defined. __add__ __sub__ __mul__ __div__

Dorm Room Class? For a dorm room class, we’ll need the name of the hall, the number of the room, and the first and second student occupying the room. How? class DormRoom(object): def __init__(self,name, number, studenta,studentb): self.name = name self.number = number self.studenta = studenta self.studentb = studentb

To print the Dorm Room Object: class DormRoom(object): def __init__(self,name, number, studenta,studentb): self.name = name self.number = number self.studenta = studenta self.studentb = studentb def __str__(self): strvar = "Dorm Room " + self.name + str(self.number) + ": {\n" strvar += str(self.studenta) strvar += str(self.studentb) strvar += "}\n\n" return strvar

To create a Dorm Room Object: astudent = Student("baker","tom",132,88) bstudent = Student("miller","john",132,92) aroom = DormRoom("Dickinson",310, astudent,bstudent) print(aroom) # prints the last name of student a in the dorm room print(aroom.studenta.lastname)

Honors Dorm? class DormRoom(object): def __init__(self,name, number, studenta,studentb): self.name = name self.number = number self.studenta = studenta self.studentb = studentb def __str__(self): strvar = "Dorm Room " + self.name + str(self.number) + ": {\n" strvar += str(self.studenta) strvar += str(self.studentb) strvar += "}\n\n" return strvar def upgrade(self,studentc): if self.studenta.testscore < studentc.testscore: if self.studenta.testscore < self.studentb.testscore: self.studenta.lastname = studentc.lastname self.studenta.firstname = studentc.firstname self.studenta.testscore = studentc.testscore self.studenta.labscore = studentc.labscore else: self.studentb.lastname = studentc.lastname self.studentb.firstname = studentc.firstname self.studentb.testscore = studentc.testscore self.studentb.labscore = studentc.labscore elif self.studentb.testscore < studentc.testscore: self.studentb.lastname = studentc.lastname self.studentb.firstname = studentc.firstname self.studentb.testscore = studentc.testscore self.studentb.labscore = studentc.labscore

To Use: liststu = [Student("baker","tom",132,88), Student("abbey","steven",122,96), Student("lawrence","sarah",144,85), Student("miller","john",132,92), Student("jones","tina",128,94)] honorsroom = DormRoom("Dickinson",314,liststu[4],liststu[0]) print honorsroom honorsroom.upgrade(liststu[3]) print honorsroom