Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Requirements: You must download the template file PA5.py and edit the SortStudents function. You may also create your own helper functions, but do not edit

Requirements:

  • You must download the template file PA5.py and edit the SortStudents function. You may also create your own helper functions, but do not edit anything below the DO NOTE EDIT line of the file

  • Your program must run without errors on the version of Python installed on the CSELabs machines, Python 3.5.2. (if youre testing this on CSELabs, you need to type python3 or idle3 instead of python or idle to start the correct version from the terminal)

  • You are not permitted to use any built-in Python sorting routines like the sorted() function or the .sort() list method. You are also not allowed to use any Python function that asks for user input, such as input(), as this will break the grading script. Finally, you are not permitted to use the characters > or < anywhere in the file, including in comments, since we dont want you to be turned into a newt.

  • You must implement the Counting Sort algorithm found in Chapter 8 of the textbook. Any other sorting algorithm will receive very little credit.

  • However, note that while the textbook algorithm describes how to sort a list of numbers once, this problem requires you to sort a list of Student objects twice, so the algorithm may need some adjustments.

  • In particular, note that while the textbook Counting Sort algorithm took in an input array A, an output array B, and a cap on the number of possible values k, your Sort will only take in an input list studentList. You can just hard code in the k values for the number of possible years (8) and the number of houses (4). You will be returning the output list rather than passing it in as an argument.

  • This assignment will be graded automatically based on the number of test cases your program passes. There will be several secret test cases in addition to the ones included in the template to ensure youre not hard-coding in the solutions.

  • Similar to the previous homework, this program will only run test cases until you fail one, avoiding the problem of having to scroll through test output to find the one broken test case.

  • The grading breakdown for this assignment is as follows:

    • 30%: File runs without syntax errors

    • 70%: Passing test cases without breaking any requirements.

  • The unedited template file already runs without syntax errors. This means that if your program causes syntax errors, you will get a better score by just submitting the original template unedited.

  • Submit your edited PA5.py file to the Programming Assignment 5 link on Canvas before 1:00 PM on 3/1/19. Do not submit the test CSV files.

  • below is the code :

  • 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 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.") 

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

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions