优化初学者关于子字符串替换的python脚本



我正在辅导一个朋友用python,我自己并不擅长。任务是编写一个脚本,以颠倒一些编造的外星语言,在添加字母"p"后重复每个元音序列。一些例子:

tomato -> topomapatopo groovy->groopoovybeautiful -> beaupeautipifupul

目标是扭转这种情况。从groopoovy -> groovy.

由于它是荷兰语赋值,因此有一个例外:"ij"被视为元音。所以blijpij -> blij(我发现这使事情变得复杂了很多)

我的解决方案对我来说似乎很笨重,我对更好、更优雅的解决方案感兴趣。不幸的是,由于这是一门编程入门课程,因此基础知识是关键。

    word = input()
    vowels = ('a', 'e', 'i', 'o', 'u')
    position = 0
    solution = ""
    while position < len(word):
        if word[position] == 'p':       # obviously, search for the letter 'p'
            add = 1                     # keep track of the sub string size
            group = ""
            while True:                 # loop to get consecutive vowels
                if word[position + add] in vowels:
                    group += word[position + add]
                    if word[position + add] == 'i' and word[position + add + 1] == 'j':   # recognize the "ij"
                        group += 'j'
                        add += 1
                    add += 1
                else:
                    break
                if position+add == len(word):       # stay within the bounds of the string
                    break
            add -= 1
            if word[position - add:position].lower() == group.lower() and group != "":
                position += add
            else:
                solution += 'p'
        else:
            solution += word[position]
        position += 1
    print(solution)

对于一个介绍性的 Python 类,这个怎么样。顶部有几个示例词;只需更改评论#。

我不是在每一步都检查"p",而是检查元音序列的开头。 此序列将始终以"p"结尾。 这是您不想将字符附加到解决方案的唯一情况;相反,您希望跳到元音序列的末尾。

"

ij"是元音的事实不会产生特殊情况,因为"i"开始元音序列。

word = "poopoo-poopoo"
# word = "poopooh"
# word = "hijipijinks"
# word = "apapepe"
# word = "copovfepefepe"
vowels = ('a', 'e', 'i', 'o', 'u')
position = 0
solution = ""
vowel_count = 0   # consecutive vowels
while position < len(word):
    c = word[position]
    if vowel_count > 0:
        if c == 'p':
            position += vowel_count + 1
            vowel_count = 0
            continue
        vowel_count += 1
    else:
        if c in vowels:
            vowel_count = 1
    solution += c
    position += len(c)
print(solution)
import re
input_text = "tomato"
encoded = re.sub('([aeiou]+)','\1p\1',input_text)
print(encoded)
decoded = re.sub('([aeiou]+)p\1','\1',encoded)
print(decoded)

应该这样做

最新更新