Guest User

Untitled

a guest
May 20th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. candidatenames = {
  4. "1": "Energilogger för USB",
  5. "2": "Lödtimer",
  6. "3": "Logikanalysator",
  7. "4": "uScope V1.1",
  8. "5": "TV dosa v1.1",
  9. "6": "Gitarrackordsgenerator",
  10. }
  11.  
  12. def compare_cand(cand1, cand2, votes):
  13. score = [0, 0]
  14. for vote in votes:
  15. if cand1 in vote:
  16. if not cand2 in vote:
  17. score[0] = score[0] + 1
  18. elif vote.index(cand1) < vote.index(cand2):
  19. score[0] = score[0] + 1
  20. else:
  21. score[1] = score[1] + 1
  22. elif cand2 in vote:
  23. score[1] = score[1] + 1
  24. return score
  25.  
  26.  
  27. def lesser_any(cand, totest, paths):
  28. for testcand in totest:
  29. if testcand == cand:
  30. continue
  31. if paths[cand][testcand] < paths[testcand][cand]:
  32. return True
  33. return False
  34.  
  35. f = open("votes.txt")
  36.  
  37. i=0
  38. votes = []
  39. candidates = set()
  40.  
  41. for line in f:
  42. these = line.split()[1:]
  43. votes.append(these)
  44. candidates.update(these)
  45.  
  46. #####
  47. stopat = len(candidates)
  48.  
  49.  
  50. candidates = list(candidates)[0:stopat]
  51.  
  52. comparisons = [[0 for col in range(len(candidates))] for row in range(len(candidates))]
  53.  
  54. for cand1 in range(len(candidates)):
  55. for cand2 in range(len(candidates)):
  56. if cand1 == cand2:
  57. continue
  58. score = compare_cand(candidates[cand1], candidates[cand2], votes)
  59.  
  60. comparisons[cand1][cand2] = score[0]
  61. comparisons[cand2][cand1] = score[1]
  62.  
  63. print comparisons
  64.  
  65. paths = [[0 for col in range(len(candidates))] for row in range(len(candidates))]
  66.  
  67. for cand1 in range(len(candidates)):
  68. for cand2 in range(len(candidates)):
  69. if cand1 != cand2:
  70. if comparisons[cand1][cand2] > comparisons[cand2][cand1]:
  71. paths[cand1][cand2] = comparisons[cand1][cand2]
  72. else:
  73. paths[cand1][cand2] = 0
  74.  
  75. for cand1 in range(len(candidates)):
  76. for cand2 in range(len(candidates)):
  77. if cand1 != cand2:
  78. for cand3 in range(len(candidates)):
  79. if cand1 != cand3:
  80. if cand2 != cand3:
  81. paths[cand2][cand3] = max ( paths[cand2][cand3], min ( paths[cand2][cand1], paths[cand1][cand3] ) )
  82.  
  83. print paths
  84.  
  85. rest = set(range(stopat))
  86.  
  87. plats = 1
  88.  
  89. while len(rest) > 0:
  90. winners = list(rest)
  91. for cand in rest:
  92. if lesser_any(cand, winners, paths):
  93. winners.remove(cand)
  94. print "%d: " % (plats),
  95. for cand in winners:
  96. print "%s (%s), " % (candidatenames[candidates[cand]], candidates[cand]),
  97. rest.remove(cand)
  98. print
  99. plats = plats + 1
Add Comment
Please, Sign In to add comment