Write a program that counts the frequencies of each word in a text, and output e
ID: 3537295 • Letter: W
Question
Write a program that counts the frequencies of each word in a text, and output each word with its count and line numbers where it appears. We define a word as a contiguous sequence of non-white-space characters. (hint: split()) Note: different capitalizations of the same character sequence should be considered same word, e.g. Python and python, I and i. The input will be several lines with the empty line terminating the text. Only alphabet characters and white spaces will be present in the input. The output is formatted as follows:
Each line begins with a number indicating the frequency of the word, a white space, then the word itself, and a list of line numbers containing this word.
Python is a cool language but OCaml
is even cooler since it is purely functional
3 is 1 2
1 a 1
1 but 1
1 cool 1
1 cooler 2
1 even 2
1 functional 2
1 it 2
1 language 1
1 ocaml 1
1 purely 2
1 python 1
1 since 2
Explanation / Answer
# get input from user till blank line text="" lines=[] while(True): line=raw_input() if (line==""): break line=line.lower() text=text+line+" " lines.append(line) #get distinct words in input using set words=text.split() distinctWords=set(words) output=[] #count words for w in distinctWords: count = words.count(w) lineNums=[] # count lines word appears in for i in xrange(0,len(lines)): if w in lines[i]: lineNums.append(str(i+1)) #store ouput as a tuple having wordCount,word linenumbers output.append((str(count),str(w+" "+" ".join(lineNums)))) #sort output on wordCount,i.e, the first tuple value output=sorted(output,reverse=True) #dislay formatted output for x in output: print(" ".join(x))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.