Sort by

recency

|

1251 Discussions

|

  • + 0 comments
    if __name__ == '__main__':
        s = input()
        char_count = Counter(s)
        sorted_char_count = sorted(char_count.items(), key=lambda x: (-x[1], x[0]))
        
        for char, count in sorted_char_count[:3]:
            print(f"{char} {count}")
    
  • + 0 comments
    if __name__ == '__main__':
        s = input()
        d = dict()
        for letter in s:
            d[letter] = d.setdefault(letter, 0) + 1
        sorted_list = sorted(d.items(), key=lambda x: (-x[1], x[0]))
        for item in sorted_list[:3]:
            print(*item)
    
  • + 0 comments

    Without modifying the code already given :)

    from collections import Counter
    
    if __name__ == '__main__':
        s = input()
        c = Counter(s)
        [print(letter, rep) for letter, rep in Counter(sorted(c.elements())).most_common(3)]
    
  • + 0 comments
    def my_func(s):
        chars_dict = {}
        str_list = list(s)
        for char in str_list:
            if char in chars_dict:
                chars_dict[char] += 1
            else:
                chars_dict[char] = 1
        return chars_dict
    if __name__ == "__main__":
        s = input()
        norm_list = my_func(s).items()
        srtd_norm_list = sorted(norm_list)
        sorted_data = sorted(srtd_norm_list, key=lambda x: x[1], reverse=True)
        counter = 0
        for i, j in sorted_data:
            print(f"{i} {j}")
            counter += 1
            if counter == 3:
                break
    
  • + 0 comments
    if __name__ == '__main__':
        s = input()
        ch = {}
    
        for i in s:
            ch[i] = ch.get(i, 0) + 1
            
        sorted_by_keys = dict(sorted(ch.items()))
        sorted_by_values = dict(sorted(sorted_by_keys.items(), key=lambda item: item[1], reverse=True)[:3])
    
        for key, value in sorted_by_values.items():
            print(key, value)