As a newbie programmer

Word Frequency @Dcoder

Dcoder icon

Title: Word Frequency

Problem: You will be given a group of words. You need to print all unique words in that group in descending order of their frequency, i.e the word repeated most number of times will come first. In case of two words having the same frequency, the word listed earlier in the group will be printed first.

Input: First line contains N, the number of words.

Next line contains N space-separated words.

Output: Print the words in descending order of their frequency

Constraints: 1 ≤ N ≤ 20

1 ≤ word[i].length ≤ 40

Sample Input: 12

can you can a can as a canner can can a can

Sample Output: 

can a you as canner

input()
words = input().split()
ordered = {}
for word in words:
    if word not in ordered:
        ordered[word] = words.count( word )

print( " ".join( sorted( ordered, key=lambda k: ordered[k], reverse=True) ) )

How do you rate this article?

1


Lochard
Lochard

20240228 Arrived in the UK for about 2 week. All my fears persist. Some are even getting worse. https://github.com/locharp/asylum_diary/


As a newbie programmer
As a newbie programmer

Sharing entry level codes. My snippets on GitHub https://github.com/locharp/code-snippets

Publish0x Publish0x

Reward the author with $0.01 in crypto, and earn yourself as you read!

20% to author / 80% to me.
Rewards are FREE. Publish0x pays them, not you.

Page not displaying correctly?