LinkedList
class.
fake_news_ms.py
.
"the"
and "trump"
both appear 35 times; "the"
is shown first because it is lexicographically less than "trump"
."are"
, "fbi"
, "not"
, and "over"
.
In the vernacular of sorting it is said that the count is a primary key and the word itself is a secondary key.
Instead of using a LinkedList
that contains instances of Node
,
change your code to use a Python list that contains instances of a new class,
Word
:
class Word
- An object of this class represents information about a word. It should contain (at least) the following attributes:
- _word: the word string.
- _count: a count of the number of occurrences of the word.
It should implement (at least) the following methods:
- __init__(self, word): initializes the object's attributes as follows: _word is set to word; _count is set to 1.
- word(self): returns the value of _word.
- count(self): returns the value of _count.
- incr(self): increments the value of _count.
- __lt__(self,other) returns True if self.word() is less than other.word() and False otherwise.
- __str__(self) and, optionally, __repr__(self).
On assignment 6 you wrote a sort()
method for LinkedList
.
In this problem the Python list of Word
instances must be sorted using the merge sort algorithm.
Code for the msort(L)
function is shown in the slides on recursion
class notes on recursion.