DECROMAX

Vote counter challenge

Nov 9th, 2021
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.05 KB | None | 0 0
  1. votes = (
  2.         'k', 'v', 'd', 'g', 'X', 'k', 'x', 'J', 'g', 'j', 'k', 'd',
  3.         'V', 'k', 'v', 'k', 'd', 'k', 'g', 'K', 'v', 'x', 'k', 'v',
  4.         'g', 'k', 'x', 'v', 'k', 'x'
  5.         )
  6.  
  7. # converts votes values to upper case to enable count
  8. convert_case = [i.upper() for i in votes]
  9.  
  10.  
  11. def vote_count(value, tup):
  12.     """counts number of occurrences within tuple
  13.    param: value to count, tuple
  14.    returns: vote count (enum)"""
  15.     enum = 0
  16.     for i in tup:
  17.         if i == value:
  18.             enum = enum + 1
  19.     return enum
  20.  
  21.  
  22. # list unique values (entrants) within 'votes'
  23. unique = [i for i in set([x.upper() for x in votes])]
  24.  
  25. hold_dict = {}
  26.  
  27. # creates holder dict: key = entrant, value = vote count
  28. for i in unique:
  29.     count = vote_count(i, convert_case)
  30.     hold_dict[i] = count
  31.  
  32. for num, (k, v) in enumerate(sorted(hold_dict.items(), key=lambda x: x[1], reverse=True), start=1):
  33.     if k != 'V':
  34.         print(f'{num}. {k} - {v} votes ({100 * v / len(votes):.2f}%) ')
  35.     else:
  36.         print(f'{num}. {k} - {v} votes (146.00%)')
Advertisement
Add Comment
Please, Sign In to add comment