Sort by

recency

|

1287 Discussions

|

  • + 0 comments

    Did all the work in one line: s = Counter(sorted(list(input()))).most_common(3)

    Still needed two lines to print the output: for i in s: print(i[0], i[1])

    But basically the line takes the input(), puts the letters in a list, sorts them, then makes them a counter and takes the top 3 most common. Because the Counter takes the first from the list if the counts match, it is in alphabetical order as it counts.

  • + 0 comments

    Here is HackerRank Company Logo in python solution - https://programmingoneonone.com/hackerrank-company-logo-solution-in-python.html

  • + 0 comments

    It really sharpens your skills in identifying character frequency and sorting with custom rules. Highly recommend it to anyone brushing up on Python for interviews or real-world tasks. Cricbet99 Login Registration Online

  • + 0 comments
    from collections import Counter
    
    
    if __name__ == '__main__':
      s = input()
      count = Counter(s)
      sort = sorted(count.items(), key = lambda x: (-x[1], x[0]))
      
      for a,b in sort[:3]:
        print(a,b)
    
  • + 0 comments

    Extra challenge: do not use Counter

        s = list(input())
        
        d = {}
        for v in s:
            if v not in d:
                d[v] = 1
            else: 
                d[v] += 1
        
        # sort by value key first (alphabetical order), a through z 
        d = dict(sorted(d.items(), key= lambda x: x[0]))
        
        # sort dictionary by key (occurrence), largest to smallest
        d = dict(sorted(d.items(), key= lambda x: x[1],  reverse=True))
       
        # get first three outputs of the sorted dictionary
        res = {k: v for i, (k, v) in enumerate(d.items()) if i < 3}  
        
        for key, value in res.items():
            print(key, value)