I'm doing some practice in Hackerrank for Python 3 learning.
In the task Most Common you are given a string which contains only lowercase English characters and you need to find the top three most common characters in that string.
I met some questions.
My solution for this problem is below:
#!/bin/python3
import sys
if __name__ == "__main__":
s = input().strip()
ch_dict = {}
for ch in s:
if ch in ch_dict : ch_dict[ch] +=1
else: ch_dict[ch] = 1
result = sorted(ch_dict.items(),key=lambda d:d[1],reverse=True)
for i in result:
if i[1] != 1:
print(" ".join(map(str,i)))
When I test this code in local environment, it works!
But in online test, it may fail!
For this input:
aabbbccde
I submit a lot of times, sometimes get right answer like this:
b 3
a 2
c 2
and can also get this:
b 3
c 2
a 2
It seems like the sort can be unstable ? OR what's matter whit my code ? OR is something wrong in Hackerrank environment ?
How can i guarantee my output?