Example #1
0
 def has_answer(self, question: Question) -> bool:
     """
     Return True iff this student has an answer for a question with the same
     id as <question> and that answer is a valid answer for <question>.
     """
     return question.id in self._answers and \
         question.validate_answer(self._answers[question.id])
Example #2
0
    def score_answers(self, question: Question, answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        for answer in answers:
            if not question.validate_answer(answer):
                raise InvalidAnswerError
        if len(answers) == 1:
            return 1.0
        score = 0
        permute = 0
        for ans in answers:
            i = answers.index(ans)
            for other in answers:
                j = answers.index(other)
                if i < j:
                    score += question.get_similarity(ans, other)
                    permute += 1
        return score / permute
Example #3
0
    def score_answers(self, question: Question,
                      answers: List[Answer]) -> float:
        """
        Return a score between 0.0 and 1.0 indicating how similar the answers in
        <answers> are.

        This score is calculated by finding the similarity of every
        combination of two answers in <answers> and taking the average of all
        of these similarity scores.

        If there is only one answer in <answers> and it is valid return 1.0
        since a single answer is always identical to itself.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        for ans in answers:
            if question.validate_answer(ans) is False:
                raise InvalidAnswerError
        if len(answers) == 1:
            return 1.0
        else:
            count = 0.0
            count_num = 0
            for index_a, v1 in enumerate(answers):
                for index_b in range(index_a + 1, len(answers)):
                    count += question.get_similarity(v1, answers[index_b])
                    count_num += 1
            return count / count_num  # yield [v1, answers[index_b]]
Example #4
0
    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
        <answers> to the question <question>.

        The score returned will be zero iff there are any unique answers in
        <answers> and will be 1.0 otherwise.

        An answer is not unique if there is at least one other answer in
        <answers> with identical content.

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.

        === Precondition ===
        len(answers) > 0
        """
        for ans in answers:
            if question.validate_answer(ans) is False:
                raise InvalidAnswerError
        if len(answers) == 1:
            return 0.0
        else:
            new_list = []
            for ans in answers:
                if ans.content not in new_list:
                    new_list.append(ans.content)
            if len(new_list) > 1:
                return 0.0
            else:
                return 1.0
Example #5
0
 def set_answer(self, question: Question, answer: Answer) -> None:
     """
     Record this student's answer <answer> to the question <question>.
     Do nothing if answer is not valid for this question.
     """
     # Validate answer for question
     if question.validate_answer(answer):
         self._answers[question.id] = answer
Example #6
0
def invalid_answer(question: Question, answers: List[Answer]) -> None:
    """
    Raise InvalidAnswerError if any answer in <answers> is not a valid
    answer to <question>.
    """
    for answer in answers:
        if not question.validate_answer(answer):
            raise InvalidAnswerError
Example #7
0
 def has_answer(self, question: Question) -> bool:
     """
     Return True iff this student has an answer for a question with the same
     id as <question> and that answer is a valid answer for <question>.
     """
     # TODO: complete the body of this method
     if question.id in self._responses:
         return question.validate_answer(self._responses[question.id][1])
     return False
Example #8
0
 def has_answer(self, question: Question) -> bool:
     """
     Return True iff this student has an answer for a question with the same
     id as <question> and that answer is a valid answer for <question>.
     """
     for answer in self._answers:
         if answer[0].id == question.id \
                 and question.validate_answer(answer[1]):
             return True
     return False
Example #9
0
 def has_answer(self, question: Question) -> bool:
     """
     Return True iff this student has an answer for a question with the same
     id as <question> and that answer is a valid answer for <question>.
     """
     # TODO: complete the body of this method
     if self._ans.get(question):
         answer = self._ans.get(question)
         return question.validate_answer(answer)
     return False
Example #10
0
    def check_answers(self, question: Question, answers: List[Answer]) -> None:
        """
        Takes a question and a list of answers, and check if they're all valid

        Raise InvalidAnswerError if any answer in <answers> is not a valid
        answer to <question>.
        """
        for answer in answers:
            if not question.validate_answer(answer):
                raise InvalidAnswerError
Example #11
0
def only_one_valid(question: Question, answers: List[Answer]) -> bool:
    """
    Evaluate if there is only one answer in <answers> and it is valid.
    """
    return len(answers) == 1 and question.validate_answer(answers[0])