Ë
    ]Pîi.  ã                  ó   — d Z ddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
 dddd	d
dddœdddœgdœdddd	ddddœdddœgdœddddddddœdddœgdœdddddd ddœd!ddœgdœd"d#d$d%d&d'ddœd(ddœgdœgZ ed)„ eD «       «      Z G d*„ d+«      Z e«       Zy,)-uw   
services/coding_service.py â€” CodingService
Handles: run code (AI compiler), submit code, evaluate, persist results.
é    )Úannotations)Úsettings)Újson_db)Úcompile_codeÚevaluate_codeé   ÚFizzBuzzÚeasyé
   zÃWrite a function fizzBuzz(n) that prints numbers from 1 to n. For multiples of 3 print 'Fizz'; for multiples of 5 print 'Buzz'; for multiples of both print 'FizzBuzz'. Otherwise print the number.zInput: n = 15Úinput)ÚlabelÚtypez$Output: 1,2,Fizz,4,Buzz,...,FizzBuzzÚoutput)ÚidÚtitleÚ
difficultyÚpointsÚ	statementÚhintsé   zPalindrome Checkz�Write a function isPalindrome(s) that returns true if the string s is a palindrome ignoring case and non-alphanumeric characters.z$Input: 'A man a plan a canal Panama'zOutput: trueé   zFibonacci SequenceÚmediumé   zmWrite a function fibonacci(n) that returns an array containing the first n numbers in the Fibonacci sequence.zInput: n = 8zOutput: [0,1,1,2,3,5,8,13]é   zFind DuplicateszrWrite a function findDuplicates(arr) that returns all elements appearing more than once, each duplicate only once.zInput: [1,2,3,2,4,3,5]zOutput: [2,3]é   zBinary SearchÚhardé(   z˜Implement binarySearch(arr, target) that searches a sorted array. Return the index or -1 if not found. Must run in O(log n). No built-in search methods.z#Input: arr=[1,3,5,7,9,11], target=7zOutput: 3 (index)c              #  ó&   K  — | ]	  }|d    –— Œ y­w)r   N© )Ú.0Úqs     ú`/Users/priyanka/Documents/Gols_Ai_Dev/public_hub_candidate/people_hub/services/coding_service.pyÚ	<genexpr>r#   G   s   è ø€ Ð/¡Y ��(•¡Yùs   ‚c                  ó¾   — e Zd ZdZed
d„«       Zedd„«       Zedd„«       Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       Ze	 	 	 	 	 	 	 	 	 	 dd„«       Z	edd„«       Z
edd„«       Zy	)ÚCodingServicez2All coding assessment logic. Injected into routes.c                 ó   — t         S )N)Ú	QUESTIONSr   ó    r"   Úget_questionszCodingService.get_questionsO   s   € äÐr(   c                ó6   ‡ — t        ˆ fd„t        D «       d «      S )Nc              3  ó4   •K  — | ]  }|d    ‰k(  sŒ|–— Œ y­w)r   Nr   )r    r!   Úquestion_ids     €r"   r#   z-CodingService.get_question.<locals>.<genexpr>U   s   øè ø€ ÐD¡	˜1¨Q¨t©W¸Ó-C”Q¡	ùs   ƒ‘)Únextr'   )r,   s   `r"   Úget_questionzCodingService.get_questionS   s   ø€ äÓD¥	ÓDÀdÓKÐKr(   c                ó^   — t         j                  |«      }|r|d   nd|› �}t        | ||«      S )z~
        Send code to AI compiler. Returns compiler-style output.
        Does NOT reveal solutions or provide fixes.
        r   ÚQ)r%   r.   r   )ÚlanguageÚcoder,   r!   r   s        r"   Úrun_codezCodingService.run_codeY   s7   € ô ×&Ñ& {Ó3ˆÙ��'’
 q¨¨Ð%6ˆÜ˜H d¨EÓ2Ð2r(   c                ó  — t         j                  |«      }|r|d   nd}t        ||||«      }t        j                  t
        j                  | ||||||d   |d   |d   |d   |d   dd	|d
   |d   dœ«      }	|	d   |dœ|¥d|	d   i¥S )zS
        Submit code: evaluate with AI, persist to coding_assessment.json.
        r   r   Ú	ai_resultÚai_feedbackÚai_scoreÚai_marksÚ	max_marksNFÚ	strengthsÚimprovements)Úcandidate_idÚ
access_keyr,   Úquestion_textr1   r2   r5   r6   r7   r8   r9   Úfinal_marksÚreviewedr:   r;   r   )Úsubmission_idr,   Úsubmitted_atÚ
created_at)r%   r.   r   ÚdbÚinsertr   ÚCODING_RESULTS_FILE)
r<   r=   r,   r>   r1   r2   r!   r9   Úeval_resultÚrecords
             r"   Úsubmit_codezCodingService.submit_codee   sÑ   € ô ×&Ñ& {Ó3ˆÙ#$�A�h’K¨"ˆ	ô $ H¨d°MÀ9ÓMˆô —‘œ8×7Ñ7Ø*Ø(Ø)Ø+Ø&Ø"Ø)¨+Ñ6Ø)¨-Ñ8Ø)¨*Ñ5Ø)¨*Ñ5Ø)¨+Ñ6Ø"Ø#Ø)¨+Ñ6Ø)¨.Ñ9ñ:
ó ˆð& $ D™\Ø(ñ
ð ð
ð ˜V LÑ1ñ	
ð 	
r(   c           
     ó¼  — |D �ch c]  }|j                  d«      ’Œ }}d}|D ]Q  }|j                  d«      }t        j                  t        j                  | |¬«      }|sŒ=||j                  dd«      z  }ŒS dt        j
                  «       dd › dt        t        t        | «      «      «      dd › �}	|	t        |«      |t        t        j
                  «       d	œS c c}w )
z7
        Record the final submission metadata.
        r,   r   )r<   r,   r8   zPH-CODE-Nr   Ú-)Ú	referenceÚtotal_submittedÚestimated_scoreÚ	max_scorerB   )ÚgetrD   Úfind_oner   rF   Únow_isoÚstrÚabsÚhashÚlenÚ	MAX_SCORE)
r<   r=   ÚsubmissionsÚtime_used_secondsÚsÚsubmitted_idsÚtotal_ai_marksÚqidÚprevÚrefs
             r"   Úfinal_submitzCodingService.final_submit“   sÛ   € ñ 8CÓC±{°!˜Ÿ™˜}Õ-°{ˆÐCØˆãˆAØ—5‘5˜Ó'ˆCÜ—;‘;Ü×,Ñ,Ø)ØôˆDò
 Ø $§(¡(¨:°qÓ"9Ñ9‘ð ð œŸ™› b qÐ)Ð*¨!¬C´´D¸Ó4FÓ0GÓ,HÈÈ!Ð,LÐ+MÐNˆð "%Ü!$ [Ó!1Ø!/Ü!*Ü!#§¡£ñ
ð 	
ùò Ds   …Cc                óL   — t        j                  t        j                  | ¬«      S )N©r<   )rD   Ú	find_manyr   rF   rb   s    r"   Úget_candidate_submissionsz'CodingService.get_candidate_submissions¶   s   € ä�|‰|œH×8Ñ8À|ÔTÐTr(   c                óR   — t        j                  t        j                  | |ddœ«      S )NT)r?   r@   )rD   Úupdater   rF   )rA   r?   s     r"   Úset_final_markszCodingService.set_final_marks¼   s)   € ä�y‰yœ×5Ñ5°}Ø&ØñG
ó ð 	r(   N)Úreturnú
list[dict])r,   Úintrh   údict | None)r1   rS   r2   rS   r,   rj   rh   Údict)r<   rS   r=   rS   r,   rj   r>   rS   r1   rS   r2   rS   rh   rl   )
r<   rS   r=   rS   rX   ri   rY   rj   rh   rl   )r<   rS   rh   ri   )rA   rS   r?   rj   rh   rk   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr)   r.   r3   rI   r`   rd   rg   r   r(   r"   r%   r%   J   s  „ Ù<ð òó ðð òLó ðLð
 ò3ó ð3ð ð)
Øð)
àð)
ð ð)
ð ð	)
ð
 ð)
ð ð)
ð 
ò)
ó ð)
ðZ ð
Øð
àð
ð  ð
ð ð	
ð
 
ò
ó ð
ðD òUó ðUð
 òó ñr(   r%   N)rp   Ú
__future__r   Úconfigr   Úutilsr   rD   Úservices.openai_servicer   r   r'   ÚsumrW   r%   Úcoding_servicer   r(   r"   Ú<module>rx      s,  ðñõ #Ý Ý ß ?ð
 ˜*°FÀbðRð
 &¨wÑ7Ø<ÀhÑOð
ñð Ð,¸FÈbðMð =ÀgÑNØ$¨hÑ7ð
ñ
ð Ð.¸hÐRTð=ð %¨gÑ6Ø2¸HÑEð
ñ
ð Ð+¸8ÈrðBð /¸Ñ@Ø%¨xÑ8ð
ñ
ð ˜/¸È2ð*ð
 <ÀWÑMØ)°8Ñ<ð
ñð]:€	ñx Ñ/¡YÓ/Ó/€	÷wñ wñt “�r(   