Sort by

recency

|

1293 Discussions

|

  • + 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)
    
  • + 0 comments

    if name == 'main': s = input() char_count = {}

    # Count character frequency
    for i in s:
        if i in char_count:
            char_count[i] += 1
        else:
            char_count[i] = 1
    
    # Convert to list of tuples
    stored_items = list(char_count.items())
    
    # Manual sort by count descending
    for i in range(len(stored_items)):
        for j in range(i + 1, len(stored_items)):
            if stored_items[i][1] < stored_items[j][1]:
                stored_items[i], stored_items[j] = stored_items[j], stored_items[i]
    
    # Print each char and count on a new line if count > 1
    for item in stored_items:
        if item[1] > 1:
            print(item[0], item[1]) 5 test case fails out of 6 
    
  • + 0 comments
    from collections import Counter
    
    if __name__ == '__main__':
        for common in Counter(sorted(input())).most_common(3):
            print (common[0],common[1])
    
  • + 0 comments

    hi guies can use:

    if __name__ == '__main__':
        s = input()
    
        l = sorted( map(lambda x: (x,s.count(x)),set(s)), 
                    key=lambda x: (-x[1],x[0]))
    
        l = map(lambda x: str(x[0])+" "+str(x[1]),l[:3])
        print('\n'.join(l))