diff --git a/app/main.py b/app/main.py index ddf47cb..6d3a66f 100644 --- a/app/main.py +++ b/app/main.py @@ -70,6 +70,7 @@ class ExerciseRequest(BaseModel): max_table: int num_exercises: int = 15 multiplication_only: bool = False + max_first_operand: int = 12 # New parameter for the maximum value of the first operand class OperationExerciseRequest(BaseModel): @@ -81,6 +82,7 @@ def generate_exercises( max_table: int, num_exercises: int = 15, multiplication_only: bool = False, + max_first_operand: int = 12, # New parameter for the maximum value of the first operand ) -> List[str]: """Génère des exercices de multiplication et division aléatoires mélangés sans doublons""" exercises: List[str] = [] @@ -93,27 +95,28 @@ def generate_exercises( while len(exercises) < num_exercises and attempts < max_attempts: attempts += 1 - # Choisir deux nombres aléatoires entre min_table et max_table - a = random.randint(min_table, max_table) - b = random.randint(min_table, max_table) - result = a * b - - # Déterminer le type d'exercice - if multiplication_only: - exercise_type = "multiplication" - else: - # Choisir aléatoirement le type d'exercice (seulement multiplication ou division) - exercise_type = random.choice(["multiplication", "division"]) - - if exercise_type == "multiplication": + # Pour les multiplications, un des opérandes est entre 1 et max_first_operand + # et l'autre est entre min_table et max_table + if multiplication_only or random.choice([True, False]): # Exercice de multiplication - operation_key = f"mult_{a}_{b}" # Clé unique pour cette opération + a = random.randint(1, max_first_operand) # Premier opérande entre 1 et max_first_operand + b = random.randint(min_table, max_table) # Second opérande entre min_table et max_table + + # Randomize the order of operands + if random.choice([True, False]): + a, b = b, a + + result = a * b + operation_key = f"mult_{min(a,b)}_{max(a,b)}" # Clé unique pour cette opération (order-independent) if operation_key not in used_operations: exercise = f"{a} · {b} = ____" exercises.append(exercise) used_operations.add(operation_key) elif not multiplication_only: # division # Exercice de division + a = random.randint(min_table, max_table) + b = random.randint(min_table, max_table) + result = a * b divisor = random.choice([a, b]) operation_key = f"div_{result}_{divisor}" # Clé unique pour cette opération if operation_key not in used_operations: @@ -127,24 +130,32 @@ def generate_exercises( remaining = num_exercises - len(exercises) for _ in range(remaining): # Générer des variations avec des nombres légèrement différents - a = random.randint(min_table, max_table) - b = random.randint(min_table, max_table) - result = a * b - - # Déterminer le type d'exercice - if multiplication_only: - exercise_type = "multiplication" - else: - # Choisir aléatoirement le type d'exercice - exercise_type = random.choice(["multiplication", "division"]) - - if exercise_type == "multiplication": + # Pour les multiplications, un des opérandes est entre 1 et max_first_operand + # et l'autre est entre min_table et max_table + if multiplication_only or random.choice([True, False]): + # Exercice de multiplication + a = random.randint(1, max_first_operand) # Premier opérande entre 1 et max_first_operand + b = random.randint(min_table, max_table) # Second opérande entre min_table et max_table + + # Randomize the order of operands + if random.choice([True, False]): + a, b = b, a + + result = a * b exercise = f"{a} · {b} = ____" elif not multiplication_only: # division + a = random.randint(min_table, max_table) + b = random.randint(min_table, max_table) + result = a * b divisor = random.choice([a, b]) exercise = f"{result} : {divisor} = ____" else: # Fallback to multiplication if multiplication_only is True + a = random.randint(1, max_first_operand) + b = random.randint(min_table, max_table) + # Randomize the order of operands + if random.choice([True, False]): + a, b = b, a exercise = f"{a} · {b} = ____" exercises.append(exercise) @@ -157,6 +168,7 @@ def create_math_exercises_pdf( max_table: int, num_exercises: int = 15, multiplication_only: bool = False, + max_first_operand: int = 12, # New parameter for the maximum value of the first operand ) -> str: """Crée un fichier PDF avec des exercices de mathématiques mélangés en 3 colonnes et l'upload sur S3""" import datetime @@ -193,7 +205,7 @@ def create_math_exercises_pdf( # Générer les exercices exercises = generate_exercises( - min_table, max_table, num_exercises, multiplication_only + min_table, max_table, num_exercises, multiplication_only, max_first_operand ) # Pas d'en-têtes de colonnes @@ -290,6 +302,7 @@ async def generate_exercises_endpoint(request: ExerciseRequest): request.max_table, request.num_exercises, request.multiplication_only, + request.max_first_operand, # Pass the new parameter ) # Return redirect to automatically download the file diff --git a/app/templates/index.html b/app/templates/index.html index 039222d..000bdf2 100644 --- a/app/templates/index.html +++ b/app/templates/index.html @@ -66,6 +66,16 @@ +
+ + +
La valeur maximale pour le premier opérande dans les multiplications
+
+