import string

def loadWords():
    """
    Returns a list of valid words. Words are strings of lowercase letters.
    Depending on the size of the word list, this function may
    take a while to finish.
    """
    inFile = open('ss.txt', 'r')
    wordList = inFile.read().split()
    #print " ", len(wordList), "words loaded."
    return wordList
def isWord(wordList, word):
    """
    Determines if word is a valid word.
    wordList: list of words in the dictionary.
    word: a possible word.
    returns True if word is in wordList.
    Example:
    >>> isWord(wordList, 'bat') returns
    True
    >>> isWord(wordList, 'asdf') returns
    False
    """
    word = word.lower()
    word = word.strip(" !@#$%^&*()-_+={}[]|\\:;'<>?,./\"")
    return word in wordList

def buildCoder(shift):
    """
    Returns a dict that can apply a Caesar cipher to a letter.
    The cipher is defined by the shift value. Ignores non-letter characters
    like punctuation, numbers, and spaces.

    shift: 0 <= int < 26
    returns: dict
    """
    mapper={}
    for ch in string.ascii_lowercase:
        if (ord(ch)+shift)>ord('z'):
            mapper[ch]=chr(ord(ch)+shift-ord('z')+ord('a')-1)
        else:
            mapper[ch]=chr(ord(ch)+shift)
    for ch in string.ascii_uppercase:
        if (ord(ch)+shift)>ord('Z'):
            mapper[ch]=chr(ord(ch)+shift-ord('Z')+ord('A')-1)
        else:
            mapper[ch]=chr(ord(ch)+shift)
    return mapper
def applyCoder(text, coder):
    """
    Applies the coder to the text. Returns the encoded text.
    text: string
    coder: dict with mappings of characters to shifted characters
    returns: text after mapping coder chars to original text
    """
    res=''
    for ch in text:
        if ch in string.ascii_lowercase:
            res = res + coder[ch]
        elif ch in string.ascii_uppercase:
            res = res + coder[ch]
        else:
            res = res + ch
    return res

def applyShift(text, shift):
    """
    Given a text, returns a new text Caesar shifted by the given shift
    offset. Lower case letters should remain lower case, upper case
    letters should remain upper case, and all other punctuation should
    stay as it is.
    text: string to apply the shift to
    shift: amount to shift the text (0 <= int < 26)
    returns: text after being shifted by specified amount.
    """
    return applyCoder(text,buildCoder(shift))


def findBestShift(wordList, text):
    """
    Finds a shift key that can decrypt the encoded text.
    text: string
    returns: 0 <= int < 26
    """
    noWords=0
    bestShift=0
    Shift=0
    while Shift<26:
        decodedMessage = applyShift(text,Shift)
        words = decodedMessage.split(" ")
        counter = 0
        for w in words:
            if isWord(wordList,w)==True:
                counter +=1
        if counter > noWords:
            noWords = counter
            bestShift = Shift   
        Shift += 1
    return bestShift

wL = loadWords()
txt = applyShift('cipher', 8)
print findBestShift(wL, txt)
