Advertisement
nirajs

rnak

Feb 29th, 2024
1,046
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. """
  2. votes = ["ABC","ACB","ABC","ACB","ACB"]
  3.  
  4. 1st Post ->
  5. A -> 5
  6. B -> 2
  7. C -> 3
  8. D -> 0
  9.  
  10. A -> []
  11. B -> []
  12.  
  13.  
  14. """
  15.  
  16. from collections import defaultdict
  17. class Solution:
  18.     def rankTeams(self, votes: list[str]) -> str:
  19.         ranking = defaultdict(lambda: [0 for x in range(len(votes[0]))])
  20.         for vote in votes:
  21.             for index, c in enumerate(vote):
  22.                 ranking[c][index] -= 1
  23.         sorted_teams = sorted(ranking.items(), key=lambda item: (item[1], item[0]))
  24.         print(sorted_teams)
  25.         return "".join(x for x,_ in sorted_teams)
  26.  
  27.     def rankTeams1(self, votes: list[str]) -> str:
  28.         ranking = defaultdict(lambda: [0 for _ in range(len(votes[0]))])
  29.  
  30.         for vote in votes:
  31.             for index, c in enumerate(vote):
  32.                     ranking[c][index] += 1
  33.         sorted_teams = sorted(ranking.keys())
  34.         print(sorted_teams)
  35.         return "".join(sorted(sorted_teams, key=lambda x: ranking[x], reverse=True))
  36.  
  37.     # def testing(self):
  38.     #     c = "c"
  39.     #     c
  40.  
  41.  
  42. sol = Solution()
  43. votes1 = ["ABC","ACB","ABC","ACB","ACB"]
  44. votes2 = ["BCA","CAB","CBA","ABC","ACB","BAC"]
  45. print(sol.rankTeams1(votes2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement