【问题标题】:How to capitalize vowels that are reversed?如何大写颠倒的元音?
【发布时间】:2019-12-05 02:27:28
【问题描述】:

处理一个要求我们创建一个函数,其中字符串中的所有元音都被反转的家庭作业问题。例如:This Is So Fun 将返回 Thus Os Si Fin。只是无法弄清楚如何让函数检测大写字母的位置,将它们转换为小写字母,反之亦然。现在函数输出Thus os SI Fin

def f(word):
    vowels = "aeiouAEIOU"
    string = list(word)
    i = 0
    j = len(word)-1

    while i < j:
        if string[i].lower() not in vowels:
            i += 1
        elif string[j].lower() not in vowels:
            j -= 1
        else:
            string[i], string[j] = string[j], string[i]
            i += 1
            j -= 1

    return "".join(string)

【问题讨论】:

  • 所以在“This Is So Fun”的例子中,元音是 (i,I,o,u)。不想触摸它周围的任何字母,但我希望它们以相反的顺序放回(u,o,I,i)。问题是我不知道如何把它们放回去,所以它们在需要的地方是大写的,在需要的地方是小写的。
  • 一个简单的解决方案是检查最后的字符串并检测是否有任何元音紧跟在空格后面。如果是,则转换为大写,如果不是,则转换为小写。

标签: python function for-loop


【解决方案1】:

如果你创建一个小函数,它需要两个字符并返回每个字符的大小写,你可以简单地将你的任务包装在里面:

def swapCase(c1, c2):
    return  (
        c1.upper() if c2.isupper() else c1.lower(), 
        c2.upper() if c1.isupper() else c2.lower()
    )


def f(word):
    vowels = "aeiouAEIOU"
    string = list(word)
    i = 0
    j = len(word)-1

    while i < j:
        if string[i].lower() not in vowels:
            i += 1
        elif string[j].lower() not in vowels:
            j -= 1
        else:
            string[i], string[j] = swapCase(string[j], string[i])
            i += 1
            j -= 1

    return "".join(string)

f("This Is So Fun")
# 'Thus Os Si Fin'

【讨论】:

    【解决方案2】:

    一种方法是检查您要替换的两个字母。如果它们的大小写相同,则无需执行任何操作,但如果它们的大小写不同,则在切换字母之前先切换它们的大小写。

    我不确定您是否需要它,但这种方法比@Madivad 的答案更灵活一些,因为它允许您在字符串中的任何位置传递带有大写字母的字符串,例如This Is SoO FunIE -> Thes Is SuO FonII

    一种简单的方法是使用str.isupper() 方法检查大小写,然后使用 if/else 大小写进行反转

    if string[i].isupper() != string[j].isupper():
      if string[i].isupper():
         string[j], string[i] = string[j].upper(), string[i].lower()
      else:
         string[j], string[i] = string[j].lower(), string[i].upper()
    

    最后,您可以将此 sn-p 它合并到您的代码中,就在您的字符串替换上方:

    def f(word):
        vowels = "aeiouAEIOU"
        string = list(word)
        i = 0
        j = len(word)-1
    
        while i < j:
            if string[i].lower() not in vowels:
                i += 1
            elif string[j].lower() not in vowels:
                j -= 1
            else:
                if string[i].isupper() != string[j].isupper():
                    if string[i].isupper():
                        string[j], string[i] = string[j].upper(), string[i].lower()
                    else:
                        string[j], string[i] = string[j].lower(), string[i].upper()
                string[i], string[j] = string[j], string[i]
                i += 1
                j -= 1
    
        return "".join(string)
    '''
    

    【讨论】:

      【解决方案3】:

      试试下面的方法:

      思路如下:

      • 首先将元音列表获取到一个列表中
      • 接下来,尝试互换它们
      • 现在,遍历输入并更改元音,同时检查输入中元音的原始大小写

      注意:我没有对所有情况进行完整的测试。您可能必须自己检查和处理。

          def f(word):
      
              vowels = "aeiouAEIOU"
              vowels_in_word = [x for x in word if x in vowels]
              word_list = list(word)
              print("Vowels in word in order = %s" %(vowels_in_word))
      
              i = 0
              j = len(vowels_in_word)-1
              while i <= j:
                  vowels_in_word[i], vowels_in_word[j] = vowels_in_word[j], vowels_in_word[i]
                  i += 1
                  j -= 1
      
              print("Changed Vowels = %s" %(vowels_in_word))
      
              i = 0
              j = 0
              for x in word_list:
                  if x in vowels:
                      if x.islower():
                          word_list[i] = vowels_in_word[j].lower()
                      else:
                          word_list[i] = vowels_in_word[j].upper()
      
                      j += 1
      
                  i += 1
      
              result_str = ''.join(x for x in word_list)
              print("Result Str = %s" %(result_str))
      
          f("This Is So Fun")
      

      输出:

      Vowels in word in order = ['i', 'I', 'o', 'u']
      Changed Vowels = ['u', 'o', 'I', 'i']
      Result Str = Thus Os Si Fin
      

      【讨论】:

        【解决方案4】:

        我会/确实如何处理它

        • 通过,抓住元音
        • 重复字符串,使用反转元音列表重建它
        • 插入元音时,检查该位置原始字符的大小写

        编辑:既然每个人都在提供解决方案,我想我也会提供解决方案。

        这就是我实现相同目标的方式:

        1. 遍历字符串并抓住元音
        2. 再次遍历字符串,换掉颠倒的元音

          str="This Is So Fun"
          print(str)
          vowels=[]
          for letter in str:
              if letter.lower() in "aeiou":
                  vowels.append(letter.lower())
          
          out = ""
          for letter in str:
              if letter.lower() in "aeiou":
                  if letter.isupper():
                      out += vowels.pop().upper()
                  else:
                      out += vowels.pop().lower()
              else:
                  out += letter
          
          print(out)
          

        【讨论】:

        • 这个问题是当我尝试在单词开头没有大写字母的其他字符串上使用它时,如果也会影响它们,我不希望它。
        • 我的问题措辞错误,我不希望每个单词都带有大写字母的字符串受到影响。例如,给定字符串“Hello world”,我希望它返回“Hollo werld”
        • 是的,在第二次迭代中,检查字符串字符的‘is upper’值,并将替换的字符放到那个位置,复制原来的大小写。查看修改后的代码
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-01
        • 2021-06-10
        相关资源
        最近更新 更多