Write a python program that accepts a text and displays a string which contains the word with the largest frequency in the text and the frequency itself separated by a space.
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.
Sample Input | Expected Output |
"Work like you do not need money love like you have never been hurt and dance like no one is watching" | like 3 |
"Courage is not the absence of fear but rather the judgement that something else is more important than fear" | fear 2 |
def max_frequency_word_counter(data): | |
word="" | |
frequency=0 | |
lower_data=data.lower() | |
lst=[] | |
lst=lower_data.split() | |
repeat=[] | |
a=[] | |
i=0 | |
while(i < len(lst)): | |
if lst.count(lst[i]) > 1: | |
repeat.append(lst[i]) | |
i=i+1 | |
else: | |
i=i+1 | |
for i in repeat: | |
a=repeat.count(i) | |
for i in range(0,len(repeat)): | |
if repeat.count(repeat[i])==a: | |
word=word+repeat[i] | |
frequency=a | |
break | |
print(word,frequency) | |
#Provide different values for data and test your program. | |
data="Listen to the big clock Tick tock tick" | |
max_frequency_word_counter(data) |
Write python function, sms_encoding() which accepts a sentence and converts it into an abbreviated sentence to be sent as SMS and returns the abbreviated sentence.
Rules are as follows:
a. Spaces are to be retained as is
b. Each word should be encoded separately
If a word has only vowels then retain the word as is
If a word has a consonant (at least 1) then retain only those consonants
Note: Assume that the sentence would begin with a word and there will be only a single space between the words.
Sample Input | Expected Output |
I love Python | I lv Pythn |
MSD says I love cricket and tennis too | MSD sys I lv crckt nd tnns t |
I will not repeat mistakes | I wll nt rpt mstks |
vowels=['a','e','i','o','u'] | |
def sms_encoding(data): | |
temp=[] | |
l=data.split(" ") | |
for word in l: | |
temp_word=[] | |
for letter in word: | |
if letter.lower() not in vowels: | |
temp_word.append(letter) | |
if len(temp_word)==0: | |
temp_word.append(word) | |
temp.append("".join(temp_word)) | |
return " ".join(temp) | |
data="GOOD DAYS AND BAD DAYS" | |
print(sms_encoding(data)) |
0 Comments
Post a Comment