diff --git a/aufgabengenerator_brueche_addieren_ungleichnamig_v6.py b/aufgabengenerator_brueche_addieren_ungleichnamig_v6.py index 14463cf..e47d564 100644 --- a/aufgabengenerator_brueche_addieren_ungleichnamig_v6.py +++ b/aufgabengenerator_brueche_addieren_ungleichnamig_v6.py @@ -1,8 +1,34 @@ import xml.etree.ElementTree as ET import random from math import gcd +import re -def create_cloze_question(name, questiontext, correct_answers, wrong_answers, feedback, tags): +def create_html_table_for_fracture_input(rows: list): + table_rows = [] + # Tabellenzeilen festlegen + for i, row in enumerate(rows): + if (i % 2) == 0: + table_row = "
Zähler: {{1:SHORTANSWER:={correct_answers[0]}~%0%{wrong_answers[0][0]}#{wrong_answers[0][1]}}} | " - cloze_text += "
Nenner: {{1:SHORTANSWER:={correct_answers[1]}~%0%{wrong_answers[1][0]}#{wrong_answers[1][1]}}} | " - cloze_text += "
Gekürzter Zähler: {{1:SHORTANSWER:={reduced_numerator}~%0%{wrong_answers[2][0]}#{wrong_answers[2][1]}}} | " - cloze_text += "
Gekürzter Nenner: {{1:SHORTANSWER:={reduced_denominator}~%0%{wrong_answers[3][0]}#{wrong_answers[3][1]}}} | " - cloze_text += "
" # Musterlösung im LaTeX-Format if gcd(correct_answers[0], correct_answers[1]) > 1: - solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{reduced_numerator}}}{{{reduced_denominator}}} \\)." + solution += f"\\( {question_math} {calcstep_math} = \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} = \\frac{{{reduced_numerator}}}{{{reduced_denominator}}} \\)." else: - solution = f"Die Musterlösung ist: \\( \\Large \\displaystyle \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} \\)." + solution += f"\\( {question_math} {calcstep_math} = \\frac{{{correct_answers[0]}}}{{{correct_answers[1]}}} \\)" + solution += "
" text_elem.text = solution @@ -97,7 +121,7 @@ for i in range(1, (n+1)): # Berechne den gemeinsamen Nenner, der kleiner als 30 sein muss common_denominator = lcm(denominator1, lcm(denominator2, denominator3)) - while common_denominator >= 30: + while common_denominator >= 50: denominator1 = random.randint(2, 10) denominator2 = random.randint(2, 10) denominator3 = random.randint(2, 10) @@ -121,10 +145,14 @@ for i in range(1, (n+1)): # Prüfe, ob der Bruch gekürzt werden kann needs_reduction = gcd(result_numerator, common_denominator) > 1 + question_puremath = f"\\Large \\displaystyle \\frac{{{numerator1}}}{{{denominator1}}} {operations[0]} \\left(\\frac{{{numerator2}}}{{{denominator2}}} {operations[2]} \\frac{{{numerator3}}}{{{denominator3}}}\\right) =" + # Frage im LaTeX-Format questiontext = (f"Berechne und kürze das Ergebnis, wenn möglich:
" - f"\\( \\Large \\displaystyle \\frac{{{numerator1}}}{{{denominator1}}} {operations[0]} " - f"\\left(\\frac{{{numerator2}}}{{{denominator2}}} {operations[2]} \\frac{{{numerator3}}}{{{denominator3}}}\\right) = \\)
") + f"\\( {question_puremath} \\)
") + + # Feedback-Ausgabe für notwendigen Zwischenschritt + calculation_step_math = f"\\frac{{{equivalent_numerator1}}}{{{common_denominator}}} {operations[0]} \\left(\\frac{{{equivalent_numerator2}}}{{{common_denominator}}} {operations[2]} \\frac{{{equivalent_numerator3}}}{{{common_denominator}}}\\right)" # Antworten correct_answers = [result_numerator, common_denominator] @@ -153,6 +181,8 @@ for i in range(1, (n+1)): questions.append(create_cloze_question( f"Addition/Subtraktion von 3 ungleichnamigen Brüchen {i}", questiontext, + question_puremath, + calculation_step_math, correct_answers, wrong_answers, feedback, @@ -165,4 +195,4 @@ quiz = create_quiz(questions) # Speichere die XML-Datei save_to_file(quiz, 'cloze_fraction_addition_subtraction_quiz.xml') -print("Moodle-XML-Datei 'cloze_fraction_addition_subtraction_quiz.xml' erfolgreich erstellt.") \ No newline at end of file +print("Moodle-XML-Datei 'cloze_fraction_addition_subtraction_quiz.xml' erfolgreich erstellt.") diff --git a/aufgabengenerator_formuals_2brueche_addieren_ungleichnamig_v1.py b/aufgabengenerator_formuals_2brueche_addieren_ungleichnamig_v1.py new file mode 100644 index 0000000..d1017f9 --- /dev/null +++ b/aufgabengenerator_formuals_2brueche_addieren_ungleichnamig_v1.py @@ -0,0 +1,187 @@ +import random +from math import gcd +import xml.etree.ElementTree as ET + +def lcm(a, b): + """Berechnet das kleinste gemeinsame Vielfache.""" + return abs(a * b) // gcd(a, b) + +def create_formula_question(numerator1, denominator1, numerator2, denominator2, n): + """Erzeugt eine Frage im Format 'Formulas'.""" + # GCD und das kleinste gemeinsame Vielfache für die Denominator + common_denominator = lcm(denominator1, denominator2) + new_numerator = (numerator1 * (common_denominator // denominator1)) + (numerator2 * (common_denominator // denominator2)) + + question = ET.Element('question', attrib={'type': 'formulas'}) + + name_elem = ET.SubElement(question, 'name') + text_elem = ET.SubElement(name_elem, 'text') + text_elem.text = 'Addition von 2 ungleichnamigen Brüchen {}'.format(n) + + questiontext_elem = ET.SubElement(question, 'questiontext', attrib={'format': 'html'}) + text_elem = ET.SubElement(questiontext_elem, 'text') + text_elem.text = '''Addiere die Brüche und kürze das Ergebnis so weit wie möglich. +{#A}
]]> ''' + + # Allgemeines Feedback + generalfeedback_elem = ET.SubElement(question, 'generalfeedback', attrib={'format': 'html'}) + text_elem = ET.SubElement(generalfeedback_elem, 'text') + text_elem.text = ' ' + + # Grundlegende Bewertungsinformationen + ET.SubElement(question, 'defaultgrade').text = '1.0000000' + ET.SubElement(question, 'penalty').text = '0.3333333' + ET.SubElement(question, 'hidden').text = '0' + ET.SubElement(question, 'idnumber').text = ' ' + + # Feedback für korrekte und inkorrekte Antworten + correctfeedback_elem = ET.SubElement(question, 'correctfeedback', attrib={'format': 'html'}) + text_elem = ET.SubElement(correctfeedback_elem, 'text') + text_elem.text = '''Die Antwort ist richtig. ]]> ''' + + partiallycorrectfeedback_elem = ET.SubElement(question, 'partiallycorrectfeedback', attrib={'format': 'html'}) + text_elem = ET.SubElement(partiallycorrectfeedback_elem, 'text') + text_elem.text = '''Die Antwort ist teilweise richtig. ]]> ''' + + incorrectfeedback_elem = ET.SubElement(question, 'incorrectfeedback', attrib={'format': 'html'}) + text_elem = ET.SubElement(incorrectfeedback_elem, 'text') + text_elem.text = '''Die Antwort ist falsch. ]]>''' + + ET.SubElement(question, 'shownumcorrect') + + varsrandom_elem = ET.SubElement(question, 'varsrandom') + text_elem = ET.SubElement(varsrandom_elem, 'text') + text_elem.text = f' ' + + # Globale Variablen definieren + varsglobal_elem = ET.SubElement(question, 'varsglobal') + text_elem = ET.SubElement(varsglobal_elem, 'text') + text_elem.text = f'n = [{numerator1}, {numerator2}];\n d = [{denominator1}, {denominator2}];' + + answernumbering_elem = ET.SubElement(question, 'answernumbering') + text_elem = ET.SubElement(answernumbering_elem, 'text') + text_elem.text = 'ABCD' + + # Antworten definieren + answers_elem = ET.SubElement(question, 'answers') + + # Indizes für die Antworten + partindex_elem = ET.SubElement(answers_elem, 'partindex') + ET.SubElement(partindex_elem, 'text').text = '0' + + placeholder_elem = ET.SubElement(answers_elem, 'placeholder') + ET.SubElement(placeholder_elem, 'text').text = '#A' + + answermark_elem = ET.SubElement(answers_elem, 'answermark') + ET.SubElement(answermark_elem, 'text').text = '1' + + answertype_elem = ET.SubElement(answers_elem, 'answertype') + ET.SubElement(answertype_elem, 'text').text = '0' + + numbox_elem = ET.SubElement(answers_elem, 'numbox') + ET.SubElement(numbox_elem, 'text').text = '2' + + # Berechnungsvariablen + vars1_elem = ET.SubElement(answers_elem, 'vars1') + vars1_elem_text = f'g = gcd(d[0],d[1]);\ncd = d[0]*d[1]/pow(g,2);\ne = [cd / d[0], cd / d[1]];\nnums = [n[0]*e[0], n[1]*e[1]];\nthesumnum = nums[0] + nums[1]' + ET.SubElement(vars1_elem, 'text').text = vars1_elem_text + + # Antwort + answer_elem = ET.SubElement(answers_elem, 'answer') + answer_text = f'[thesumnum, cd]' + ET.SubElement(answer_elem, 'text').text = answer_text + + answernotunique_elem = ET.SubElement(answers_elem, 'answernotunique') + ET.SubElement(answernotunique_elem, 'text').text = '0' + + vars2_elem = ET.SubElement(answers_elem, 'vars2') + vars2_elem_text = u"" + ET.SubElement(vars2_elem, 'text').text = vars2_elem_text + + correctness_elem = ET.SubElement(answers_elem, 'correctness') + ET.SubElement(correctness_elem, 'text').text = 'ga + 0.5*gb' + + unitpenalty_elem = ET.SubElement(answers_elem, 'unitpenalty') + ET.SubElement(unitpenalty_elem, 'text').text = '1' + + postunit_elem = ET.SubElement(answers_elem, 'postunit') + ET.SubElement(postunit_elem, 'text').text = ' ' + + ruleid_elem = ET.SubElement(answers_elem, 'ruleid') + ET.SubElement(ruleid_elem, 'text').text = '1' + + otherrule_elem = ET.SubElement(answers_elem, 'otherrule') + ET.SubElement(otherrule_elem, 'text').text = ' ' + + # Unterfragen-Text + subqtext_elem = ET.SubElement(answers_elem, 'subqtext', attrib={'format': 'html'}) + sub_question_text = '''Berechne:\\(\\Large \\displaystyle \\frac{{n[0]}}{{d[0]}} + \\frac{{n[1]}}{{d[1]}} =\\) {_0} / {_1}
]]> ''' + ET.SubElement(subqtext_elem, 'text').text = sub_question_text + + # Lösungshinweis + feedback_elem = ET.SubElement(answers_elem, 'feedback', attrib={'format': 'html'}) + feedback_text = '''Lösungshinweis +Die korrekte Lösung ist:
+\\(\\Large \\displaystyle \\frac{{n[0]}}{{d[0]}} + \\frac{{n[1]}}{{d[1]}} = \\frac{{nums[0]}}{{cd}} + \\frac{{nums[1]}}{{cd}} = \\frac{{thesumnum}}{{cd}}\\)
]]>''' + ET.SubElement(feedback_elem, 'text').text = feedback_text + + # correctfeedback + + correctfeedback_elem = ET.SubElement(answers_elem, 'correctfeedback', attrib={'format': 'html'}) + correctfeedback_text = '''Richtig!]]>''' + ET.SubElement(correctfeedback_elem, 'text').text = correctfeedback_text + + # partially correct feedback + + pcorrectfeedback_elem = ET.SubElement(answers_elem, 'partiallycorrectfeedback', attrib={'format': 'html'}) + pcorrectfeedback_text = '''Teilweise richtig.]]>''' + ET.SubElement(pcorrectfeedback_elem, 'text').text = pcorrectfeedback_text + + # incorrect feedback + incorrectfeedback_elem = ET.SubElement(answers_elem, 'incorrectfeedback', attrib={'format': 'html'}) + incorrectfeedback_text = '''Leider nicht richtig.]]>''' + ET.SubElement(incorrectfeedback_elem, 'text').text = incorrectfeedback_text + + return question + +def generate_questions(num_questions): + """Generiert Aufgaben für die Addition ungleichnamiger Brüche.""" + questions = [] + + for i in range(num_questions): + numerator1 = random.randint(1, 10) + denominator1 = random.randint(2, 10) + numerator2 = random.randint(1, 10) + denominator2 = random.randint(2, 10) + + # Sicherstellen, dass die Nenner unterschiedlich sind + while denominator1 == denominator2: + denominator2 = random.randint(2, 10) + + question = create_formula_question(numerator1, denominator1, numerator2, denominator2, i) + questions.append(question) + + return questions + +def create_quiz(questions): + """Erzeugt die gesamte Quiz-Struktur.""" + quiz = ET.Element('quiz') + for q in questions: + quiz.append(q) + return quiz + +def save_to_file(xml_element, filename): + """Speichert das XML-Dokument in einer Datei.""" + tree = ET.ElementTree(xml_element) + with open(filename, 'wb') as f: + tree.write(f, encoding='utf-8', xml_declaration=True) + +# Hauptprogramm +if __name__ == "__main__": + num_questions = 1 # Anzahl der zu generierenden Aufgaben + questions = generate_questions(num_questions) + quiz = create_quiz(questions) + + save_to_file(quiz, 'formulas_fraction_addition_quiz.xml') + + print("Moodle-XML-Datei 'formulas_fraction_addition_quiz.xml' erfolgreich erstellt.") diff --git a/formulas_fraction_addition_quiz.xml b/formulas_fraction_addition_quiz.xml new file mode 100644 index 0000000..afb316a --- /dev/null +++ b/formulas_fraction_addition_quiz.xml @@ -0,0 +1,10 @@ + +{#A}
]]> +\(\Large \displaystyle \frac{{n[0]}}{{d[0]}} + \frac{{n[1]}}{{d[1]}} =\) {_0} / {_1}
]]> +Die korrekte Lösung ist:
\(\Large \displaystyle \frac{{n[0]}}{{d[0]}} + \frac{{n[1]}}{{d[1]}} = \frac{{nums[0]}}{{cd}} + \frac{{nums[1]}}{{cd}} = \frac{{thesumnum}}{{cd}}\)
]]> +