# -*- coding: utf-8 -*-
"""
Created on Sun Aug 03 02:39:21 2014
@author: TodLiuMeng
"""
import re
def vowels_First(string):
char=string[0]
if (char=='a' )| (char=='A'):
return True
elif (char=='e' )|( char=='E'):
return True
elif (char=='i' )|( char=='I'):
return True
elif (char=='o' )|( char=='O'):
return True
elif (char=='u' )|( char=='U'):
return True
return False
def vowels_figure(string):
for char in string:
if (char=='a' )| (char=='A'):
return True
elif (char=='e' )|( char=='E'):
return True
elif (char=='i' )|( char=='I'):
return True
elif (char=='o' )|( char=='O'):
return True
elif (char=='u' )|( char=='U'):
return True
elif (char=='y' )|( char=='Y'):
return True
return False
def Transtrcat(string):
if string[0]=='y':
n=re.search('[aeiou]',string).start()
else:
n=re.search('[aeiouy]',string).start()
for i in range(n):
temp=string[0]
string=string[1:]+temp
return string
def pig_latin(string):
string=string.lower()
s=string.split()
temp=""
newstring=""
for i in range(len(s)):
temp=s[i]
if vowels_figure(temp):
if vowels_First(temp):
temp+="hay"
elif ((temp[0]=='q') and (temp[1]=='u')):
temp=temp[2:]+"quay"
else:
temp=Transtrcat(temp)+"ay"
else:
temp+="ay"
newstring+=temp+' '
return newstring
string="Welcome to the Python world Are you ready"
print pig_latin(string)
elcomeway otay ethay ythonpay orldway arehay ouyay eadyray
elcomeway otay ethay ythonpay orldway arehay ouyay eadyray
对于上面的代码是自己写的,在Spyder下运行 。语句很简单,但程序写得有些冗余,欢迎大家多多指正,多谢支持~~