Leading Word Tracker
Implement a class WordTracker that counts the words in lines of text as they arrive and knows which word is in the lead. Write these methods:
WordTracker()starts with no words counted.add_line(line)counts every word ofline, one word at a time from left to right. Words are separated by single spaces. Returns the number of words inline.count_of(word)returns how many timeswordhas been counted so far, or0if it has never appeared.top_word()returns the word with the highest count. If several words share the highest count, it returns the one that reached that count first. Returns""if no words have been counted.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
WordTracker()
add_line("the cat saw the dog") returns 5
top_word() returns "the"
add_line("dog dog") returns 2
top_word() returns "dog"
count_of("the") returns 2
count_of("cow") returns 0
After the first line the leads with 2. The second line takes dog to 3, which is more than 2, so dog leads.
Constraints
linehas1to8words separated by single spaces, with no space at either end.- Every word is
1to6lowercase letters. - Each test makes
1to30method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.