diff --git a/ia04/comsoc/utils.go b/ia04/comsoc/utils.go index e416a675bf6e8a75ec93605791fc2258addea761..5b4934d2764915c3fab051f47bc5961ab56c0cbc 100644 --- a/ia04/comsoc/utils.go +++ b/ia04/comsoc/utils.go @@ -1,40 +1,42 @@ package comsoc -import ("errors") +import ( + "errors" +) type Alternative int type Profile [][]Alternative type Count map[Alternative]int // renvoie l'indice ou se trouve alt dans prefs -func rank(alt Alternative, prefs []Alternative) int{ - for i,v := range prefs { - if alt == v { - return i - } - } - return -1 +func rank(alt Alternative, prefs []Alternative) int { + for i, v := range prefs { + if alt == v { + return i + } + } + return -1 } // renvoie vrai ssi alt1 est préférée à alt2 func isPref(alt1, alt2 Alternative, prefs []Alternative) bool { - return rank(alt1, prefs) > rank(alt2, prefs) + return rank(alt1, prefs) < rank(alt2, prefs) } // renvoie les meilleures alternatives pour un décomtpe donné func maxCount(count Count) (bestAlts []Alternative) { - max := 0 - for _,v := range count { - switch { - case v == max: - bestAlts = append(bestAlts, Alternative(v)) - case v > max: - bestAlts = []Alternative{Alternative(v)} - // bestAlts = append(bestAlts, v) - max = v - } - } - return + max := 0 + for _, v := range count { + switch { + case v == max: + bestAlts = append(bestAlts, Alternative(v)) + case v > max: + bestAlts = []Alternative{Alternative(v)} + // bestAlts = append(bestAlts, v) + max = v + } + } + return } // vérifie le profil donné, par ex. qu'ils sont tous complets et que chaque alternative n'apparaît qu'une seule fois par préférences @@ -42,21 +44,21 @@ func maxCount(count Count) (bestAlts []Alternative) { // vérifier que tout le monde a les mêmes alternatives, qu'il n'est pas vide - // vérifie le profil donné, par ex. qu'ils sont tous complets et que chaque alternative de alts apparaît exactement une fois par préférences func checkProfileAlternative(prefs Profile, alts []Alternative) error { - for _,voter := range prefs { - if len(voter) != len(alts) { - return errors.New("001: ProfileErrors") - } - check := make(map[Alternative]int) - for _,alt := range voter { - check[alt] += 1 - if check[alt] > 1 { - return errors.New("001: ProfileErrors") - } - } - } - return nil + for _, voter := range prefs { + if len(voter) != len(alts) { + return errors.New("001: ProfileErrors") + } + check := make(map[Alternative]int) + for _, alt := range voter { + check[alt] += 1 + if check[alt] > 1 { + return errors.New("001: ProfileErrors") + } + } + } + return nil } + //jerome.gaigne@hds.utc.fr