
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to find top three mostly occurred letters from company name
Suppose we have a company name as string. We have to find the most common three characters from the company name and show them by following these rules −
- Pick most frequent three letters
- Sort them in descending order
- If the frequencies of some characters are same then take by their alphabetical order
So, if the input is like s = "TUTORIALSPOINT", then the output will be [[3, 'T'], [2, 'I'], [2, 'O']]
To solve this, we will follow these steps −
- x := a map containing letters and frequencies of letters in s
- res := a new list
- for each i in x, do
- insert pair (x[i], i) into ret
- res := res after sorted based on alphabetical order
- res := res after sorted based on frequency in reverse order
- return first three items from res
Example
Let us see the following implementation to get better understanding
from collections import Counter def solve(s): x = Counter(s) res = [] for i in x: res.append([x[i], i]) res = sorted(res, key=lambda cnt: cnt[1]) res = sorted(res, key=lambda cnt: cnt[0], reverse=True) return res[:3] s = "TUTORIALSPOINT" print(solve(s))
Input
"TUTORIALSPOINT"
Output
[[3, 'T'], [2, 'I'], [2, 'O']]
- Related Articles
- Program to find elements from list which have occurred at least k times in Python
- Program to find length of longest word that can be formed from given letters in python
- Program to find minimum deletion cost to avoid repeating letters in Python
- Program to find expected value of maximum occurred frequency values of expression results in Python
- Program to find all words which share same first letters in Python
- Get first three letters from every string in C#
- Program to find out the minimum path to deliver all letters in Python
- Program to find n length string made of letters from m sized alphabet with no palindrome in Python
- Program to remove string characters which have occurred before in Python
- Program to find three unique elements from list whose sum is closest to k Python
- Python program to find the maximum of three numbers
- Program to count number of words we can generate from matrix of letters in Python
- Python program to find common elements in three sorted arrays?
- JavaScript regex program to display name to be only numbers, letters and underscore.
- Difference between Vendor Name and Company Name

Advertisements