|
| 1 | +''' |
| 2 | +Problem Statement |
| 3 | +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. |
| 4 | +
|
| 5 | +Rules: |
| 6 | +
|
| 7 | +The word should have the largest frequency. |
| 8 | +
|
| 9 | +In case multiple words have the same frequency, then choose the word that has the maximum length. |
| 10 | +
|
| 11 | +Assumptions: |
| 12 | +
|
| 13 | +The text has no special characters other than space. |
| 14 | +
|
| 15 | +The text would begin with a word and there will be only a single space between the words. |
| 16 | +
|
| 17 | +Perform case insensitive string comparisons wherever necessary. |
| 18 | +
|
| 19 | +Sample Input |
| 20 | +
|
| 21 | +"Work like you do not need money love like you have never been hurt and dance like no one is watching" |
| 22 | +"Courage is not the absence of fear but rather the judgement that something else is more important than fear" |
| 23 | +
|
| 24 | +Expected Output |
| 25 | +
|
| 26 | +like 3 |
| 27 | +fear 2 |
| 28 | +''' |
| 29 | + |
| 30 | +#lex_auth_0127382283825971201450 |
| 31 | + |
| 32 | +def max_frequency_word_counter(data): |
| 33 | +word="" |
| 34 | +frequency=0 |
| 35 | + |
| 36 | +#start writing your code here |
| 37 | +#Populate the variables: word and frequency |
| 38 | +word="" |
| 39 | +frequency=0 |
| 40 | + |
| 41 | +#start writing your code here |
| 42 | +#Populate the variables: word and frequency |
| 43 | +dict={} |
| 44 | +data1=data.lower() |
| 45 | +list=data1.split() |
| 46 | +my_set=set(list) |
| 47 | +list2=[] |
| 48 | +for i in my_set: |
| 49 | +dict[i]=list.count(i) |
| 50 | + |
| 51 | +max=0 |
| 52 | + |
| 53 | +for key,value in dict.items(): |
| 54 | +if max<dict[key]: |
| 55 | +max=dict[key] |
| 56 | +#list3=[] |
| 57 | +list2=[] |
| 58 | +frequency=max |
| 59 | +for key,value in dict.items(): |
| 60 | +if max==dict[key]: |
| 61 | +list2.append(key) |
| 62 | + |
| 63 | +word=list2[0] |
| 64 | +for i in range(1,len(list2)): |
| 65 | +if len(word)<len(list2[i]): |
| 66 | +word=list2[i] |
| 67 | + |
| 68 | +print(word,frequency) |
| 69 | + |
| 70 | + |
| 71 | +# Use the below given print statements to display the output |
| 72 | +# Also, do not modify them for verification to work |
| 73 | +#print(word,frequency) |
| 74 | + |
| 75 | + |
| 76 | +#Provide different values for data and test your program. |
| 77 | +data="Work like you do not need money, love like you have never been hurt, and dance like no one is watching" |
| 78 | +max_frequency_word_counter(data) |
0 commit comments