zoukankan      html  css  js  c++  java
  • Python学习---字符串处理

    This world is but a canvas to our imagination.

    世界只是我们的想象的画布。 ----Apri 22

    '''
    题目内容:
    “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
    '''

    def is_vowel(c):
        ###判断是元音字符
        return c in ['a','e','i','o','u']
    
    #print(is_vowel('y'))
    ###输入
    string = input()
    ###初步处理
    string = string.lower()
    string = string.strip()
    words = string.split()
    ###临时存储列表
    change_string = []
    for word in words:
        first = word[0]
        if is_vowel(first):
            ###首字母是元音
            word = word[:] + "hay"
        else:
            ###首字母不是元音
            if word[:2] =="qu":
                ###前两字是“qu”
                word = word[2:] + "quay"
            else:
                for i in range(1,len(word)):
                    ###对每个WORD,遍历字符
                    if is_vowel(word[i]) or (word[i] =='y'):
                         break
                    else:
                        first = first + word[i]
                word = word[len(first):] + first + "ay"
        ###将改写的WORD 存入临时列表
            change_string.append(word)
    for word in change_string:
        print(word,end=" ")
    print()
    
  • 相关阅读:
    AcWing 1027. 方格取数 dp
    AcWing 1014. 登山 dp
    acwing 482. 合唱队形 dp
    LeetCode 1463. 摘樱桃II dp
    LeetCode 100. 相同的树 树的遍历
    LeetCode 336. 回文对 哈希
    LeetCode 815. 公交路线 最短路 哈希
    算法问题实战策略 DARPA大挑战 二分
    算法问题实战策略 LUNCHBOX 贪心
    AcWing 1100. 抓住那头牛 BFS
  • 原文地址:https://www.cnblogs.com/whatiwhere/p/8906259.html
Copyright © 2011-2022 走看看