Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Instructions - Please follow all instructions closely. There is no penalty for going above and beyond the assignment requirements. However, failure to meet all required
Instructions - Please follow all instructions closely. There is no penalty for going "above and beyond" the assignment requirements. However, failure to meet all required parts can affect your grade. Please consult the rubric for each section. - Either raw Python (*.py) or Jupyter Notebook (*.jpynb) files are acceptable for this lab. - A single file may be used for this project. Part I Create a Student class which will contain a dictionary and be used in lists. It inherits from object: class Student(object): - _ init_() signature: def _init_(self, id, firstName, lastName, courses = None): - The "id", "firstName" and "lastName" parameters are to be directly assigned to member variables (ie: self.id = id) - The "courses" parameter is handled differently. If it is None, assign dict() to self.courses, otherwise assign courses directly to the member variable. - Note: The "courses" dictionary contains key/value pairs where the key is a string that is the course number (like "CSE-123") and the value is a number from 0-4.0 (represents the grade the student received). - gpa() signature: def gpa(self): - This method calculates the cumulative grade point average for the student. - This can be done by looping through the courses member variable and summing the values in that dictionary then dividing by the count of items in courses. - If self.courses is empty, just return 0. - addCourse() signature: def addcourse(self, course, score): - This method adds a new entry into the self.courses member variable where "course" is the item's key and "score" is its value. - Use assertions to ensure "score" is a numeric type and is between 0 and 4 - addCourses() signature: def addcourses(self, courses): - This method appends the given dictionary (called "courses") to the member variable self.courses. Hint: Check the dictionary update method. - Note: This may bother you since the passed in parameter has the same name as the member variable defined in _ init .. However, remember scope plays a part here. The one defined in init _ can only be accessed using "self.courses" which allows Python to figure out which one to use. - Use assertions to ensure "courses" is a dictionary (type dict) - str() (Tis - This method should output a formatted string such that printing multiple Student objects will line up. Example result for 3 objects (fields shown: id, lastName, firstName, GPA, courses): - _ _repr (): We will not directly use this method, but it should have an output similar to _str However, the column formatting is not necessary. You can use commas in between fields: 123456, Smith, Johnnie, \{'CSE-101': 3.5, 'CSE-102': 3, 'CSE-201': 4, 'CSE-220': 3.75, 'CSE-325': 4\} - header() signature: def header(): This method returns a string that can used as a header for the results of the _str method. Notice that this is a class method because there is no "self" parameter, so it will be called using Student.header(). Example result: ID Last Name First Name GPA Courses Extra Credit - Use reduce() function in the ga() method when calculating the sum before dividing. Part II Create a list of Student objects and populate it. Then create a method to print the student list with a header. - The list should be declared using: students=[] - Use the following data when populating students: - Create a method called printStudents() that prints the header defined in part I and prints all the students under it. - Signature: def printstudents(students): - Camaria mitmin. Requirements - At least 3 students must be created without passing an argument to __init__s courses parameter. - The courses then must be added using individual calls to the student object's addCourse() method. - At least 3 students must be created without passing an argument to _ init s courses parameter. - The courses then must be added using a call to the student object's addCourses() method using a dictionary. - The remaining 4 student objects are to be created where the courses are passed to _ init _'s courses parameter which is a dictionary containing all the courses and scores. S Part III Use comprehensions, lambdas and loops to query the student list. - Query 1: Sort the list by lastName, firstName, both in ascending order, and print the results using printStudents(). - Query 2: - Sort the list by GPA in descending order and print the results using printStudents(). - Query 3: - Create a set that contains all unique courses taken by all students and print the set. - Can use set comprehension (see extra credit). - Note: there are 17 unique courses - Query 4: Get a list of students who have taken 'CSE-201' and print the list using printStudents(). Must use list comprehension. Note: there should be 6 students. - Query 5: Get a list of "honor roll" students (GPA >= 3.5) and print the list using printStudents(). - Must use list comprehension. - Note: there should be 4 students. Extra Credit: Implement query 3 using only a set comprehension. It can have nested "for" statements, but only a single set comprehension
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started