# Class exercises from week 3

import string

#------------------------------------------------------------
# this just prints out the phrase unchanged

def echo():
    phrase = raw_input("Enter a phrase: ")
    print phrase

# >>> echo()
# Enter a phrase: the rain in spain
# the rain in spain

#------------------------------------------------------------
# this prints each character of the phrase on separate lines

def echoChars():
    phrase = raw_input("Enter a phrase: ")
    for letter in phrase:
        print letter

# >>> echoChars()
# Enter a phrase: the rain in spain
# t
# h
# e
# .
# .
# .

#------------------------------------------------------------
# this prints each word of the phrase on separate lines

def echoWords():
    phrase = raw_input("Enter a phrase: ")
    words = string.split(phrase)
    for w in words:
        print w

# >>> echoWords()
# Enter a phrase: the rain in spain
# the
# rain
# in
# spain

#------------------------------------------------------------
# like echoWords, but prints words fully capitalized

def yellWords():
    phrase = raw_input("Enter a phrase: ")
    words = string.split(phrase)
    for w in words:
        print string.upper(w)

# >>> yellWords()
# Enter a phrase: the rain in spain
# THE
# RAIN
# IN
# SPAIN

#------------------------------------------------------------
# this constructs an acronym from the phrase

def acronym():
    phrase = raw_input("Enter a phrase: ")
    words = string.split(phrase)
    acro = ""
    for w in words:
        acro = acro + w[0]
    print string.upper(acro)

# >>> acronym()
# Enter a phrase: the rain in spain
# TRIS

#------------------------------------------------------------
# this computes the average length of all the words

def averageLen():
    phrase = raw_input("Enter a phrase: ")
    wordList = string.split(phrase)
    numWords = len(wordList)
    total = 0
    for word in wordList:
        total = total + len(word)
    average = float(total) / numWords
    print "Average word length is", average

# >>> averageLen()
# Enter a phrase: the rain in spain
# Average word length is 3.5

#------------------------------------------------------------
# this prints the words in reverse order, on the same line

def echoReverse():
    phrase = raw_input("Enter a phrase: ")
    words = string.split(phrase)
    reversal = ""
    for w in words:
        reversal = w + " " + reversal
    print reversal

# >>> echoReverse()
# Enter a phrase: the rain in spain
# spain in rain the

#------------------------------------------------------------
# this prints the words in reverse order, on separate lines

def echoWordsReverse():
    phrase = raw_input("Enter a phrase: ")
    words = string.split(phrase)
    numWords = len(words)
    for i in range(1, numWords+1):
        print words[-i]

# >>> echoWordsReverse()
# Enter a phrase: the rain in spain
# spain
# in
# rain
# the

#------------------------------------------------------------
# another way to do it

def echoWordsReverse2():
    phrase = raw_input("Enter a phrase: ")
    words = string.split(phrase)
    reversal = ""
    for w in words:
        reversal = w + " " + reversal
    # now print out the reversed words
    for w in string.split(reversal):
        print w

# >>> echoWordsReverse2()
# Enter a phrase: the rain in spain
# spain
# in
# rain
# the

#------------------------------------------------------------
