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
| from typing import List, Dict, Any
class QuestionService: """ 提供题目查询和格式化服务 [cite: 153] """
def __init__(self, questions: List[Dict[str, Any]]): self.questions = questions
def format_question_output(self, q_data: Dict[str, Any], show_answer: bool = False) -> str: """格式化单个题目输出 [cite: 153]""" question = q_data['detail']['question'] knowledge = q_data['detail'].get('knowledgePoint', {}) test_cases = q_data['detail'].get('questionTestList', [])
output = [ "=" * 60, f"ID: {question['id']}", f"标题: {question['title']}", f"类型: {question['type']} | 难度: {question['difficulty']}", f"知识点: {knowledge.get('name', 'N/A')}", "-" * 60, "📝 题目内容:", question['content'].strip(), f"\n测试用例 ({len(test_cases)}个):" ]
for idx, case in enumerate(test_cases[:3]): output.append(f" - 输入: {case.get('input', '无')} | 预期输出: {case.get('output', '无')}")
if show_answer: output.append("\n💡 标准答案:") output.append(question['answer'].strip())
output.append("=" * 60) return "\n".join(output)
def find_by_id(self, question_id: str) -> Dict[str, Any]: for item in self.questions: if item['id'] == question_id: return item return None
def find_by_type(self, q_type: str) -> List[Dict[str, Any]]: return [ item for item in self.questions if item['detail']['question']['type'] == q_type ]
def find_by_difficulty(self, difficulty: str) -> List[Dict[str, Any]]: return [ item for item in self.questions if item['detail']['question']['difficulty'] == difficulty ]
def search_by_keyword(self, keyword: str) -> List[Dict[str, Any]]: keyword = keyword.lower() results = [] for item in self.questions: question = item['detail']['question'] title = question['title'].lower() content = question['content'].lower() if keyword in title or keyword in content: results.append(item) return results
def get_statistics(self) -> tuple: """返回(类型集合, 难度集合) [cite: 159]""" types = set() difficulties = set() for item in self.questions: question = item['detail']['question'] types.add(question['type']) difficulties.add(question['difficulty']) return sorted(list(types)), sorted(list(difficulties))
|