文字列の母音を反転します

"""
设计:Python程序设计
作者:初学者
日期:2022年 03月 26日
"""


# 例91  翻转字符串中的元音字母
#     1.问题描述
#         写一个方法,输入给定字符串,翻转字符串中的元音字母
#     2.问题示例
#         输入s="hello",输出"holle"。
#     3.代码实现
class Solution:
    """
    参数s:字符串
    返回:返回新的字符串
    """

    def vowel(self, a):
        vowel_list = set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'])
        rec = list(a)
        start, end = 0, len(rec) - 1
        while start <= end and rec[start] not in vowel_list:
            start += 1
        while start <= end and rec[end] not in vowel_list:
            end -= 1
        if start <= end:
            rec[start], rec[end] = rec[end], rec[start]
            start += 1
            end -= 1
        return ''.join(rec)


# 主函数
if __name__ == '__main__':
    s = Solution()
    a = "hello"

    print("输入:", a)
    print("输出:", s.vowel(a))

おすすめ

転載: blog.csdn.net/m0_56852291/article/details/123762368