Rules:
The word should have the largest frequency.
In case multiple words have the same frequency, then choose the word that has the maximum length.
Assumptions:
The text has no special characters other than space.
The text would begin with a word and there will be only a single space between the words.
Perform case insensitive string comparisons wherever necessary.
SOLUTION:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def max_frequency_word_counter(data): | |
word="" | |
frequency=0 | |
data=data.lower() #for lowercase the sentence | |
lt=data.split() #split sentence in words | |
for e in lt: | |
freq=lt.count(e) #count the occurrence | |
if freq > frequency : | |
frequency=freq | |
word=e | |
elif freq == frequency : | |
if len(e) > len(word) : | |
frequency=freq | |
word=e | |
print(word,frequency) | |
data = "Work like you do not need money, love like your have never your been hurt, and dance like no one is watching" | |
max_frequency_word_counter(data) |
0 Comments