# ACT2-4 functions.

'''This Program returns the vocabulary size of Moby Dick.'''

# Remove the '#' characters as you write the functions below.

def readMobyDickShort():
    '''Reads the text file MobyDick.txt and returns a list of words from the first 1000 characters'''
    fileName = 'MobyDick.txt'
    myFile = open(fileName,'r')
    fileString = myFile.read()
    myFile.close()
    fileString = fileString[0:1000]
    myList = fileString.split()
    return myList

def readMobyDickAll():
    '''Reads the text file MobyDick.txt and returns a list of words from the entire text'''
    fileName = 'MobyDick.txt'
    myFile = open(fileName,'r')
    fileString = myFile.read()
    myFile.close()
    print 'Warning! Returning the entire list of words!'
    myList = fileString.split()
    return myList

def vocabSize():
    '''Gets the vocabulary size of Moby Dick'''
    myList = readMobyDickShort()
    uniqueList = noReplicates(myList)
    return len(uniqueList)
    
def noReplicates(wordList):
    '''takes a list as argument, returns a list free of replicate items.
    slow implementation.'''
    listToReturn = []
    for word in wordList:
        if not(isElementOf(word,listToReturn)):
            listToReturn = listToReturn + [word]
    return listToReturn

def testNoReplicates():
    '''Test for the noReplicates() function'''
    testList = ['apple','banana','carrot','carrot','banana','apple']
    print 'starting list is',testList
    solList = ['apple','banana','carrot']
    print 'solution list should be',solList
    
    uniqueList = noReplicates(testList)
    print 'ending list is',uniqueList
    return
        
def isElementOf(myElement,myList):
    '''takes a string and a list and returns True if the string is
    in the list and False otherwise.'''
    for item in myList:
        if myElement == item:
            return True
    return False

def testIsElementOf():
    '''Test for the isElementOfFunction'''
    testList = ['a','b','c']
    print 'test list is',testList
    value = isElementOf('c',testList)
    print 'c should be in the list: value is',value
    
    value = isElementOf('d',testList)
    print 'd should not be in the list: value is',value
    return
