替换单词中的第一个元音而不是第二个元音,又名。语言

Replace first vowel in word but not second vowel, aka. lew language

所以我想要实现的是一个可以编码和解码德国有趣编码“Löffelsprache”的程序

它的工作原理如下: 每个元音都被替换为“元音本身”+短+“又是元音” 默认短是“lew”; 例如:你好 > helewellolewo

我用我的代码得到了它,但它很混乱而且不是最优雅的解决方案。

还有一些规定,“ear”不会翻译成“elewealewar”,而只能翻译成“e[=26” =]lewear.

所以如果后面有第二个元音,它会被忽略。

为了更好地理解,我找到了这个使用“b”作为缩写的在线生成器: https://de.toolpage.org/tool/bsprache

代码:

# Get user input
inputtextraw = input("What to en/decrypt?\n").lower()
# set the message to be translated (inputtext)
inputtext = inputtextraw
# set the short
shortraw = "lew"

# Set a placeholder so the following replace functions don't replace part of the previously placed short
short_placeholder = "@"


# Find out if it's already encoded or needs to be encoded
if "a" + shortraw + "a" in inputtext or "e" + shortraw + "e" in inputtext or "i" + shortraw + "i" in inputtext or "o" + shortraw + "o" in inputtext or "u" + shortraw + "u" in inputtext:
    # DECODE
    try:
        # Replace all shorts with the placeholder
        inputtext = inputtext.replace(shortraw, short_placeholder)
        # Decode
        inputtext = inputtext.replace("e" + short_placeholder + "e", "e")
        inputtext = inputtext.replace("a" + short_placeholder + "a", "a")
        inputtext = inputtext.replace("i" + short_placeholder + "i", "i")
        inputtext = inputtext.replace("o" + short_placeholder + "o", "o")
        inputtext = inputtext.replace("u" + short_placeholder + "u", "u")
        # Save result
        result = inputtext
    except:
        print("error")
    print('\n"' + inputtextraw + '" means:\n' + result + '\n')
else:
    # ENCODE
    try:
        # Encode to vowel + placeholder + vowel
        inputtext = inputtext.replace("e", "e" + short_placeholder + "e")
        inputtext = inputtext.replace("a", "a" + short_placeholder + "a")
        inputtext = inputtext.replace("i", "i" + short_placeholder + "i")
        inputtext = inputtext.replace("o", "o" + short_placeholder + "o")
        inputtext = inputtext.replace("u", "u" + short_placeholder + "u")
        # replace the placeholder with the actual short
        result = inputtext.replace(short_placeholder, shortraw)
    except:
        print("error")
    print('\n"' + inputtextraw + '" in ' + shortraw + ' code is:\n' + result + '\n')

不要使用 replace() 进行编码,只需遍历您的字符串并检查每个字符是否在字符/元音列表中。

因为您想跳过第二个元音的编码,所以在处理下一个字符之前必须检查最后一个字符是否已编码:

data = []

for c in inputtext:
    if c in vList and not data:
        data.append(c+shortraw+c)
    elif c in vList and shortraw not in data[-1]:
        data.append(c+shortraw+c)
    else:
        data.append(c)

newString = ''.join(data)

最后你必须join()生成的结果集到一个新的字符串。

例子

# Get user input
inputtextraw = input("What to en/decrypt?\n").lower()
# set the message to be translated (inputtext)
inputtext = inputtextraw
# set the short
shortraw = "lew"
# list of characters to check
vList = ['a','e','i','o','u']

def is_encoded(inputtext,shortraw):
    for c in vList: 
        if c+shortraw+c in inputtext:
            return True

    return False

def decode_string(inputtext,shortraw):
    for c in vList:
        inputtext = inputtext.replace(c+shortraw, '')
    return inputtext

def encode_string(inputtext,shortraw):
    data = []

    for c in inputtext:
        if c in vList and not data:
            data.append(c+shortraw+c)
        elif c in vList and shortraw not in data[-1]:
            data.append(c+shortraw+c)
        else:
            data.append(c)

    return ''.join(data)

if is_encoded(inputtext,shortraw):
    #decode
    print(f'String is encoded -> decoded string is {decode_string(inputtext,shortraw)}')
else:
    #encode
    print(f'String is not encoded\n"{inputtextraw}" in {shortraw} code is:\n {encode_string(inputtext,shortraw)} \n')