Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the function bodies with TODOs please, can you fix the initializer if not completely correct? Thank you! === Module Description === This file contains

Complete the function bodies with "TODO"s please, can you fix the initializer if not completely correct? Thank you! === Module Description === This file contains classes that describe a university course and the students who are enrolled in these courses. """ from __future__ import annotations from typing import TYPE_CHECKING, List, Tuple, Optional def sort_students(lst: List[Student], attribute: str) -> List[Student]: """ Return a shallow copy of sorted by === Precondition === is a attribute name for the Student class >>> s1 = Student(1, 'Misha') >>> s2 = Student(2, 'Diane') >>> s3 = Student(3, 'Mario') >>> sort_students([s1, s3, s2], 'id') == [s1, s2, s3] True >>> sort_students([s1, s2, s3], 'name') == [s2, s3, s1] True """ return sorted(lst, key=lambda s: getattr(s, attribute)) class Student: """ A Student who can be enrolled in a university course. === Public Attributes === id: the id of the student name: the name of the student === Representation Invariants === name is not the empty string """ id: int name: str def __init__(self, id_: int, name: str) -> None: """ Initialize a student with name and id """ pass def __str__(self) -> str: """ Return the name of this student """ pass def has_answer(self, question: Question) -> bool: """ Return True iff this student has an answer for a question with the same id as and that answer is a valid answer for . """ pass def set_answer(self, question: Question, answer: Answer) -> None: """ Record this student's answer to the question . """ pass def get_answer(self, question: Question) -> Optional[Answer]: """ Return this student's answer to the question . Return None if this student does not have an answer to """ pass class Course: """ A University Course === Public Attributes === name: the name of the course students: a list of students enrolled in the course === Representation Invariants === - No two students in this course have the same id - name is not the empty string """ name: str students: List[Student] def __init__(self, name: str) -> None: """ Initialize a course with the name of . """ self.name = name self.students = [] def enroll_students(self, students: List[Student]) -> None: """ Enroll all students in in this course. If adding any student would violate a representation invariant, do not add any of the students in to the course. """ self.student.append() # TODO: complete the function body def all_answered(self, survey: Survey) -> bool: """ Return True iff all the students enrolled in this course have a valid answer for every question in . """ # TODO: complete the body of this method def get_students(self) -> Tuple[Student, ...]: """ Return a tuple of all students enrolled in this course. The students in this tuple should be in order according to their id from lowest id to highest id. Hint: the sort_students function might be useful """ # TODO: complete the body of this method if __name__ == '__main__': import python_ta 

=========================================================================================

class Survey: """ A survey containing questions as well as criteria and weights used to evaluate the quality of a group based on their answers to the survey questions. === Private Attributes === _questions: a dictionary mapping each question's id to the question itself _criteria: a dictionary mapping a question's id to its associated criterion _weights: a dictionary mapping a question's id to a weight; an integer representing the importance of this criteria. _default_criterion: a criterion to use to evaluate a question if the question does not have an associated criterion in _criteria _default_weight: a weight to use to evaluate a question if the question does not have an associated weight in _weights === Representation Invariants === No two questions on this survey have the same id Each key in _questions equals the id attribute of its value Each key in _criteria occurs as a key in _questions Each key in _weights occurs as a key in _questions Each value in _weights is greater than 0 _default_weight > 0 """ _questions: Dict[int, Question] _criteria: Dict[int, Criterion] _weights: Dict[int, int] _default_criterion: Criterion _default_weight: int def __init__(self, questions: List[Question]) -> None: """ Initialize a new survey that contains every question in . This new survey should use a HomogeneousCriterion as a default criterion and should use 1 as a default weight. """ self._questions = Dict[int, Question] self._criteria = Dict[int, any] self._weights = Dict[int, int] self._default_criterion = [] self._default_weight = 0 def __len__(self) -> int: """ Return the number of questions in this survey """ return len(self._questions) def __contains__(self, question: Question) -> bool: """ Return True iff there is a question in this survey with the same id as . """ # TODO: complete the body of this method def __str__(self) -> str: """ Return a string containing the string representation of all questions in this survey You can choose the precise format of this string. """ # TODO: complete the body of this method def get_questions(self) -> List[Question]: """ Return a list of all questions in this survey """ # TODO: complete the body of this method def _get_criterion(self, question: Question) -> Criterion: """ Return the criterion associated with in this survey. Iff .id does not appear in self._criteria, return the default criterion for this survey instead. === Precondition === .id occurs in this survey """ # TODO: complete the body of this method def _get_weight(self, question: Question) -> int: """ Return the weight associated with in this survey. Iff .id does not appear in self._weights, return the default weight for this survey instead. === Precondition === .id occurs in this survey """ # TODO: complete the body of this method def set_weight(self, weight: int, question: Question) -> bool: """ Set the weight associated with to and return True. If .id does not occur in this survey, do not set the and return False instead. """ # TODO: complete the body of this method def set_criterion(self, criterion: Criterion, question: Question) -> bool: """ Set the criterion associated with to and return True. If .id does not occur in this survey, do not set the and return False instead. """ # TODO: complete the body of this method def score_students(self, students: List[Student]) -> float: """ Return a quality score for calculated based on their answers to the questions in this survey, and the associated criterion and weight for each question . This score is determined using the following algorithm: 1. For each question in , find its associated criterion, weight, and answers to this question. Use the score_answers method for this criterion to calculate a quality score. Multiply this quality score by the associated weight. 2. Find the average of all quality scores from step 1. If an InvalidAnswerError would be raised by calling this method, or if there are no questions in , this method should return zero. === Precondition === All students in have an answer to all questions in this survey """ # TODO: complete the body of this method def score_grouping(self, grouping: Grouping) -> float: """ Return a score for calculated based on the answers of each student in each group in to the questions in . If there are no groups in this score is 0.0. Otherwise, this score is determined using the following algorithm: 1. For each group in , get the score for the members of this group calculated based on their answers to the questions in this survey. 2. Return the average of all the scores calculated in step 1. === Precondition === All students in the groups in have an answer to all questions in this survey """ # TODO: complete the body of this method if __name__ == '__main__': import python_ta python_ta.check_all(config={'extra-imports': ['typing', 'criterion', 'course', 'grouper']}) 

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

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions

Question

1. What are your creative strengths?

Answered: 1 week ago

Question

What metaphors might describe how we work together?

Answered: 1 week ago