Question: from __future__ import annotations from typing import TYPE_CHECKING, Union, Dict, List from criterion import HomogeneousCriterion, InvalidAnswerError if TYPE_CHECKING: from criterion import Criterion from grouper import
from __future__ import annotations from typing import TYPE_CHECKING, Union, Dict, List from criterion import HomogeneousCriterion, InvalidAnswerError if TYPE_CHECKING: from criterion import Criterion from grouper import Grouping from course import Student
class Question: """ An abstract class representing a question used in a survey
=== Public Attributes === id: the id of this question text: the text of this question
=== Representation Invariants === text is not the empty string """
id: int text: str
def __init__(self, id_: int, text: str) -> None: """ Initialize a question with the text
def __str__(self) -> str: """ Return a string representation of this question that contains both the text of this question and a description of all possible answers to this question.
You can choose the precise format of this string. """ raise NotImplementedError
def validate_answer(self, answer: Answer) -> bool: """ Return True iff
def get_similarity(self, answer1: Answer, answer2: Answer) -> float: """ Return a float between 0.0 and 1.0 indicating how similar two answers are.
=== Precondition ===
=== Public Attributes === content: an answer to a single question """ content: Union[str, bool, int, List[str]]
def __init__(self, content: Union[str, bool, int, List[Union[str]]]) -> None: """Initialize an answer with content
def is_valid(self, question: Question) -> bool: """Return True iff self.content is a valid answer to
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
def __len__(self) -> int: """ Return the number of questions in this survey """ # TODO: complete the body of this method
def __contains__(self, question: Question) -> bool: """ Return True iff there is a question in this survey with the same id as
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
Iff
=== Precondition ===
def _get_weight(self, question: Question) -> int: """ Return the weight associated with
Iff
=== Precondition ===
def set_weight(self, weight: int, question: Question) -> bool: """ Set the weight associated with
If
def set_criterion(self, criterion: Criterion, question: Question) -> bool: """ Set the criterion associated with
If
def score_students(self, students: List[Student]) -> float: """ Return a quality score for
This score is determined using the following algorithm:
1. For each question in
If an InvalidAnswerError would be raised by calling this method, or if there are no questions in
=== Precondition === All students in
def score_grouping(self, grouping: Grouping) -> float: """ Return a score for
If there are no groups in
1. For each group in
=== Precondition === All students in the groups in
if __name__ == '__main__': import python_ta python_ta.check_all(config={'extra-imports': ['typing', 'criterion', 'course', 'grouper']})
ANOTHER
from __future__ import annotations from typing import TYPE_CHECKING, List, Tuple, Optional if TYPE_CHECKING: from survey import Answer, Survey, Question
def sort_students(lst: List[Student], attribute: str) -> List[Student]: """ Return a shallow copy of
=== Precondition ===
>>> 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
def __str__(self) -> str: """ Return the name of this student """ # TODO: complete the body of this method
def has_answer(self, question: Question) -> bool: """ Return True iff this student has an answer for a question with the same id as
def set_answer(self, question: Question, answer: Answer) -> None: """ Record this student's answer
def get_answer(self, question: Question) -> Optional[Answer]: """ Return this student's answer to the question
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
def enroll_students(self, students: List[Student]) -> None: """ Enroll all students in
If adding any student would violate a representation invariant, do not add any of the students in
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
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 python_ta.check_all(config={'extra-imports': ['typing', 'survey']})
class Criterion: """ An abstract class representing a criterion used to evaluate the quality of a group based on the group members' answers for a given question. """
def score_answers(self, question: Question, answers: List[Answer]) -> float: """ Return score between 0.0 and 1.0 indicating the quality of the group of
Raise InvalidAnswerError if any answer in
Each implementation of this abstract class will measure quality differently. """ raise NotImplementedError
def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of
The last slice may contain fewer than
=== Precondition === n <= len(lst)
>>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [False]] True """ # TODO: complete the body of this function
def windows(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing windows of
=== Precondition === n <= len(lst)
>>> windows([3, 4, 6, 2, 3], 2) == [[3, 4], [4, 6], [6, 2], [2, 3]] True >>> windows(['a', 1, 6.0, False], 3) == [['a', 1, 6.0], [1, 6.0, False]] True """ # TODO: complete the body of this function
class Grouper: """ An abstract class representing a grouper used to create a grouping of students according to their answers to a survey.
=== Public Attributes === group_size: the ideal number of students that should be in each group
=== Representation Invariants === group_size > 1 """
group_size: int
def __init__(self, group_size: int) -> None: """ Initialize a grouper that creates groups of size
=== Precondition === group_size > 1 """ # TODO: complete the body of this method
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in
plz use python finish TODO, thank u
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
