%%AMC:preprocess_command=/home/monsieur/Projets-QCM/test-python/prePythonTex4AMC
%%AMC:jobspecific=1
%%AMC:latex_engine=pdflatex --shell-escape
%--------------------------

\documentclass[a4paper]{article}

\usepackage[utf8]{inputenc}    
\usepackage[T1]{fontenc}

\usepackage[francais,bloc,completemulti]{automultiplechoice}    
\usepackage{fancyhdr,amssymb,amsmath,graphicx}

% ---------- Utilisation de codes Python -----
\usepackage{pythontex}

%====== begin file contents ======
\begin{filecontents*}[overwrite]{pythonGraphe.py}
from networkx import *
import networkx as nx
import matplotlib.pyplot as plt
from random import sample,randint

import numpy as np
#import random
#random.seed(1235)
np.random.seed(1235)



liste_Personnes = ["Al","Be","Ch","Do","Em","Fa","Gu","He","Is","Ja","Ke","Lo","Ma","Ni","Od","Pi","Qu","Ro","Su","Ta","Ur","Ve","Wa","Xa","Yv","Zo"]
amis=sample(liste_Personnes[3:10],4)

# Fonction pour générer et tester le graphe
def generer_graphe():
  reseau_social=nx.Graph()
  for personne in amis :
      reseau_social.add_node(personne)
      for relie in sample(liste_Personnes[5:14],(randint(2,6))):
          if relie != personne:
             reseau_social.add_edge(personne,relie)
  return reseau_social

# Boucle pour relancer la génération jusqu'à obtenir un graphe connexe
while True:
  reseau_social = generer_graphe()
  if nx.is_connected(reseau_social):
    break
  else:
    print("Graphe non connexe, relance de la génération.")

couleurs=[["pink","red"],["lightblue","blue"],["lightgreen","green"],["yellow","orange"]]
#couleur=couleurs[randint(0,len(couleurs)-1)]
couleur=couleurs[1]
options = {
    "font_size": 12,
    "node_size": 1000,
    "node_color": couleur[0],
    "edgecolors": couleur[1],
    "linewidths": 2,
    "width": 2,
}

position = circular_layout(reseau_social)


# Ouvrir le fichier en mode écriture
with open('solution.txt', 'w', encoding='utf-8') as file:
    diametre = nx.diameter(reseau_social)
    rayon = nx.radius(reseau_social)
    centre = nx.center(reseau_social)

# Écrire les résultats dans le fichier
    file.write(f"Diamètre: {diametre}\n")
    file.write(f"Rayon: {rayon}\n")
    file.write(f"Centre: {centre}\n")

print("Diametre=",diameter(reseau_social))
print("Rayon=",radius(reseau_social))
print("Centre=",center(reseau_social))

draw(reseau_social,pos=position, with_labels=True,**options)
plt.savefig('Graphe-filecontents.png',bbox_inches='tight')
plt.close()  # Fermer la figure pour éviter de l'afficher dans l'environnement Python

diametre=nx.diameter(reseau_social)
rayon=nx.radius(reseau_social)
centre=nx.center(reseau_social)

\end{filecontents*}
%====== end file contents ======

\immediate\write18{chmod +x pythonGraphe.py}

\begin{document}

\onecopy{5}{%

\immediate\write18{python3 pythonGraphe.py && mv Graphe-filecontents.png Graphe-filecontents-\AMCStudentNumber.png && mv solution.txt solution-\AMCStudentNumber.txt}

\includegraphics[width=10cm]{Graphe-filecontents-\AMCStudentNumber.png}

\begin{questionmultx}{rayon}
Quel est le rayon du graphe ? 

\pys{%
\AMCnumericChoices{!{rayon}}{digits=1, decimals=0, sign=false, scoreexact=1, scorewrong=0, vspace=0em, hspace=2em, borderwidth=0pt, nozero}
}
\end{questionmultx}


}
\end{document}
