1 / 12

Sorting a class of students?

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

sharla
Download Presentation

Sorting a class of students?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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)]

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

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

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

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

  6. Now: 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) 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)

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

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

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

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

  11. 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 elifself.studentb.testscore < studentc.testscore: self.studentb.lastname = studentc.lastname self.studentb.firstname = studentc.firstname self.studentb.testscore = studentc.testscore self.studentb.labscore = studentc.labscore

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

More Related