Pig Latin (Python)

基本规则:

1.对于以辅音开头的单词,初始元音之前的所有字母都放在单词序列的末尾。然后,添加“ay”。

2.对于以元音开头的单词,只需在末尾添加“yay“,包括”半元音“。

k_list=['a','e','i','o','u','y']

str_words=input()
list_words=str_words.split()

#装处理后的单词
piglatin=[]

for word in list_words:

    # 确认是否为单词
    wasalpha = word.isalpha()

    # 判断特殊情况
    wastitle = word.istitle()
    wasupper = word.isupper()
    word = word.lower()

    #存储单词前后特殊字符
    prefixletter=''
    suffixletter=''

    #存储辅音
    preconsonants=''

    if wasalpha:
        while len(word)>0 and not word[0].isalpha():
            prefixletter+=word[0]
            word=word[1:]
        while len(word)>0 and not word[-1].isalpha():
            suffixletter+=word[-1]
            word=word[:-1]

        while len(word)>0 and word[0] not in k_list:
            preconsonants+=word[0]
            word=word[1:]

        #假设单词开头无辅音
        #后加‘yay’,否则加‘ay’
        if preconsonants == '':

            word+='yay'

        else:
            word+=preconsonants + 'ay'

        if wasupper:
             word=word.upper()

        if wastitle:
            word=word.title()

        piglatin.append(prefixletter + word + suffixletter)

    else:

        piglatin.append(word)

print(' '.join(piglatin))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值