需要帮助用python编写Pig拉丁翻译代码吗

2024-06-14 08:00:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一项任务,我必须将英语单词翻译成拉丁语,这意味着如果一个单词以元音开头,那么这个单词的末尾会加上“ay”(“apple”会变成“appleay”),这不是问题,因为代码相对容易编写。
然而,第二部分是,如果单词以一个辅音开头,第一个元音之前的所有辅音都会被删除并添加到单词的末尾,后面还会添加字符串“ay”(“cheese”将变成“eesechay”)

这是一个相当简单的概念,但如果单词以辅音开头,我正在努力寻找翻译该单词的方法,以下是我目前的代码:

def pigLatin(word):
    for l in vowels:
        if word[0] == l:
            word = word + "ay"
    for L in consonants:
        if word[0] == L:
            for i in vowels:
                for s in word:
                    if s == i:
                        #this is where im completely lost

仅供参考,元音和辅音是仅包含元音和辅音的数组,单词由用户输入

编辑:

感谢您的帮助,我已经成功地重新编写了代码,并获得了一些有用的东西:

def pigLatin(word):
    if word[0]in vowels:
        word = word + "ay"
    elif word[0] in consonants:
        c = ""
        for l in word:
            if l in vowels:
                break
            elif l in consonants:
                c = c + l
        word = word[len(c)-len(word):len(word)]
        word = word + c + "ay"

再次感谢您的帮助:)


Tags: 代码inforlenifdef单词word
2条回答

我同意Charles Duffy的评论,我们不为您设计程序。然而,你下错了兔子洞,我想你需要一些指导。这是我刚才所说的一个例子。有很多方法可以做到这一点,这是一个简单的解决方案(其中之一)

def pigLatin(word):
    vowels = list("aeiou")
    consonants = list("bcdfghjklmnpqrstvwxyz")

    if word[0] in vowels:
        word = word + "ay"
    else:
        for counter, letter in enumerate(list(word)):
            if letter in vowels:
                word = word[counter:] + word[:counter] + "ay"
                break

    return word


print(pigLatin("art"))
print(pigLatin("donkey"))

如果输入到pigLatin中的单词包含大写字符怎么办?您可以通过将所有内容转换为小写(或大写,您的首选项)来修改函数

def pigLatin(word):
    vowels = list("aeiou")
    consonants = list("bcdfghjklmnpqrstvwxyz")

    if word[0].lower() in vowels:
        word = word + "ay"
    else:
        for counter, letter in enumerate(list(word)):
            if letter.lower() in vowels:
                word = word[counter:] + word[:counter] + "ay"
                break

    return word

你知道这段代码有多简单和灵活吗

以下几点可能会有所帮助:

string模块中的ascii_lowercase是一个预定义字符串,包含所有小写字母字符:

>>> from string import ascii_lowercase
>>> ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> 

我们可以通过创建一组元音并计算元音与所有字符之间的差异来生成所有辅音的集合:

from string import ascii_lowercase as alphabet

vowels = set("aeiou")
consonants = set(alphabet) ^ vowels

print(consonants)

输出:

{'c', 's', 'q', 'm', 'g', 'd', 'y', 'l', 'b', 'k', 't', 'j', 'r', 'p', 'h', 'v', 'n', 'w', 'z', 'f', 'x'}
>>> 

因为这是一个集合,所以没有内在的顺序,但这并不重要。如果我们想知道给定的字符是辅音还是元音,我们只需检查对应集合的成员身份(可以对列表执行相同的操作,但集合将是首选的数据结构)

无论是否为vowelsconsonants使用列表或集合,都可以通过简单地检查成员身份(检查字符是否在集合中)来简化代码:

if word[0] in vowels:
    # The first letter is a vowel
elif word[0] in consonants:
    # The first letter is a consonant

如果您事先知道word只包含小写字母(没有特殊符号、数字、大写字母等),则可以进一步简化:

if word[0] in vowels:
    # The first letter is a vowel
else:
    # If it's not a vowel, it must be a consonant

然而,如果你仔细想想,你根本不需要检查第一个字母是否是元音。您已经知道将在最后一个字符串的末尾添加"ay",不管第一个字母是元音还是辅音-因此,您只需要检查第一个字母是否是辅音

使用到目前为止的一切,我将得到以下伪代码:

def to_pig_latin(word):

    from string import ascii_lowercase as alphabet

    vowels = set("aeiou")
    consonants = set(alphabet) ^ vowels

    if word[0] in consonants:
        # Do something

    return ... + "ay"

我已经重命名了函数to_pig_latin,因为蛇壳是首选的,并且在函数名前面加上to表示您正在翻译/转换某些内容。我还将vowelsconsonants的创建移到了函数中,只是因为没有理由将它放在函数之外,这样做更有趣

相关问题 更多 >