Do this activity with a partner. You don't have to turn anything in.
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 return 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
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 ACT2-4.py
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.
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.