'''
Created on Sep 22, 2014

@author: Susan
'''



def isVowel(char):
    return char.lower() in 'aeiou'



# THIS IS REALLY BAD CODE WITH LOTS OF ERRORS
def countOfLetterThreeTimes(title, letter):
    # return true if letter occurs in title at least three times
    # scan the word character by character in a while loop 
    # why doesn't this work?
    count = 0
    pos = 0
    while (count < 3 ):
        if title[pos] == letter:
            count += 1
        pos += 1
    return True

def countOfLetterThreeTimes2(title, letter):
    # return true if letter occurs in title at least three times
    # scan the word character by character in a while loop 
    # why doesn't this work?
    count = 0
    pos = 0
    while (count < 3 and pos < len(title)):
        if title[pos] == letter:
            count += 1
        pos += 1
        '''   This is the same as below
    if count == 3:
        return True
    else:
        return False
        '''
    if count == 3:
        return True
    return False



#alternative:
def countOfLetterThreeTimes3(title, letter):
    # return true if letter occurs in title at least three times
    # scan the word character by character in a while loop 
    # why doesn't this work?
    count = 0
    pos = 0
    for i in range(0,len(title)):
        if title[i] == letter:
            count += 1
        if count == 3:
            return True
        #else      VERY BAD - loop only executes once
        #    return False
    return False
 
def countOfLetterThreeTimes4(title, letter):
    return title.count(letter) >= 3
     
# Now return the phrase from the beginning until 
# the word that has the third letter
def phraseThroughWordWithThirdLetter(title, letter):
    count = 0
    pos = 0
    while (count < 3 and pos < len(title)):
        if title[pos] == letter:
            count += 1
        pos += 1
    # this crashes if letter does not appear three times
    # title[pos] doesn't when run off end of string
    while (title[pos]!= " " and pos < len(title)):
        pos += 1
    if pos == len(title):
        return title
    else:  # found the letter three times
        return title[:pos]    
 
# Now return the phrase from the beginning until 
# the word that has the third letter
def phraseThroughWordWithThirdLetter2(title, letter):
    count = 0
    pos = 0
    while (count < 3 and pos < len(title)):
        if title[pos] == letter:
            count += 1
        pos += 1
    # This is safer, check if pos is valid and then 
    # check title[pos] which now must be a valid item
    while (pos < len(title) and title[pos]!= " "):
        pos += 1
    if pos == len(title):
        return title
    else:  # found the letter three times
        return title[:pos]    

title = "the goose is on the loose that is it"
print countOfLetterThreeTimes2(title, "o")
print countOfLetterThreeTimes2(title, "z")
print countOfLetterThreeTimes3(title, "o")
print countOfLetterThreeTimes3(title, "z")
print countOfLetterThreeTimes4(title, "o")
print countOfLetterThreeTimes4(title, "z")

print phraseThroughWordWithThirdLetter(title, "o")
print phraseThroughWordWithThirdLetter(title, "e")
print phraseThroughWordWithThirdLetter(title, "t")
print phraseThroughWordWithThirdLetter2(title, "s")
print phraseThroughWordWithThirdLetter2(title, "z")


