We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Python
  3. Collections
  4. Word Order
  5. Discussions

Word Order

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1405 Discussions, By:

recency

Please Login in order to post a comment

  • ayoadebenjamin
    19 hours ago+ 0 comments

    from collections import Counter

    def word_order(words): word_counts = Counter(words)

    values = ' '.join(str(value) for value in word_counts.values())
    dict_len = len(word_counts)
    
    return dict_len, values
    

    if name == 'main': n = int(input()) words = [input() for _ in range(n)] result = word_order(words) print(result[0]) print(result[1])

    0|
    Permalink
  • Rameeza
    1 day ago+ 0 comments
    N=int(input())
    s={}
    for _ in range(N):
    		word = input()
        if word in s.keys():
            s[i] += 1
        else:
            s[i] = 1
    print(len(s.keys()))
    print(*[i for i in s.values()])
    
    0|
    Permalink
  • yeamin_rajeev
    3 days ago+ 0 comments

    Not sure why the problem was rated 'medium' and why there's a low success rate. I used OrderedDict and it was accepted on first submission, here's my code:

    import collections
    
    n = int(input())
    ordered_count = collections.OrderedDict()
    uniq_words = 0
    for i in range(n):
        word_list = input()
        if (word_list in ordered_count):
            ordered_count[word_list] += 1
        else:
            ordered_count[word_list] = 1
            uniq_words += 1
    
    print (uniq_words)
    val_lst = list(ordered_count.values())
    print (*val_lst)
    
    0|
    Permalink
  • coguismod
    4 days ago+ 0 comments
    def main():
        n = int(input())
        words = {}
        counter = 0
        appearances = ""
        
        for i in range(n):
            word = input()
            
            if words.get(word) == None:
                counter += 1
                words[word] = 1
            else:
                words[word] += 1
        
        for x in words:
            value = str(words[x])
            
            if len(appearances) == 0:
                appearances = value
                continue
            
            appearances += " " + value
        
        print(counter)
        print(appearances)
        
    main()
    
    0|
    Permalink
  • embrown801
    4 days ago+ 0 comments

    Python Counter

    from collections import Counter
    
    
    def word_count(words):
        return Counter(words)    
    
    def print_word_counts(d):
        print(len(d))
        print(' '.join(map(str,list(d.values()))))
        
    if __name__ == "__main__":
        n = int(input())
        words = [input() for _ in range(n)]
        word_counts = word_count(words)
        print_word_counts(word_counts)
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy