I have just completed a fun exercise on HackerRank named Company Logo. The task was to “print the three most common characters along with their occurrence count”.

Task

Two key conditions were:

  • Sort in descending order of occurrence count,
  • If the occurrence count is the same, sort the characters in alphabetical order.

As with most of the problems, there is more than one solution, but because I recently explored the collections module and DefaultDict in particular, it was the first thing that came to my mind - I would insert each character into the dictionary and on each occurence increment the value by 1.

The more challenging condition came afterwards, where I had to sort it first by value and if the value is the same, by the key. Here I used the built-in sorted() function and it was an opportunity to understand this function more deeply because it can be used in a variety of ways.

Example:

Sample input:

1
aabbbccde

Sample output:

1
2
3
b 3
a 2
c 2

My Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from collections import defaultdict

company_name_chars = defaultdict(int) # init defaultdict with integer as default value
company_name = 'tctesvgnclzykpmbcqixlonfidpdjbynrcpxsjmucpeyormenrjbibafpzkq...' # Example cut here as it's too long, btw interesting company name choice :)

for letter in company_name:

    company_name_chars[letter] += 1 # Increment each char if already in the name, else create a new key

# Here I learned a lot of new things about sorted()
# you can also sort on the dicts.items() method, which returns a list of tuples with (key, value)
# then use lambda function to sort first by value (descending order by inserting '-'), then by key
char_occurences = sorted(company_name_chars.items(), key=lambda item: (-item[1], item[0]))

# Here I iterate over the sorted list of tuples, which I sliced to first 3 tuples, because I only need top 3 values
for letter, count in char_occurences[:3]:

    print(letter, count)

Output from the example:

1
2
3
m 47
n 46
w 46

Everytime I finish an exercise I check the discussion for the solutions of other users - to learn and see other ways of achieving the same goal. Indeed there were many solutions, some of which looked like magic (having only 2-3 lines), but these kinds of solutions sometimes compromise readability of the code, which is also a very important part of a clean code.