Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import traceback #SortStudents function. Takes as input a list of Student objects, #sorted alphabetically by name (last, then first), and outputs a list of #Student

image text in transcribedimage text in transcribed

import traceback

#SortStudents function. Takes as input a list of Student objects, #sorted alphabetically by name (last, then first), and outputs a list of #Student objects, sorted by the following priority: #house, then year, then name. def SortStudents(studentList): #TODO: Implement this function return studentList

# DO NOT EDIT BELOW THIS LINE

#Student class #Each task has three instance variables: # self.name is a string representing the name of the student # self.house is a string representing which house the student is in # self.year is an integer representing what year the student is class Student: def __init__(self,csvstring): csvdata = csvstring.split(",") self.name = csvdata[0] self.house = csvdata[1] self.year = int(csvdata[2]) def __repr__(self): return " {:25}: {:12} {}".format(self.name,self.house,self.year) def __eq__(self,other): return type(self) == type(other) and \ self.name == other.name and \ self.house == other.house and \ self.year == other.year

#Takes a string filename as an argument, and constructs a list # of Students from the information in the CSV file at filename def getStudentList(filename): fp = open(filename) fp.readline() studentList = [] for line in fp: studentList.append(Student(line)) return studentList

if __name__ == '__main__': tests = ['roster1.csv','roster2.csv','roster3.csv','roster4.csv', 'roster5.csv','roster6.csv'] correct = ['roster1sorted.csv','roster2sorted.csv', 'roster3sorted.csv','roster4sorted.csv', 'roster5sorted.csv','roster6sorted.csv']

#Run test cases, check whether sorted list correct count = 0

try: for i in range(len(tests)): print(" --------------------------------------- ") print("TEST #",i+1) print("Reading student data from:",tests[i]) roster = getStudentList(tests[i]) print("Reading sorted student data from",correct[i]) rosterSorted = getStudentList(correct[i]) print("Running: SortStudents() on data list ") output = SortStudents(roster) print("Expected:",rosterSorted," Got:",output) assert len(output) == len(rosterSorted), "Output list length "\ +str(len(output))+\ ", but should be "+str(len(rosterSorted)) for j in range(len(output)): assert output[j] == rosterSorted[j],"Student #"\ +str(j+1)+" incorrect: "+\ str(output[j])+" should be "+str(rosterSorted[j]) print("Test Passed! ") count += 1 except AssertionError as e: print(" FAIL: ",e)

except Exception: print(" FAIL: ",traceback.format_exc())

#Check for less than or greater than signs anywhere in the file cursed = False with open(__file__) as f: source = f.read() for ch in source: if ord(ch) == 60: print("Less than sign detected: Curse activated!") count = 0 cursed = True if ord(ch) == 62: print("Greater than sign detected: Curse activated!") count = 0 cursed = True

print() if cursed: print("You are now a newt. Don't worry, you'll get better.") print(count,"out of",len(tests),"tests passed.")

import traceback \#Sortstudents function. Takes as input a list of student objects, \#sorted alphabetically by name (last, then first), and outputs a list of HStudent objects, sorted by the following priority: Hhouse, then year, then name. def Sortstudents(studentList): \#TOD0: Implement this function return studentList \# DO NOT EDIT BELOW THIS LINE HStudent class \#Each task has three instance variables: \# self.name is a string representing the name of the student * self.house is a string representing which house the student is in \# self.year is an integer representing what year the student is class student: def __init__(self, csvstring): csvdata = csvstring split(",") self. name =csvdata[] self, house = csvdata[1] self.year = int ( csvdata [2]) def__repr__(self): return " {:25}:{:12}{}. .format(self. name, self.,house, self.year) def __eq__(self, other): return type(self) == type (other) and I self. name == other. name and self.house == other. house and I self.year == other.year \#Takes a string filename as an argument, and constructs a list \# of Students from the information in the CSV file at filename def getstudentList(filename): fp=open(fi lename) fp. readline() studentList = [] for line in fp : studentList. append (Student(line)) return studentList

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Under which Ribbon tab can you find the Font command buttons?

Answered: 1 week ago

Question

1. Describe the power of nonverbal communication

Answered: 1 week ago