php version fonctionnelle ( alignée sur python )
+ json bien formé
This commit is contained in:
@@ -33,20 +33,22 @@ $default_mention=$mentions-1;
|
||||
|
||||
$votes=$poll["votation"]["votes"];
|
||||
if ($verbose) {
|
||||
print('candidats:' . implode(",",$candidats));
|
||||
print(implode(',',$votes));
|
||||
print("candidats\n");
|
||||
var_dump($candidats);
|
||||
print("votes:\n");
|
||||
var_dump($votes);
|
||||
}
|
||||
|
||||
$warnings=[];
|
||||
$default_vote=[];
|
||||
foreach ($candidats as $candidat ) {
|
||||
foreach ($candidats as $candidat => $nom_candidat ) {
|
||||
$default_vote[$candidat]=$default_mention;
|
||||
}
|
||||
|
||||
$collect=[];
|
||||
foreach ($votes as $vote) {
|
||||
# missing candidat in vote is the worts one
|
||||
foreach ($candidats as $candidat) {
|
||||
foreach ($candidats as $candidat => $nom_candidat ) {
|
||||
if (! array_key_exists($candidat,$vote)) {
|
||||
$vote[$candidat]=$default_mention;
|
||||
}
|
||||
@@ -72,15 +74,16 @@ foreach ($votes as $vote) {
|
||||
}
|
||||
|
||||
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 ) {
|
||||
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=[];
|
||||
@@ -92,7 +95,7 @@ $merite_pourcent=[];
|
||||
# cumul du pire au meilleur
|
||||
$range_mentions = $mentions;
|
||||
|
||||
foreach ($candidats as $candidat) {
|
||||
foreach ($candidats as $candidat => $nom_candidat) {
|
||||
$vote=$collect[$candidat];
|
||||
$cumul=0;
|
||||
for ($i=0; $i < $range_mentions; $i ++) {
|
||||
@@ -100,7 +103,7 @@ foreach ($candidats as $candidat) {
|
||||
$pourcent[$i]=0;
|
||||
}
|
||||
$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) ) {
|
||||
$cumul=$cumul+$vote[$mention];
|
||||
$pourcent[$mention]= ( 100 * $vote[$mention] ) / $votants;
|
||||
@@ -108,11 +111,10 @@ foreach ($candidats as $candidat) {
|
||||
if ($cumul >= $votant_median and $mention_m == null ) {
|
||||
$mention_m=$mention;
|
||||
}
|
||||
|
||||
$m[$mention]=$cumul;
|
||||
}
|
||||
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;
|
||||
$merite[$candidat]=$m;
|
||||
@@ -120,20 +122,16 @@ foreach ($candidats as $candidat) {
|
||||
}
|
||||
|
||||
if ($verbose) {
|
||||
print($merite) ;
|
||||
print($mention_mediane);
|
||||
print("\nmerite:\n");
|
||||
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 )
|
||||
$found=[];
|
||||
|
||||
print(json_encode($mention_mediane));
|
||||
|
||||
# la clé de candidats est un shortcut
|
||||
print(json_encode($candidats));
|
||||
|
||||
|
||||
for ($i = 0; $i < $mentions; $i ++) {
|
||||
for ($mention = 0; $mention < $mentions; $mention ++) {
|
||||
foreach ($mention_mediane as $candidat => $merite_median)
|
||||
if ($merite_median == $mention) {
|
||||
$item=[];
|
||||
|
||||
Reference in New Issue
Block a user