Activity 2-2

October 8, 2015

Task 1: Review from Last Class

  1. On the Desktop, make a folder called ACT2-2. Download, poem.txt, and MobyDick.txt, and save them in the ACT2-2 directory.
  2. Open IDLE, and then open Remind yourself of what the two functions actually do.
  3. In the ACT2-2 window, press F5 (or select Run > Run Module). What happens?
  4. In the IDLE Python shell that should have opened when you pressed F5, assign the list [-5,0,-2] to the variable myList. Use the avg3() function to compute the average.
  5. Use the addOne() function to add one to myList[2].
  6. The split function, without an input argument, splits a string on any whitespace (including spaces, newlines, tabs, etc.). Use the split() function to split the variable stringToSplit on different delimiters. Observe what happens when you enter the following expressions:
    • stringToSplit.split()
    • stringToSplit.split("\n")
    • stringToSplit.split("a")
    • stringToSplit.split("out")

Task 2: Read in the Shel Silverstein Poem, and split it into words

  1. In, write a function called readShel that opens the poem and returns a list of words.
    1. Download the Shel Silverstein poem, and assign the file location as a string to a variable named fileName. Write an expression that opens the file at fileName for reading and assigns the return value to myFile. The value of myFile now a file object that has been opened and can be read.
    2. Assign to fileString the contents of the file, which are returned by calling the read function on the file. fileString is now a big string that contains the poem.
    3. Close the file object myFile; you don't need it any longer, and the operating system really hates when you forget to close a file you've opened.
    4. Use the split function to create a list of words from the string, separated by whitespace, and return that list.

Task 3: Count the Number of Words in the Shel Silverstein Poem

  1. Now fill in the countWordsInShel() function.
    1. Grab the list of words in the poem using the readShel function you wrote.
    2. Create a variable with the value 0 that will keep track of your count.
    3. Use a for-loop to iterate over all words, incrementing your count each time.
    4. Return the count when the for-loop is finished - it should have been incremented once for each word in the poem!
  2. Repeat the previous two steps using "Moby Dick" instead of the poem. Fill in the functions readMobyDick and countWordsInMobyDick. They should be very similar to the functions you just wrote.

If we have time...

Task 4: Compute the Average Word Length in "Moby Dick"

  1. Fill in the avgWordLengthInMobyDick() function.
    1. Grab the list of words in the book using the readMobyDick function you wrote.
    2. Create a variable with the value 0 that will sum up the lengths of all words.
    3. Use a for-loop to iterate over all words, summing up the length of all words using the len function.
    4. With the total sum of all word-lengths (i.e., the total number of non-whitespace characters), get the average by dividing this sum by the number of words and return the average.