Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

class Question: An abstract class representing a question used in a survey === Public Attributes === id: the id of this question text: the text

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 this question with the text .""" self.id = id_ self.text = 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. """ return 'Question : '+self.text

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.

Preconditions: - and are both valid answers to this question """ raise NotImplementedError

class CheckboxQuestion: # TODO: make this a child class of another class defined in this file """A question whose answers can be one or more of several options

=== Public Attributes === id: the id of this question text: the text of this question

=== Private Attributes === TODO: Describe any private attributes you create here """ id: int text: str

def __init__(self, id_: int, text: str, options: list[str]) -> None: """Initialize this question with the text and id and possible answers given in . """ # TODO: implement this method or remove it (to inherit it as is)

def __str__(self) -> str: """Return a string representation of this question including the text of the question and a description of the possible answers.

""" # TODO: implement this method or remove it (to inherit it as is)

def validate_answer(self, answer: Answer) -> bool: """Return True iff is a valid answer to this question.

An answer is valid iff: * It is a non-empty list. * It has no duplicate entries. * Every item in it is one of the answer options for this question. """ # TODO: implement this method or remove it (to inherit it as is)

def get_similarity(self, answer1: Answer, answer2: Answer) -> float: """Return the similarity between and .

Similarity is defined as the ratio between the number of strings that are common to both .content and .content over the total number of unique strings that appear in both .content and .content. If there are zero unique strings in common, return 1.0.

For example, if .content == ['a', 'b', 'c'] and .content == ['c', 'b', 'd'], there are 2 strings common to both: 'c' and 'b'; and there are 4 unique strings that appear in both: 'a', 'b', 'c', and 'd'. Therefore, the similarity between these two answers is 2/4 = 0.5.

Preconditions: - and are both valid answers to this question """ # TODO: implement this method or remove it (to inherit it as is) class Answer: """An answer to a question used in a survey

=== Public Attributes === """ content: Union[str, bool, int, list[str]]

def __init__(self, content: Union[str, bool, int, list[str]]) -> None: """Initialize this answer with content """ # TODO: implement this method!

def is_valid(self, question: Question) -> bool: """Return True iff this answer is a valid answer to """ # TODO: implement 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 a 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.

=== Representation Invariants === No two questions on this survey have the same id Each key in _questions equals the id attribute of its value The dictionaries _questions, _criteria, and _weights all have the same keys Each value in _weights is greater than 0

NOTE: The weights associated with the questions in a survey do NOT have to sum up to any particular amount. """ _questions: dict[int, Question] _criteria: dict[int, Criterion] _weights: dict[int, 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: implement this method!

def __len__(self) -> int: """Return the number of questions in this survey """ # TODO: implement this method!

def __contains__(self, question: Question) -> bool: """Return True iff there is a question in this survey with the same id as . """ # TODO: implement 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: implement this method!

def get_questions(self) -> list[Question]: """Return a list of all questions in this survey """ # TODO: implement this method!

def _get_criterion(self, question: Question) -> Criterion: """Return the criterion associated with in this survey.

Preconditions: - .id occurs in this survey """ # TODO: implement this method!

def _get_weight(self, question: Question) -> int: """Return the weight associated with in this survey.

Preconditions: - .id occurs in this survey """ # TODO: implement 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: implement 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: implement 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.

The score is determined using the following algorithm: 1. For each question in this survey, find the question's associated criterion (do we want homogeneous answers, for instance), weight, and answers to the question. Use the score_answers method for its criterion to calculate how well the answers satisfy the criterion. Multiply this quality score by the question's weight. 2. Find the average of all quality scores from step 1.

This method should NOT throw an InvalidAnswerError. If one occurs during the execution of this method or if there are no questions in , return zero.

Preconditions: - All students in have an answer to all questions in this survey - len(students) > 0 """ # TODO: implement this method!g

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 return 0.0. Otherwise, the score is determined using the following algorithm: 1. For each group in , calculate the score for the members of this based on their answers to the questions in this survey. 2. Return the average of all the scores calculated in step 1.

Preconditions: - All students in the groups in have an answer to all questions in this survey """ # TODO: implement this method!

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_2

Step: 3

blur-text-image_3

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

Modern Database Management

Authors: Jeff Hoffer, Ramesh Venkataraman, Heikki Topi

12th edition

133544613, 978-0133544619

More Books

Students also viewed these Databases questions