Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- candidatenames = {
- "1": "Energilogger för USB",
- "2": "Lödtimer",
- "3": "Logikanalysator",
- "4": "uScope V1.1",
- "5": "TV dosa v1.1",
- "6": "Gitarrackordsgenerator",
- }
- def compare_cand(cand1, cand2, votes):
- score = [0, 0]
- for vote in votes:
- if cand1 in vote:
- if not cand2 in vote:
- score[0] = score[0] + 1
- elif vote.index(cand1) < vote.index(cand2):
- score[0] = score[0] + 1
- else:
- score[1] = score[1] + 1
- elif cand2 in vote:
- score[1] = score[1] + 1
- return score
- def lesser_any(cand, totest, paths):
- for testcand in totest:
- if testcand == cand:
- continue
- if paths[cand][testcand] < paths[testcand][cand]:
- return True
- return False
- f = open("votes.txt")
- i=0
- votes = []
- candidates = set()
- for line in f:
- these = line.split()[1:]
- votes.append(these)
- candidates.update(these)
- #####
- stopat = len(candidates)
- candidates = list(candidates)[0:stopat]
- comparisons = [[0 for col in range(len(candidates))] for row in range(len(candidates))]
- for cand1 in range(len(candidates)):
- for cand2 in range(len(candidates)):
- if cand1 == cand2:
- continue
- score = compare_cand(candidates[cand1], candidates[cand2], votes)
- comparisons[cand1][cand2] = score[0]
- comparisons[cand2][cand1] = score[1]
- print comparisons
- paths = [[0 for col in range(len(candidates))] for row in range(len(candidates))]
- for cand1 in range(len(candidates)):
- for cand2 in range(len(candidates)):
- if cand1 != cand2:
- if comparisons[cand1][cand2] > comparisons[cand2][cand1]:
- paths[cand1][cand2] = comparisons[cand1][cand2]
- else:
- paths[cand1][cand2] = 0
- for cand1 in range(len(candidates)):
- for cand2 in range(len(candidates)):
- if cand1 != cand2:
- for cand3 in range(len(candidates)):
- if cand1 != cand3:
- if cand2 != cand3:
- paths[cand2][cand3] = max ( paths[cand2][cand3], min ( paths[cand2][cand1], paths[cand1][cand3] ) )
- print paths
- rest = set(range(stopat))
- plats = 1
- while len(rest) > 0:
- winners = list(rest)
- for cand in rest:
- if lesser_any(cand, winners, paths):
- winners.remove(cand)
- print "%d: " % (plats),
- for cand in winners:
- print "%s (%s), " % (candidatenames[candidates[cand]], candidates[cand]),
- rest.remove(cand)
- print
- plats = plats + 1
Add Comment
Please, Sign In to add comment