# ACT2-3 functions.
# Remove the '#' characters as you write the functions below.

def readMobyDick():
    '''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()
    print 'Warning! Do not evaluate the entire list of MobyDick!'
    print 'Returning the first 1,000 characters of the text.'
    fileString = fileString[0:1000]
    myList = fileString.split()
    return myList

def countWordsInMobyDick():
    '''Counts the number of words in MobyDick.txt'''
    myList = readMobyDick()
    count = 0
    for word in myList:
        count = count + 1
    return count 

def avgWordLengthInMobyDick():
    '''Gets the average word length in MobyDick.txt'''
    myList = readMobyDick()
    avglen = 0
    for word in myList:
        avglen = avglen + len(word)
    avglen = avglen/float(len(myList))
    return avglen

def getLongestWordInMobyDick():
    '''Returns the longest word in MobyDick.txt'''
    myList = readMobyDick()
    longestword = ""
    for word in myList:
        if len(word) > len(longestword):
            longestword = word
    return longestword

def vocabSize():
    '''Gets the vocabulary size of Moby Dick'''
    myList = readMobyDick()
    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 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

