questions.mojo (2705B)
1 from std.collections import List 2 3 4 @fieldwise_init 5 struct Question(Copyable, Movable): 6 var id: String 7 var kind: String 8 var instructions: String 9 var choices: List[String] 10 var rubric: List[String] 11 12 13 @fieldwise_init 14 struct QuestionBundle(Copyable, Movable): 15 var bundle_id: String 16 var version: String 17 var model: String 18 var questions: List[Question] 19 20 21 def _copy_strings(items: List[String]) -> List[String]: 22 var copied = List[String]() 23 for item in items: 24 copied.append(String(item)) 25 return copied^ 26 27 28 def choice_question( 29 id: String, instructions: String, choices: List[String] 30 ) raises -> Question: 31 if id.strip() == "" or instructions.strip() == "": 32 raise Error("choice question requires id and instructions") 33 if len(choices) < 2: 34 raise Error("choice question requires at least two choices") 35 return Question( 36 id=String(id), 37 kind="choice", 38 instructions=String(instructions), 39 choices=_copy_strings(choices), 40 rubric=List[String](), 41 ) 42 43 44 def noul_question(id: String, instructions: String) raises -> Question: 45 if id.strip() == "" or instructions.strip() == "": 46 raise Error("noul question requires id and instructions") 47 return Question( 48 id=String(id), 49 kind="noul", 50 instructions=String(instructions), 51 choices=List[String](), 52 rubric=List[String](), 53 ) 54 55 56 def score_question( 57 id: String, instructions: String, rubric: List[String] 58 ) raises -> Question: 59 if id.strip() == "" or instructions.strip() == "": 60 raise Error("score question requires id and instructions") 61 if len(rubric) < 2: 62 raise Error("score question requires at least two rubric levels") 63 return Question( 64 id=String(id), 65 kind="score", 66 instructions=String(instructions), 67 choices=List[String](), 68 rubric=_copy_strings(rubric), 69 ) 70 71 72 def question_bundle( 73 bundle_id: String, version: String, model: String, questions: List[Question] 74 ) raises -> QuestionBundle: 75 if bundle_id.strip() == "" or version.strip() == "" or model.strip() == "": 76 raise Error("question bundle requires id, version and model") 77 if len(questions) == 0: 78 raise Error("question bundle must contain at least one question") 79 var copied = List[Question]() 80 for question in questions: 81 copied.append(question.copy()) 82 return QuestionBundle( 83 bundle_id=String(bundle_id), 84 version=String(version), 85 model=String(model), 86 questions=copied^, 87 ) 88 89 90 def bundle_pins_configuration(bundle: QuestionBundle) -> Bool: 91 return bundle.version != "" and bundle.model != ""