-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcollections_most_common.py
44 lines (30 loc) · 1.01 KB
/
collections_most_common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Problem Statement
You are given a string S.
The string contains only lowercase alphabetic characters.
Your task is to find top three most common characters in the string S.
Input Format
Single line containing string S.
Constraints
3<len(S)<104
Output Format
Print the most common characters along with their count of occurences in three lines.
Output must sorted in descending order of count of occurences.
If count of occurences is same then sort in ascending order of characters.
Sample Input
aabbbccde
Sample Output
b 3
a 2
c 2
Explanation
b is occuring 3 times. Hence, it is printed first.
Both a and c occur 2 times. So, a is printed in second line and c in third line because in ascending order of alphabets a comes ahead of c.
Note: The string S has at least 3 distinct characters.
"""
# Enter your code here. Read input from STDIN. Print output to STDOUT
from collections import Counter
a = list(raw_input())
c = Counter(a)
for i in sorted(c.items(), key=lambda x:(-x[1], x[0]))[:3]:
print i[0], i[1]