University of Arizona, Department of Computer Science

CSc 120: Fake News

This program is behaviorally identical to the fake news program from Assignment 8, with three exceptions:
  1. The program will be altered to use Python lists instead of your LinkedList class.
  2. Sorting will be done using the merge sort algorithm. (Merging of lists starts on slide 50 in the class notes on recursion. Slides on merge sort then follow.)
  3. If more than one word has the same count, those words are shown in alphabetical order, as specified under Output Format.

Expected Behavior

As in Assignment 8. Your program should be in a file fake-news-ms.py.

Input format

As in Assignment 8.

Output format

As in Assignment 8 but with an important difference: if more than one word has the same count, those words are shown in alphabetical order. Here are two places in the example output where that ordering is evident:

In the vernacular of sorting it is said that the count is a primary key and the word itself is a secondary key.

Programming Requirements

  1. 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.
    • __str__(self) and, optionally, __repr__(self).

  2. On assignment 8 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 on slide 73 in the class notes on recursion.

Errors

As in Assignment 8.

Examples

Some examples of the behavior of this program are given here.