Back to Course Lecture 3: Dicts and Files 3 / 7
Tip: You can skip to a specific concept on this list
No concepts added for this class yet

Video Concepts in this class:

Top Count ▼Hint ▲Hint

Remember that you can search in the assets/googleu folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.

The objective is to implement a function `print_top(filename, n)` that counts how often each word appears in a text file and prints just the top 'n' most common words sorted so the most common word is first, then the next most common, and so on. 
The output should look like this:


word count
word count...


We will tackle this by steps in each of the questions below.

  • Implement a function word_count_dict(filename) that counts how often each word appears in a text file and returns a dict of the form word_count_dict[word] = count.

    Use string.split() with no arguments to split on all whitespace. Use string.lower() to make all words lower case, so that 'The' and 'the' count as the same word.

Please to take this test and advance your course progress

Top Count ▼Hint ▲Hint

Try first to understand how using key=function in sorted(items, key=Function, reverse=Boolean) can help you sort items by their count number.

Also remember that you can search in the assets/googleu folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.

The objective is to implement a function `print_top(filename, n)` that counts how often each word appears in a text file and prints just the top 'n' most common words sorted so the most common word is first, then the next most common, and so on. 
The output should look like this:


word count
word count...


We will tackle this by steps in each of the questions below.

  • Now implement the previously explained function print_top(filename, n) using word_count_dict(filename) as a utility function.

    Assume that n will be less than the length of the returned dict. Also, assume that you can just call word_count_dict(filename) and it will work as it's supposed to.

Please to take this test and advance your course progress

Word Count ▼Hint ▲Hint

Remember that you can search in the 'assets/googleu' folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.

  • Implement a print_words(filename) function that counts how often each word appears in a text file and prints:


    word1 count1
    word2 count2...


    Return the above list in order sorted by word (python will sort punctuation to come before letters -- that's fine).Use string.split() with no arguments to split on all whitespace. Use string.lower() to make all words lower case, so that 'The' and 'the' count as the same word.


Please to take this test and advance your course progress

Mimic ▼Hint ▲Hint

Remember that you can search inside the assets/googleu folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.

Implement the function `mimic(filename, n)` that returns a random list of words that mimic the original text inside the provided file. Using a dict, map each word that appears in the file to a list of all the words that immediately follow that word inside the file. Use that dict to generate for any random word key a following word, repeating this process for 'n' random words.


We will tackle this by steps in each of the questions below.

  • Implement the function mimic_dict(filename) that returns a dict that maps each word in the file with a list of all the words that immediately follow it inside the file. The list fo words can be in any order and should include duplicates. For example, the key "and" might have the list


    [ "then", "best", "then", "after", ... ]


    Given that inside the file "and" was followed by "then", "best", once again "then", "after", and so on. We'll say that the empty string is what comes before the first word in the file.

    Do a simple split() on whitespace to obtain all the words in a file. Rather than reading the file line by line, it's easier to read it into one giant string and split it once.

Please to take this test and advance your course progress

Mimic ▼Hint ▲Hint

Remember that you can search inside the assets/googleu folder, using Classroom.tv's python console, for text files with which you can practice and build up your answers before submitting.

Implement the function `mimic(filename, n)` that returns a random list of words that mimic the original text inside the provided file. Using a dict, map each word that appears in the file to a list of all the words that immediately follow that word inside the file. Use that dict to generate for any random word key a following word, repeating this process for 'n' random words.


We will tackle this by steps in each of the questions below.

  • Implement the function mimic(filename, n), as explained above, using the mimic_dict(filename) function as a utility function. Remember that the implemented function should return a word, then look up what words might come next and pick one at random as the next word, repeating the process for n random words (that'll print n+1 words!). Use an empty string ('') as the first word to prime things.

    If you ever get stuck with a word that is not in the dict, go back to the empty string to keep things moving.

    With a dict like the one returned by mimic_dict(filename) it should be fairly easy to emit random text that mimics the original. The standard python module random includes a random.choice(list) method which picks a random element from a non-empty list.

    Assume that you can just call mimic_dict(filename) and it will work as it's supposed to.

Please to take this test and advance your course progress

Lecture 3's contents:

- Dict type
- Files and some utilities
- Some exercises

By Nick Parlante. Support materials and exercises: code.google.com

Interactive Shell