Activity 2-4

October 15, 2015

Task 1: Spotting Buggy Functions

Do this activity with a partner. You don't have to turn anything in.

  1. For each of the following Python functions, without actually running the code with a computer, answer the following: Does the function work properly in its current form? If not, what input will cause it to fail and how would you fix the function? The line numbers on the left-hand side are for reference only.
    def addTwo(x,y):
        '''Takes two numbers and returns their sum.'''
        z = x + y
        return x
    def subtractTwo(x,y):
        '''Takes two numbers and returns their difference'''
        z = x - y
        return t
    def multiplyTwo(x,y):
        '''Takes two numbers and returns their product'''
        z = x*y
    def divideTwo(x,y):
        '''Takes two numbers and returns their quotient.'''
        z = x/y
        return z
    def addList(myList):
        '''Takes a list of numbers and returns the sum of the elements.'''
        s = 0
        for w in myList():
            s = s + w
        return s

Task 2: Count the Vocabulary Size in Moby Dick

We will split into groups and brainstorm some methods for computing the size of Moby Dick's vocabulary, which is the set of unique words that appear in the book, not just the count of all words. After some brainstorming, as a class we'll implement a version of this new function, which we'll call vocabSize(). You can refer to to see the "skeleton code" of some helper functions you might like to implement along the way. You should download MobyDick.txt if you don't still have a copy from a previous activity or homework.

  1. In your group, write a function called vocabSize() that returns the length of the vocabulary used in Moby Dick. These might be helpful buildings blocks for your function:
    def noReplicates(wordList):
        '''Takes a list as argument, returns a list free of replicate items.'''
        # TODO. You might want to implement this.
    def isElementOf(myElement,myList):
        '''Takes a string and a list and returns True if the string is
        in the list and False otherwise.'''
        # TODO. You might want to implement this.
    def vocabSize():
        '''Gets the vocabulary size of Moby Dick.'''
        myList = readMobyDickShort()
        # Do some other stuff here...
        # Then return the vocabulary size.
  2. When you've finished, check that your function works properly. Are there cases where it does not give an accurate count?