-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompany Logo.py
46 lines (34 loc) · 1.24 KB
/
Company Logo.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
#! /bin/python3
"""
Title: Company Logo
Source: HackerRank
Link: https://www.hackerrank.com/challenges/most-commons/problem
Task: Accept a string and return a 3 letter string. The returned string is composed of the most common letters from the input. If 2 or more letters share the same
number of occurrence, then sort them in alphabetical order.
"""
from collections import Counter
if __name__ == '__main__':
# Change string input to list
s = list(input())
# Create a dictionary based off s and get number of duplicates
s_dict = Counter(s)
# Sort dictionary by value in ascending order
"""
NOTES:
lambda x: (-x[1], x[0])
1. Sort based on this key
2. First, sort values in descending order
3. Second, to change order to ascending, add "-"
4. If values cannot be sorted, sort by key
"""
s_dict = sorted(s_dict.items(), key= lambda x: (-x[1], x[0])) # <= sorted() returns a list
# Iterate multi-list (like a multi-array)
counter = 0
for i in s_dict:
# Print key and value from multi-list
print("{} {}".format(i[0], i[1]))
counter += 1
# Only print the top 3 pairs.
# Quit after 3
if counter == 3:
break