Python——Pig Latin

# -*- 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)
            

“Pig Latin”是一个英语儿童文字改写游戏,整个游戏遵从下述规则:


(1). 元音字母是‘a’、‘e’、‘i’、‘o’、‘u’。字母‘y’在不是第一个字母的情况下,也被视作元音字母。其他字母均为辅音字母。例如,单词“yearly”有三个元音字母(分别为‘e’、‘a’和最后一个‘y’)和三个辅音字母(第一个‘y’、‘r’和‘l’)。

(2). 如果英文单词以元音字母开始,则在单词末尾加入“hay”后得到“Pig Latin”对应单词。例如,“ask”变为“askhay”,“use”变为“usehay”。

(3). 如果英文单词以‘q’字母开始,并且后面有个字母‘u’,将“qu”移动到单词末尾加入“ay”后得到“Pig Latin”对应单词。例如,“quiet”变为“ietquay”,“quay”变为“ayquay”。

(4). 如果英文单词以辅音字母开始,所有连续的辅音字母一起移动到单词末尾加入“ay”后得到“Pig Latin”对应单词。例如,“tomato”变为“omatotay”, “school” 变为“oolschay”,“you” 变为“ouyay”,“my” 变为“ymay ”,“ssssh” 变为“sssshay”。

(5). 如果英文单词中有大写字母,必须所有字母均转换为小写。

输入样例
Welcome to the Python world Are you ready

输出样例

elcomeway otay ethay ythonpay orldway arehay ouyay eadyray


elcomeway otay ethay ythonpay orldway arehay ouyay eadyray


对于上面的代码是自己写的,在Spyder下运行 。语句很简单,但程序写得有些冗余,欢迎大家多多指正,多谢支持~~

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值