Chapter 6 Exercise Set 1

Dictionaries and sets doctest exercises

  1. """
      >>> type(thing1)
      <class 'dict'>
      >>> type(thing2)
      <class 'set'>
    """
    
  2. """
      >>> 4 in d
      True
      >>> 'name' in d
      False
      >>> d['name'] = 'Eric'
      >>> 'name' in d
      True
    """
    

Letter Counts

  1. Write a program that prompts the user for a string and returns a table of each letter that occurs in the string together with its number of occurances, irrespective of case. The table should list the letters in alphabetical order and display them in lower case. White space, punctuation, digits, or any other non-alphabetic characters should be ignored.

    A sample run of the program would look this this:

    Please enter a string: ThiS String has Upper and lower case Letters.
    
    a 3
    c 1
    d 1
    e 5
    g 1
    h 2
    i 2
    l 2
    n 2
    o 1
    p 2
    r 4
    s 5
    t 4
    u 1
    w 1
    

Mad Lib

  1. Write a program named madlib.py that makes a Mad Lib, prompting the user for parts of speech and then substituting their responses into a piece of text in place of orginial words from the text for which they are substituted.

    The following is a sample run of a program like this that uses the first paragraph from Alice’s Adventures in Wonderland as it’s source text:

    $ python3 madlib.py
    Please enter a verb ending in 'ing': sitting
    Please enter a adjective: silly
    Please enter a kind of relative: cousin
    Please enter a noun: apricot
    Please enter a verb ending in 'ing': babbling
    Please enter a girl's name: Marisol
    Please enter a noun: rug
    Please enter a noun: pin
    Please enter a noun: computer
    
    Marisol was beginning to get very silly of sitting by her cousin on
    the computer, and of having nothing to do: once or twice she had peeped
    into the rug her cousin was babbling, but it had no apricot or pin in it,
    ‘and what is the use of a rug,’ thought Marisol ‘without apricots or
    pins?’
    

Sorted list in a text file

  1. Write a program that asks the user for the name of a file containing a list of items, one per line, in a text file. The program should load the list from the file, sort it, and then write it back out to a file with the name sorted appended to the original name.

    For example, if the user types stuff.txt as the name of the input file, the program will load the list of items from stuff.txt, sort it, and write it back out to a file named stuffsorted.txt.

    Assuming the file states.txt contains the following:

    New Jersey
    Maine
    Virginia
    Rhode Island
    Maryland
    Alabama
    

    After running your program there would be a new file named statessorted.txt containing the following:

    Alabama
    Maine
    Maryland
    Rhode Island
    New Jersey
    Virginia
    

Project: counting words with e’s

  1. Write a program that takes a text file as an argument containing some of your favorite text - perhaps a poem, a speech, instructions to bake a cake, some inspirational verses to a song, etc., removes all punctuation, breaks the text into a list of words, and counts the number of words in your text that contain the letter ‘e’. Your program should print an analysis of the text like this:

    Your text contains 243 words, of which 109 (44.8%) contain an 'e'.
    

The first three words starting with each letter

  1. The file /usr/share/dict/words should exist on unix-based systems. If it does it contains a list of words in alphabetical order.

    Write a program that prints out the first three words beginning with each letter of the alphabet.

Project: Counting Words in Alice in Wonderland

  1. Write a program called alice_words.py that creates a text file named alice_words.txt containing an alphabetical listing of all the words found in alice_in_wonderland.txt together with the number of times each word occurs. The first 10 lines of your output file should look something like this:

    Word              Count
    =======================
    a                 631
    a-piece           1
    abide             1
    able              1
    about             94
    above             3
    absence           1
    absurd            2
    

    How many times does the word, alice, occur in the book?

    What is the longest word in Alice in Wonderland ? How many charactes does it have?