forked from Rahi13/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_dictionary.py
65 lines (50 loc) · 1.61 KB
/
sort_dictionary.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
Challenge from Hacker Rank: https://www.hackerrank.com/challenges/most-commons/problem?isFullScreen=true
Given a string , which is the company name in lowercase letters, your task is to find the top three most common characters in the string.
Print the three most common characters along with their occurrence count.
Sort in descending order of occurrence count.
If the occurrence count is the same, sort the characters in alphabetical order.
'''
#!/bin/python3
import math
import os
import random
import re
import sys
import operator
if __name__ == '__main__':
s = input()
#Find the all the unique instances of a list
unique = []
for i in s:
if i not in unique:
unique.append(i)
#Count the frequency of the all the unique values in the given string
#create a dictionary called frequency which has letter:frequency
count = 0
frequency = {}
s1 = ''
for i in unique:
for j in s:
if i == j:
count = count + 1
else:
continue
frequency[i] = count
count = 0
#sort the frequency dictionary first by frequency (values) in descending order and then by keys in ascending order. When both match print.
#Make a dictionary of this order
letter = ''
value = 0
final = {}
for i in sorted(frequency.values(),reverse=True):
for j in sorted(frequency.keys(), reverse = False):
if frequency[j] == i:
letter = j
value = i
final[letter] = value
#get first three key:value of this dictionary
logo = dict(list(final.items())[:3])
#print result
for l,v in logo.items():
print(l,v)