Sort by

recency

|

1296 Discussions

|

  • + 0 comments
    s = input()
    c=Counter(s).items()
    s=sorted(list(c), key=lambda item: (-item[1], item[0]))[:3]
    [print(_[0],_[1]) for _ in s]
    
  • + 0 comments

    To the admin,

    No matter what I try, after all test cases are successful, the problem does not appear as solved on my profile and always has the try again button. Please check.

    Regards

    Prajwal

  • + 0 comments

    if name == 'main': s = input().strip() c = Counter(s) # Sort by (-frequency, alphabetical) top_3 = sorted(c.items(), key=lambda x: (-x[1], x[0]))[:3] for char, freq in top_3: print(char, freq)

  • + 0 comments

    First time using lambda expression to sort a dictionnary.

    Find my code below.

    The logic is to use collections.Counter to count every letter of the company name. Then sort them firstly by the occurence, then alphabetically.

    At the beginning, I've started by using the most_common() function of Counter, but it was quite difficult to implement it in a lambda expression for me. But As I said, it was my first time with lambda expression. So if you have some advices, I am open.

    Here my code :

    from collections import Counter
    
    if __name__ == '__main__':
        s = input()
        logo_name = Counter(s)
        #print(logo_name.keys())
    
        
        # Sort in the case where few letters have the same number of occurences
        key = lambda item: (-item[1], item[0])
        most_common_letters = sorted(logo_name.items(), key=key)
        #print(most_common_letters)
        
        # 3 most common letters
        most_common_letters = most_common_letters[:3]
        #print(most_common_letters)
        
        for element in most_common_letters:
            item, value = element
            print(f"{item} {value}")
    
  • + 0 comments
    import math
    import os
    import random
    import re
    import sys
    
    def occ_desc(string):
        dist = {}
        for i in string:
            dist[i] = dist.get(i, 0) + 1
    
        # Sort by frequency (descending), then by character (ascending)
        sorted_items = sorted(dist.items(), key=lambda item: (-item[1], item[0]))
    
        # Print top 3
        for key, value in sorted_items[:3]:
            print(f"{key} {value}")
    
    if __name__ == '__main__':
        s = input()
        occ_desc(s)