Answered step by step
Verified Expert Solution
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
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 """ 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. """ 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 MultipleChoiceQuestion: # TODO: make this a child class of another class defined in this file """ A question whose answers can be one of several options === 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, options: List[str]) -> None: """ Initialize a question with the text and id and possible answers . === Precondition === No two elements in are the same string contains at least two elements """ # TODO: complete the body of this method def __str__(self) -> str: """ Return a string representation of this question including the text of the question and a description of the possible answers. You can choose the precise format of this string. """ # TODO: complete the body of this method def validate_answer(self, answer: Answer) -> bool: """ Return True iff is a valid answer to this question. An answer is valid if its content is one of the possible answers to this question. """ # TODO: complete the body of this method def get_similarity(self, answer1: Answer, answer2: Answer) -> float: """ Return 1.0 iff .content and .content are equal and 0.0 otherwise. === Precondition === and are both valid answers to this question. """ # TODO: complete the body of this method class NumericQuestion: # TODO: make this a child class of another class defined in this file """ A question whose answer can be an integer between some minimum and maximum value (inclusive). === 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, min_: int, max_: int) -> None: """ Initialize a question with id and text whose possible answers can be any integer between and (inclusive) === Precondition === min_ < max_ """ # TODO: complete the body of this method def __str__(self) -> str: """ Return a string representation of this question including the text of the question and a description of the possible answers. You can choose the precise format of this string. """ # TODO: complete the body of this method def validate_answer(self, answer: Answer) -> bool: """ Return True iff the content of is an integer between the minimum and maximum (inclusive) possible answers to this question. """ # TODO: complete the body of this method def get_similarity(self, answer1: Answer, answer2: Answer) -> float: """ Return the similarity between and over the range of possible answers to this question. Similarity calculated by: 1. first find the absolute difference between .content and .content. 2. divide the value from step 1 by the difference between the maximimum and minimum possible answers. 3. subtract the value from step 2 from 1.0 Hint: this is the same calculation from the worksheet in lecture! For example: - Maximum similarity is 1.0 and occurs when == - Minimum similarity is 0.0 and occurs when is the minimum possible answer and is the maximum possible answer (or vice versa). === Precondition === and are both valid answers to this question """ # TODO: complete the body of this method class YesNoQuestion: # TODO: make this a child class of another class defined in this file """ A question whose answer is either yes (represented by True) or no (represented by False). === 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 and id . """ # TODO: complete the body of this method def __str__(self) -> str: """ Return a string representation of this question including the text of the question and a description of the possible answers. You can choose the precise format of this string. """ # TODO: complete the body of this method def validate_answer(self, answer: Answer) -> bool: """ Return True iff 's content is a boolean. """ # TODO: complete the body of this method def get_similarity(self, answer1: Answer, answer2: Answer) -> float: """ Return 1.0 iff .content is equal to .content and return 0.0 otherwise. === Precondition === and are both valid answers to this question """ # TODO: complete the body of this method 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 === Representation Invariants === text is not the empty string """ id: int text: str def __init__(self, id_: int, text: str, options: List[str]) -> None: """ Initialize a question with the text and id and possible answers . === Precondition === No two elements in are the same string contains at least two elements """ # TODO: complete the body of this method def __str__(self) -> str: """ Return a string representation of this question including the text of the question and a description of the possible answers. You can choose the precise format of this string. """ # TODO: complete the body of this method def validate_answer(self, answer: Answer) -> bool: """ Return True iff is a valid answer to this question. An answer is valid iff its content is a non-empty list containing unique possible answers to this question. """ # TODO: complete the body of this method 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 For example, if .content == ['a', 'b', 'c'] and .content == ['c', 'b', 'd'], the strings that are common to both are ['c', 'b'] and the unique strings that appear in both are ['a', 'b', 'c', 'd']. === Precondition === and are both valid answers to this question """ # TODO: complete the body of this method 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
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