File size: 2,793 Bytes
2b523d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
MCQ Validator and Scorer
"""
from typing import List, Dict

class MCQValidator:
    
    @staticmethod
    def validate_mcq(mcq: Dict) -> bool:
        """
        Validate if MCQ has all required fields
        
        Args:
            mcq: MCQ dictionary
            
        Returns:
            True if valid, False otherwise
        """
        required_fields = ['question', 'options', 'correct_answer']
        
        # Check required fields
        if not all(field in mcq for field in required_fields):
            return False
        
        # Check options
        if not isinstance(mcq['options'], dict):
            return False
        
        if len(mcq['options']) < 2:
            return False
        
        # Check correct answer
        if mcq['correct_answer'] not in mcq['options']:
            return False
        
        return True
    
    @staticmethod
    def score_answers(
        mcqs: List[Dict],
        user_answers: Dict[int, str]
    ) -> Dict:
        """
        Score user answers
        
        Args:
            mcqs: List of MCQs
            user_answers: Dict mapping question index to user's answer
            
        Returns:
            Scoring result dictionary
        """
        total_questions = len(mcqs)
        correct_count = 0
        results = []
        
        for i, mcq in enumerate(mcqs):
            user_answer = user_answers.get(i)
            correct_answer = mcq['correct_answer']
            is_correct = user_answer == correct_answer
            
            if is_correct:
                correct_count += 1
            
            results.append({
                'question_index': i,
                'question': mcq['question'],
                'user_answer': user_answer,
                'correct_answer': correct_answer,
                'is_correct': is_correct,
                'explanation': mcq.get('explanation', '')
            })
        
        score_percentage = (correct_count / total_questions * 100) if total_questions > 0 else 0
        
        return {
            'total_questions': total_questions,
            'correct_answers': correct_count,
            'incorrect_answers': total_questions - correct_count,
            'score_percentage': round(score_percentage, 2),
            'grade': MCQValidator._calculate_grade(score_percentage),
            'results': results
        }
    
    @staticmethod
    def _calculate_grade(score: float) -> str:
        """Calculate letter grade from score"""
        if score >= 90:
            return 'A+'
        elif score >= 80:
            return 'A'
        elif score >= 70:
            return 'B'
        elif score >= 60:
            return 'C'
        elif score >= 50:
            return 'D'
        else:
            return 'F'