Description A group of Vatican City police officers are planning a trip to the United States. Since they only speak Pig Latin, they will need to translate a lot of English phrases. Write a simple program to perform basic English to Pig Latin translation. Translation rules 1. If a word has no letters, don't translate it. 2. All punctuation should be preserved. 3. If the word begins with a capital letter, then the translated word should too. 4. Separate each word into two parts. The first part is called the "prefix" and extends from the beginning of the word up to, but not including, the first vowel. (The letter "y" will be considered a vowel.) The Rest of the word is called the "stem". 5. The Pig Latin text is formed by reversing the order of the prefix and stem and adding the letters "ay" to the end. For example, "sandwich" is composed of "s" + "andwich" + "ay" + "." and would translate to "andwichsay." 6. If the word contains no consonents, let the prefix be empty and the word be the stem. The word ending should be "yay" instead of merely "ay." For example, "I" would be "Iyay". Phase 1 Your first task is to produce a function that takes one argument, a word, and returns the portion of the word up to, but not including the first vowel. For example, if you sent 'banana' to your function, it should return 'b'. Sending 'Sibley' should return 'S', 'stream' should return 'str', and 'break' should return 'br'. Print out your working function and a sample run. Phase 2 Using what you learned from Phase 1, write a function (or functions) that takes a single word as an argument and returns the word with the prefix and stem reversed. Phase 3 Modify the function from Phase 2 to properly handle the "ay" word ending, capital letters, and punctuation. Final Phase Input Your program should perform translation one line at a time. The program will continue to accept input until it is terminated by a Ctrl-D character. Output Program output should follow each input line. The translation rules will determine how each word is translated. Sample session --> Stop Opstay --> No littering Onay itteringlay --> No shirts, no shoes, no service Onay irtsshay, onay oesshay, onay ervicesay --> No persons under 14 admitted Onay ersonspay underay 14 admitteday --> Hey buddy, get away from my car! Eyhay uddybah, etgay awayyay omfray ymay arcay! --> ^D |
program 1 :
#!/usr/bin/env python
import test1
try :
while 1:
Pig_latin=raw_input('-->')
a=Pig_latin.split()
i=0
while i<len(a) :
if i == 0:
test1.Pig_word0(a[i])
else :
test1.Pig_word1(a[i])
i=i+1
print ''
except EOFError:
print '\n'
pass
Program 2:
#! /usr/bin/env python
def Pig_word0(n):
""" This is a function which can make string translate to pig latin and first letter become capital. """
# determined string include a,e,i,o,u or not
Prefix=[]
for i in n :
if i in "aeiouy":
break
else :
Prefix.append(i)
# determined which is prefix and stem
Stem=[]
for i in range(len(n)-len(Prefix)) :
Stem.append(n[i+len(Prefix)])
# combine prefix and stem
Pig_Latin_pre=Stem+Prefix
# translate first letter become capital and other is lower case
Pig_Latin_pre_len = len(Pig_Latin_pre)
i = 0
while i < Pig_Latin_pre_len:
if i == 0:
Pig_Latin_pre[i] = Pig_Latin_pre[i].upper()
else :
Pig_Latin_pre[i] = Pig_Latin_pre[i].lower()
i=i+1
# determined if string is number ignore, otherwise add ay at the end of whole word
for i in Pig_Latin_pre:
if i in "123456789":
i
break
else :
Pig_Latin_pre.append('ay')
break
# exchange ",!" to the end.
temp_a=[]
temp_b=[]
for i in Pig_Latin_pre :
if i in ',!' :
temp_a.append(i)
else :
temp_b.append(i)
Pig_Latin_pre=temp_b+temp_a
# print word out
Pig_Latin = ''.join(Pig_Latin_pre)
print Pig_Latin+' ',
def Pig_word1(n):
"""This is a function which can make string translate to pig latin directly."""
# determined string include a,e,i,o,u or not
Prefix=[]
for i in n :
if i in "aeiouy":
break
else :
Prefix.append(i)
# determined which is prefix and stem
Stem=[]
for i in range(len(n)-len(Prefix)) :
Stem.append(n[i+len(Prefix)])
# combine prefix and stem
Pig_Latin_pre=Stem+Prefix
# determined if string is number ignore, otherwise add ay at the end of whole word
for i in Pig_Latin_pre:
if i in "123456789":
i
break
else :
Pig_Latin_pre.append('ay')
break
# exchange ",!" to the end.
temp_a=[]
temp_b=[]
for i in Pig_Latin_pre :
if i in ',!' :
temp_a.append(i)
else :
temp_b.append(i)
Pig_Latin_pre=temp_b+temp_a
# print word out
Pig_Latin = ''.join(Pig_Latin_pre)
print Pig_Latin+' ',
#Example 3.28. Splitting a String
When I make this program meet some problem :
1. EOFError must use try and except
2. String must seperate a=Pig_latin.split().
Reference :
http://openbookproject.net/pybiblio/practice/wilson/piglatin.php
0 意見:
張貼留言