import random from math import gcd, lcm import xml.etree.ElementTree as ET from collections import Counter # Konstanten # Anzahl der zu generierenden Fragen. NUM_QUESTIONS = 1 # Algebraische Struktur der Aufgaben STRUCTURE = "-(+)" # TODO # Platzhalter hinzufügen # Anzahl der benötigten Brüche NUM_FRACTIONS = 1 + STRUCTURE.count("+") + STRUCTURE.count("+") + STRUCTURE.count("*") + STRUCTURE.count("/") # Größter Zähler MAX_NUMERATOR = 20 # Größter Nenner MAX_DENOMINATOR = 20 # Größter erlaubter gemeinsamer Nenner MAX_COMMON_DENOMINATOR = 100 # Dateiname für Ausgabe FILENAME = 'formulas_new_quiz.xml' def create_formula_question(n): numerators = [] denominators = [] for i in range(NUM_FRACTIONS): n = random.randint(1, MAX_NUMERATOR) d = random.randint(2, MAX_DENOMINATOR) numerators.append(n) denominators.append(d) # Sicherstellen, dass die Nenner unterschiedlich sind same_denominators = [k for k,v in Counter(denominators).items() if v>1] while len(same_denominators) > 1: new_denom = random.randint(2, MAX_DENOMINATOR) denominators.replace(same_denominators[0], new_denom) same_denominators = [k for k,v in Counter(denominators).items() if v>1] # [DEBUG] print(numerators) print(denominators) # [DEBUG] *denoms_integers, = denominators common_denominator = lcm(*denoms_integers) fractions = [] for i in range(NUM_FRACTIONS): fraction = {"n": numerators[i], "d": denominators[i]} fractions.append(fraction) # [DEBUG] print(fractions) # [DEBUG] cd = common_denominator expanded_numerators = [frac["n"] * cd // frac["d"] for frac in fractions] expanded_fractions = [] for i in range(NUM_FRACTIONS): fraction = {"n": expanded_numerators[i], "d": cd} expanded_fractions.append(fraction) # [DEBUG] print(expanded_fractions) # [DEBUG] eval_string = f"{expanded_numerators[0]}" cnt_next_fraction = 1 for c in STRUCTURE: if c == "(" or c ==")": eval_string += c elif c == "+" or c == "-" or c == "*" or c == "/": eval_string += c eval_string += f"{expanded_numerators[cnt_next_fraction]}" cnt_next_fraction += 1 # [DEBUG] print(eval_string) # [DEBUG] result_numerator = eval(eval_string) question = ET.Element('question', attrib={'type': 'formulas'}) return question def generate_questions(num_questions): """ Generiert eine Liste von Aufgaben. Args: num_questions (int): Die Anzahl der zu generierenden Fragen. Returns: list: Eine Liste von Frage-Elementen. """ questions = [] for i in range(num_questions): # Hier können Sie die Funktionsdefinition für die Erzeugung einer Aufgabe hinzufügen questions.append(create_formula_question(i)) return questions def create_quiz(questions): """ Erstellt das gesamte Quiz-Element. Args: questions (list): Eine Liste von Frage-Elementen. Returns: Element: Das gesamte Quiz -Element. """ 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 eine Datei. Args: xml_element (Element): Das XML-Element, zu speichern. filename (str): Der Name der Zieldatei. Returns: None """ tree = ET.ElementTree(xml_element) with open(filename, 'wb') as f: tree.write(f, encoding='utf_8', xml_declaration=True) return def replace_vars2(xml_file): # XML-Datei lesen with open(xml_file, 'r') as f: text = f.read() text = text.replace("&","&") text = text.replace("<","<") text = text.replace(">",">") with open(xml_file, 'w') as f: f.write(text) # Hauptprogramm if __name__ == "__main__": questions = generate_questions(NUM_QUESTIONS) quiz = create_quiz(questions) save_to_file(quiz, FILENAME) replace_vars2(FILENAME) print("Moodle-XMLDatei erfolgreich erstellt.")