Just preserve a
and b
to take the rigth choice:
candidates_and_fp_votes = {'a': 25, 'c': 25, 'b': 0, 'e': 23, 'd': 23}
print(candidates_and_fp_votes)
final_vals = [[a,b] for a, b in candidates_and_fp_votes.items() if
list(candidates_and_fp_votes.values()).count(b) > 1]
print("Duplicates are: ")
for i in final_vals:
print(i)
fAndS=sorted(candidates_and_fp_votes.values())[len(candidates_and_fp_votes)-2:len(candidates_and_fp_votes)]
maxVals=[[a,b] for a, b in candidates_and_fp_votes.items() if b==max(candidates_and_fp_votes.values())]
if final_vals:
print("There is a duplicate with")
print(final_vals)
if len(maxVals)==2:
print("2 highest scoring candidates are equal")
print(maxVals)
print("No tiebreaker sys required")
else:
final_vals = [ a for a,b in final_vals if b in fAndS]
if not final_vals:
print("It is not a duplicate with the second and highest values")
print(fAndS)
print("No tiebreaker sys required")
else:
print("There is a duplicate with the second and highest values")
print(fAndS)
print("Tie-breaker required")
Output 1:
{'a': 25, 'c': 17, 'b': 0, 'e': 0, 'd': 23}
Duplicates are:
['b', 0]
['e', 0]
There is a duplicate with
[['b', 0], ['e', 0]]
It is not a duplicate with the second and highest values
[23, 25]
No tiebreaker sys required
Output 2:
{'a': 25, 'c': 23, 'b': 0, 'e': 0, 'd': 23}
Duplicates are:
['c', 23]
['b', 0]
['e', 0]
['d', 23]
There is a duplicate with
[['c', 23], ['b', 0], ['e', 0], ['d', 23]]
There is a duplicate with the second and highest values
[23, 25]
Tie-breaker required
Output 3:
{'a': 25, 'c': 25, 'b': 0, 'e': 23, 'd': 23}
Duplicates are:
['a', 25]
['c', 25]
['e', 23]
['d', 23]
There is a duplicate with
[['a', 25], ['c', 25], ['e', 23], ['d', 23]]
2 highest scoring candidates are equal
[['a', 25], ['c', 25]]
No tiebreaker sys required
[EDIT]
Here is a more simple answer, just thinking all conditions together:
(Based only the dict sorted)
candidates_and_fp_votes = {'a': 25, 'c': 25, 'b': 0, 'e': 0, 'd': 23}
sorted_votes = sorted([[value,key] for key,value in candidates_and_fp_votes.items()])
resultList=[]
dictSize = len(candidates_and_fp_votes)
print sorted_votes
if(dictSize>=3):
if sorted_votes[dictSize-2][0]==sorted_votes[dictSize-3][0]:
print("Need tie break between")
resultList = [ key for value,key in sorted_votes if value==sorted_votes[dictSize-2][0] ]
print resultList
else:
print("Winers")
resultList = [key for value,key in sorted_votes[dictSize-2:dictSize]]
print resultList
Output:
[[0, 'b'], [0, 'e'], [23, 'd'], [25, 'a'], [25, 'c']]
Winers
['a', 'c']