Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that counts the frequencies of each word in a text, and output e

ID: 3550601 • 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

sample output

1 a [1]

1 even [2]

1 functional [2]

1 language [1]

1 purely [2]

1 Python [1]

3 is [1, 2]

1 since [2]

1 OCaml [1]

1 but [1]

1 it [2]

1 cooler [2]

1 cool [1]



Code

from collections import Counter, defaultdict

data = """Python is a cool language but OCaml

is even cooler since it is purely functional"""

result = defaultdict(lambda: [0, []])

for i, l in enumerate(data.splitlines()):

for k, v in Counter(l.split()).items():

result[k][0] += v

result[k][1].append(i+1)

for k, v in result.items():

print('{1} {0} {2}'.format(k, *v))


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote