
    =dji"                     d    S SK r S SKJrJr  S rS rS\\   S\\   4S jrS\\   S\\   4S jrg)	    N)ListDictc                    [        U[        5      (       a  [        U5      S:  a  X4$ UR                  US5      n[	        UR                  5       5      n[        R                  " U5        [        UR                  5       5      n[        U5       VVs0 s H
  u  pgXtU   _M     nnnUn	UR                  5        H  u  pzX:X  d  M  Un	  X4$    X4$ s  snnf )zj
Shuffle options while maintaining correct answer text
Returns: shuffled_options_dict, correct_answer_key
    )
isinstancedictlengetlistvaluesrandomshufflesortedkeys	enumerateitems)question_textoptions_dictcorrect_answer_keycorrect_answer_textoption_valuesoption_keysikeyshuffled_optionsnew_correct_keyvalues              9D:\beyond school\05_solvesmart\server\src\utils\random.pyshuffle_question_optionsr       s    
 lD))S->-B// '**+=rB ,,./M NN=! **,-K<Ek<RS<R&!1--<RS )O&,,.
'!O,, /
 ,, Ts   Cc                 N   U (       d  / $ / n/ nU  GH  nUR                   nUR                  n[        U[        5      (       a  [	        U5      S:  a\  UR                  UR                  UR                  5        VVs/ s H	  u  pgXgS.PM     snnUR                  UUR                  S.5        M  UR                  US5      n[        UR                  5       5      n	[        UR                  5       5      n
 U
R                  U5      n[#        5       n[	        U5      S:  a   US   US   :X  a  UR%                  US   5        ['        [	        U
5      5       Vs/ s H  oU;  d  M
  UPM     nnU(       d  [        ['        [	        U
5      5      5      n[(        R*                  " U5      nU
R-                  5       nUU   UU   sUU'   UU'   [/        U	5       VVs0 s H  u  nnUUU   _M     nnnUnUR                  5        H  u  nnUU:X  d  M  Un  O   UR                  U5        [	        U5      S:  a  UR1                  S5        UR                  UR                  UR                  5        VVs/ s H	  u  pgXgS.PM     snnUR                  UUR                  S.5        GM     U$ s  snnf ! [          af    UR                  UR                  UR                  5        VVs/ s H	  u  pgXgS.PM     Os  snnf snnUR                  UUR                  S.5         GM  f = fs  snf s  snnf s  snnf )z
Shuffle options for multiple questions while avoiding consecutive same correct positions
Returns: list of questions with shuffled options
r   )r   r   )questionoptionslevelcorrect_answerques_idr   r   )r#   r%   r   r	   r
   appendr"   r   
difficultyidr   r   r   r   r   index
ValueErrorsetaddranger   choicecopyr   pop)questions_datashuffled_questionslast_two_positionsdatar   r   kvr   r   r   correct_indexpositions_to_avoidr   valid_positionsnew_correct_indexshuffled_valuesr   r   r   r   s                        r   shuffle_questions_options_batchr?   #   s   
 	||!00,--\1BQ1F%% MM?K?Q?Q?ST?StqA2?ST"477'   +../A2F \..01\0023	)//0CDM !U!"a'!"%);B)??"&&'9"'=> ',C,>&?_&?L^C^1&?_"5]);#<=O #MM/: (,,.-.0N 	K&8I(J CLKBXYBX3C!33BXY -*002JC++"% 3 	!!"34!"Q&""1%!!;K;Q;Q;ST;S41.;ST__-ww#
 	Q ` Q U"  		%% MM?K?Q?Q?ST?StqA2?ST"477'  		& ` Z  Us<   9J1J#	LLL%L!#3LK'&(LL	questionsreturnc                    U (       d  U $ / n/ nU  GH  nUR                  S/ 5      nUR                  SS5      n[        U5      S:  d  U(       d  UR                  U5        MQ   UR                  U5      n[        5       n[        U5      S:  a!  US   US   :X  a  UR                  US   5        O[        U5      S:X  a   [        [        U5      5       Vs/ s H  oU;  d  M
  UPM     n	nU	(       d  [        [        [        U5      5      5      n	[        R                  " U	5      n
UR                  5       nX   X   sX'   X'   UR                  5       nXS'   X   US'   UR                  U5        UR                  U
5        [        U5      S:  d  GM~  UR                  S5        GM     U$ ! [         a    UR                  U5         GM  f = fs  snf )	z
Randomize correct answer positions to avoid consecutive same options.

Args:
    questions: List of question dicts with 'options' array and 'answer' text
    
Returns:
    List of questions with shuffled options
r#   answerr   r   r'   r(      r   )r   r
   r)   r,   r-   r.   r/   r0   r   r   r1   r2   r3   )r@   randomized_questionsr6   r"   r#   r%   current_correct_indexr;   r   r<   r=   new_optionsupdated_questions                r   randomize_correct_optionsrI      s    ,,y"-!h3w<!> ''1	$+MM.$A! !U!"a'!"%);B)??"&&'9"'=>#$) ',CL&9Y&9FX=X1&9Y "5W#67O #MM/: lln *K,N 	K*K,J $==?&1#%0%C"##$45 	!!"34!"Q&""1%m p  Y  	 ''1	  Zs   "F&	G	 G	&GGc                 f   U (       d  U $ / nU  H  nUR                  S/ 5      nUR                  SS5      n[        U5      S:  d  U(       d  UR                  U5        MP  UR                  5       n[        R
                  " U5        UR                  5       nXVS'   XFS'   UR                  U5        M     U$ )a  
Simple version: Just shuffle options for each question randomly.
Ensures variety in correct answer positions across questions.

Args:
    questions: List of question dicts with 'options' array and 'answer' text
    
Returns:
    List of questions with shuffled options
r#   rC   r   r   )r   r
   r)   r2   r   r   )r@   rE   r"   r#   r%   r   rH   s          r    randomize_correct_options_simplerK      s     ,,y"-!h3w<!> ''1 #<<>'( $==?&6#%3"##$45% (      )r   typingr   r   r    r?   rI   rK    rL   r   <module>rO      sQ     ->[|H d H T
 H V$ T
 $ tDz $ rL   