- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find the final ranking of teams in order from highest to lowest rank in python

Suppose we have a list of strings called votes, here each entry is in lowercase letters and they are representing votes on candidates in order from highest to lowest preference. Here the rank of a candidate depends first by the number of votes received on the highest preference. Now if there are ties, we shall check the number of votes received on next highest preference, and so on. If there are still ties, then they will be ranked alphabetically. So we have to find the final ranking of the teams in order from highest to lowest ranked.

So, if the input is like votes = ["zyx", "zxy", "xyz"], then the output will be "zxy", as z received the most number of highest preference, so it is ranked first. Then x received the second most number of highest preference, and y didn't receive any highest preference votes.

To solve this, we will follow these steps −

- count := length of strings in votes
- cand := an empty map where each keys will be a list of size count, and initially they are filled with 0
- for each v in votes, do
- for each index i and value c in v, do
- increase cand[c, i] by 1

- sort cand items in descending order based on value, when values are same, sort them alphabetically
- return a string by concatenating the sorted elements.

- for each index i and value c in v, do

Let us see the following implementation to get better understanding −

## Example

from collections import defaultdict class Solution: def solve(self, votes): count = len(votes[0]) cand = defaultdict(lambda: [0] * count) for v in votes: for i, c in enumerate(v): cand[c][i] += 1 return "".join(sorted(cand.keys(), key=lambda x: (cand[x], -ord(x)), reverse=True)) ob = Solution() votes = ["zyx", "zxy", "xyz"] print(ob.solve(votes))

## Input

["zyx", "zxy", "xyz"]

## Output

zxy

- Related Questions & Answers
- MySQL order by from highest to lowest value?
- Java Program to get the lowest and highest value in TreeSet
- Program to find maximal network rank in Python
- Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)
- Program to find the highest altitude of a point in Python
- A single MySQL query to find the highest and lowest among two tables?
- Python program to find the highest 3 values in a dictionary
- Highest and lowest in an array JavaScript
- Program to find final states of rockets after collision in python
- Program to Find Out the Maximum Final Power of a List in Python
- How to order by the highest value from two columns in MySQL?
- Highest and lowest value difference of array JavaScript
- Program to find highest common factor of a list of elements in Python
- Program to find lowest sum of pairs greater than given target in Python
- Program to find lowest possible integer that is missing in the array in Python