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 """ # TODO: complete the body of this method

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 is a valid answer to this question. """ raise NotImplementedError

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 === and are both valid answers to this question """ raise NotImplementedError class Answer: """ An answer to a question used in a survey

=== 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 """ # TODO: complete the body of this method

def is_valid(self, question: Question) -> bool: """Return True iff self.content is a valid answer to """ # TODO: complete the body of this method

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. """ # TODO: complete the body of this method

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 . """ # 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']})

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 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 """ # TODO: complete the body of this method

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 and that answer is a valid answer for . """ # TODO: complete the body of this method

def set_answer(self, question: Question, answer: Answer) -> None: """ Record this student's answer to the question . """ # TODO: complete the body of this method

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 """ # TODO: complete the body of this method

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 . """ # TODO: complete the body of this method

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. """ # TODO: complete the body of this method

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 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 to the question .

Raise InvalidAnswerError if any answer in is not a valid answer to .

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 in order. Each slice is a list of size containing the next elements in .

The last slice may contain fewer than elements in order to make sure that the returned list contains all elements in .

=== 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 in order. Each window is a list of size containing the elements with index i through index i+ in the original list where i is the index of window in the returned list.

=== 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 using the questions in to create the grouping. """ raise NotImplementedError

plz use python finish TODO, thank u

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!