Pig Latin

“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

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import re
# import math
# import datetime
# import sys

# from PyQt4.QtCore import *
# from PyQt4.QtGui import *

# import numpy as np
# import matplotlib.pyplot as plt
# import copy

result = ''

s = raw_input()
s = s.split()
# print s
# print s[0]
# l1 = ['a', 'e', 'i', 'o', 'u']

for word in s:
    word = word.lower()
    # print word
    if re.match('[aeiou]', word):
        result = result + word + 'hay' + ' '
        # print word + 'hay'
    elif re.match('qu', word):
        # print word[2:] + 'quay'
        result = result + word[2:] + 'quay' + ' '
    # else:
    #     for index, alph in enumerate(word):
    #         if word[0] == 'y':
    #             if re.search('[aeiou]', alph):
    #              # word = word[index+1:] + word[:index+1] + 'ay'
    #              # print word
    #                 break
    #         else:
    #             if re.search('[aeiouy]', alph):
    #                 break
    #     word = word[index:] + word[:index] + 'ay'
    #     print word

    elif not re.search('[aeiouy]', word):
        # print word + 'ay'
        result = result + word + 'ay' + ' '
    else:
        for index, alph in enumerate(word[1:]):

            if re.search('[aeiouy]', alph):
             # word = word[index+1:] + word[:index+1] + 'ay'
             # print word
                    break
            # else:
            #     if re.search('[aeiouy]', alph):
            #         break
        # print word[index+1:] + word[:index+1] + 'ay'
        result = result + word[index+1:] + word[:index+1] + 'ay' + ' '


print result[:-1]
### 实现Pig Latin字母加密算法 #### Pig Latin 加密规则概述 对于给定的一个英文单词,如果该单词以辅音开头,则将第一个辅音或辅音簇移动到词尾,并加上 "ay";如果以元音开头,则直接在词尾加 "way"[^1]。 #### Python 示例代码实现 下面是一个简单的Python函数来展示如何转换单个单词: ```python def pig_latin(word): vowels = 'aeiou' if word[0].lower() in vowels: return word + 'way' # 如果是以元音开头 for i, letter in enumerate(word): if letter.lower() in vowels: return word[i:] + word[:i] + 'ay' # 移动辅音至结尾并添加'ay' return word + 'ay' # 对于全由辅音组成的特殊情况 ``` 此函数首先检查输入的`word`是否以元音字符('a', 'e', 'i', 'o', 或者'u')之一开始。如果是这样,那么就在原始单词后面追加字符串 `"way"` 并返回结果。如果不是(即以辅音开头),则找到第一个元音的位置并将前面的部分移到单词末尾再附加 `"ay"` 为了处理整个句子而不是单独的单词,可以扩展上述逻辑如下所示: ```python import re def sentence_to_pig_latin(sentence): words = re.findall(r'\b\w+\b', sentence) # 使用正则表达式分割成单词列表 translated_words = [] for word in words: translated_word = pig_latin(word) translated_words.append(translated_word) result_sentence = ' '.join(translated_words) return result_sentence.capitalize() ``` 这段代码会遍历传入的 `sentence` 中每一个单词,并调用之前定义好的 `pig_latin()` 函数来进行翻译。最后把所有的翻译后的单词重新组合起来形成一个新的句子,并确保首字母大写。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值