php version fonctionnelle ( alignée sur python )

+ json bien formé
This commit is contained in:
philippe lhardy
2025-06-23 10:17:21 +02:00
parent a637d58b49
commit 502ea0aeaf
2 changed files with 105 additions and 21 deletions

86
samples/3.json Normal file
View File

@@ -0,0 +1,86 @@
{
"candidats":{
"A":"albert",
"B":"Beatrice",
"C":"Chloé",
"D":"Deborah",
"E":"Eric",
"F":"Francçois",
"G":"Gisèle",
"H":"Hugo"
},
"votants":{
"decompte":11
},
"mentions":[
"Très Bien",
"Bien",
"Assez Bien",
"Passable",
"Insuffisant",
"A rejeter"
],
"votation":
{
"date":"14 juin 2025",
"lieu":"liffré",
"votes":[
{
"A":1,
"C":2,
"D":3
},
{
"B":1,
"C":2,
"E":3
},
{
"A":1,
"C":2,
"F":3
},
{
"G":1,
"C":2,
"D":3
},
{
"A":1,
"G":2,
"E":3
},
{
"E":1,
"C":2,
"D":3
},
{
"E":1,
"C":2,
"D":3
},
{
"A":1,
"D":2
},
{
"A":5,
"C":2,
"D":3
},
{
"A":4
},
{
"A":1,
"C":2,
"D":3,
"B":4,
"E":3,
"F":4,
"G":0
}
]
}
}

View File

@@ -33,20 +33,22 @@ $default_mention=$mentions-1;
$votes=$poll["votation"]["votes"]; $votes=$poll["votation"]["votes"];
if ($verbose) { if ($verbose) {
print('candidats:' . implode(",",$candidats)); print("candidats\n");
print(implode(',',$votes)); var_dump($candidats);
print("votes:\n");
var_dump($votes);
} }
$warnings=[]; $warnings=[];
$default_vote=[]; $default_vote=[];
foreach ($candidats as $candidat ) { foreach ($candidats as $candidat => $nom_candidat ) {
$default_vote[$candidat]=$default_mention; $default_vote[$candidat]=$default_mention;
} }
$collect=[]; $collect=[];
foreach ($votes as $vote) { foreach ($votes as $vote) {
# missing candidat in vote is the worts one # missing candidat in vote is the worts one
foreach ($candidats as $candidat) { foreach ($candidats as $candidat => $nom_candidat ) {
if (! array_key_exists($candidat,$vote)) { if (! array_key_exists($candidat,$vote)) {
$vote[$candidat]=$default_mention; $vote[$candidat]=$default_mention;
} }
@@ -72,15 +74,16 @@ foreach ($votes as $vote) {
} }
if ($verbose) { if ($verbose) {
print($collect); print("collect:\n");
print(json_encode($collect));
} }
$votant_median_check= ($votants % 2 == 0) ? $votants / 2 : (($votants-1) / 2); $votant_median_check= ($votants % 2 == 0) ? intdiv($votants,2) : intdiv(($votants-1),2) + 1;
$votant_median=$votants / 2 + $votants % 2; $votant_median=intdiv($votants, 2) + $votants % 2;
if ( $votant_median_check != $votant_median ) { if ( $votant_median_check != $votant_median ) {
print('[ERROR] le nombre median de votants semble erroné. contactez le developpeur de ce code.' + str($votant_median_check)); print('[ERROR] le nombre median de votants (' . $votant_median_check . '/' . $votant_median . ') semble erroné. contactez le developpeur de ce code.');
} }
$merite=[]; $merite=[];
@@ -92,7 +95,7 @@ $merite_pourcent=[];
# cumul du pire au meilleur # cumul du pire au meilleur
$range_mentions = $mentions; $range_mentions = $mentions;
foreach ($candidats as $candidat) { foreach ($candidats as $candidat => $nom_candidat) {
$vote=$collect[$candidat]; $vote=$collect[$candidat];
$cumul=0; $cumul=0;
for ($i=0; $i < $range_mentions; $i ++) { for ($i=0; $i < $range_mentions; $i ++) {
@@ -100,7 +103,7 @@ foreach ($candidats as $candidat) {
$pourcent[$i]=0; $pourcent[$i]=0;
} }
$mention_m=null; $mention_m=null;
for ($i=$range_mentions -1; $i > 0; $i --) { for ($mention=$range_mentions -1; $mention >=0; $mention --) {
if ( array_key_exists($mention,$vote) ) { if ( array_key_exists($mention,$vote) ) {
$cumul=$cumul+$vote[$mention]; $cumul=$cumul+$vote[$mention];
$pourcent[$mention]= ( 100 * $vote[$mention] ) / $votants; $pourcent[$mention]= ( 100 * $vote[$mention] ) / $votants;
@@ -108,11 +111,10 @@ foreach ($candidats as $candidat) {
if ($cumul >= $votant_median and $mention_m == null ) { if ($cumul >= $votant_median and $mention_m == null ) {
$mention_m=$mention; $mention_m=$mention;
} }
$m[$mention]=$cumul; $m[$mention]=$cumul;
} }
if ($cumul < $votants) { if ($cumul < $votants) {
print('[ERROR] le cumul des votes doit correspondre au nombre de votants. contactez le developpeur de ce code.'); print("[ERROR] le cumul des votes " . $cumul . " doit correspondre au nombre de votants. contactez le developpeur de ce code.");
} }
$mention_mediane[$candidat]=$mention_m; $mention_mediane[$candidat]=$mention_m;
$merite[$candidat]=$m; $merite[$candidat]=$m;
@@ -120,20 +122,16 @@ foreach ($candidats as $candidat) {
} }
if ($verbose) { if ($verbose) {
print($merite) ; print("\nmerite:\n");
print($mention_mediane); print(json_encode($merite));
print("\nmention_mediane:\n");
print(json_encode($mention_mediane));
} }
# les gagnants sont ceux qui ont la mention mediane minimale ( la meilleure ) # les gagnants sont ceux qui ont la mention mediane minimale ( la meilleure )
$found=[]; $found=[];
print(json_encode($mention_mediane)); for ($mention = 0; $mention < $mentions; $mention ++) {
# la clé de candidats est un shortcut
print(json_encode($candidats));
for ($i = 0; $i < $mentions; $i ++) {
foreach ($mention_mediane as $candidat => $merite_median) foreach ($mention_mediane as $candidat => $merite_median)
if ($merite_median == $mention) { if ($merite_median == $mention) {
$item=[]; $item=[];