Sort by

recency

|

1304 Discussions

|

  • + 0 comments
    from collections import Counter
    
    
    if __name__ == '__main__':
    s = input().strip()
    ctr = Counter(s)
    for key, item in sorted(ctr.items(), key=lambda x: (-x[1], x[0]))[:3]:
        print(f"{key} {item}")
    
  • + 0 comments

    solution using Counter:

    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    from collections import Counter
    
    if __name__ == '__main__':
        s = input()   
        my_c = Counter(s)
        
        sorted_desc_c = dict(sorted(my_c.items(), key = lambda x: (-x[1], x[0])))
        k = 0
        for letter, count in sorted_desc_c.items():
            if(k<3):
                print(letter, count)
                k += 1
            
    
  • + 0 comments
    if __name__ == '__main__':
        s = input()
        
        counts = {}
        
        for i in s:
            counts[i] = s.count(i)
            
        counts = sorted(counts.items(), key=lambda x: (-x[1], x[0]))
        
        for i in range(3):
            print(counts[i][0], counts[i][1])
    
  • + 0 comments
    import math
    import os
    import random
    import re
    import sys
    from collections import Counter
    
    
    if __name__ == '__main__':
        s = input()
    
    count= Counter(s)
    sorted_items = sorted(count.items(), key=lambda x: (-x[1], x[0]))
    for char, freq in sorted_items[:3]:
        print(char, freq)
        
    
    ``
    
  • + 0 comments

    Did a little digging and found the Counter.most_common(n) method, which returns the list you need to iterate print(*) through. Sorted the string beforehand because .most_common(n) will sort by order of elements encountered.

    from collections import Counter
    
    
    if __name__ == '__main__':
        s = sorted(input())
        s_count = Counter(s)
        [print(*entry) for entry in s_count.most_common(3)]