From 9f7f55f9aad5bf6e67e969584f8f7253c78edd59 Mon Sep 17 00:00:00 2001 From: Mohamed Date: Tue, 9 Jan 2024 09:24:00 +0100 Subject: [PATCH] Only shuffle incorrect answer --- src/domain/exercise/generate_one_exercise.rs | 27 ++++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/domain/exercise/generate_one_exercise.rs b/src/domain/exercise/generate_one_exercise.rs index e24faae..c45b1fb 100644 --- a/src/domain/exercise/generate_one_exercise.rs +++ b/src/domain/exercise/generate_one_exercise.rs @@ -1,5 +1,4 @@ use rand::seq::SliceRandom; - use crate::domain::exercise::extract_and_shuffle_options::extract_and_shuffle_options; use crate::domain::exercise::select_random_verse_index::select_random_verse_index; use crate::domain::similar::sourate_from_verse::sourate_name_from_verse; @@ -15,7 +14,12 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer let exclude_verse = Some(exercise.verses[valid_verse_index].verse.clone()); let extracted_values = extract_and_shuffle_options(&mut exercise.verses, exercise_type, &exclude_verse); - let mut alternatives: Vec = extracted_values.into_iter().map(|value| { + + // Extract and keep the correct answer separate + let correct_alternative = Alternative { verse: Some(exercise.verses[valid_verse_index].verse.clone()) }; + + // Prepare incorrect alternatives and shuffle them + let mut incorrect_alternatives: Vec = extracted_values.into_iter().map(|value| { match exercise_type { ExerciseType::FindDiscriminant => { Alternative { @@ -43,19 +47,26 @@ pub fn generate_one_exercise(dbs: &Database, exercise: &mut ExerciseOutput, exer }, _ => unimplemented!(), } - }).take(2).collect(); + }).filter(|alt| alt.verse != Some(exercise.verses[valid_verse_index].verse.clone())) + .collect(); + + incorrect_alternatives.shuffle(&mut rand::thread_rng()); + + // Combine correct and incorrect alternatives, correct answer at the end + incorrect_alternatives.push(correct_alternative); + + // Truncate the alternatives to the desired length (e.g., 4) + incorrect_alternatives.truncate(4); - // Add the correct answer - let valid_verse = exercise.verses.get(valid_verse_index).unwrap(); - alternatives.push(Alternative { verse: Some(valid_verse.verse.clone()) }); - alternatives.shuffle(&mut rand::thread_rng()); + // Use the combined and truncated list as the final alternatives + let alternatives = incorrect_alternatives; // Return None if there are not enough alternatives if alternatives.len() <= 1 { None } else { let mut generated_exercise = Exercise { - statement: valid_verse.clone(), + statement: exercise.verses[valid_verse_index].clone(), alternatives, exercise_type: exercise_type.clone(), };