使用递归计算字符串中的元音 [英] Counting vowels in a string using recursion

查看:484
本文介绍了使用递归计算字符串中的元音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解递归是一个函数调用自己,但我不知道如何确切地获得我的函数调用它自己获得所需的结果。我需要简单地计算给函数的字符串中的元音。

  def recVowelCount:
'使用递归计算返回元音中的元音数量'
vowelcount = 0
vowels =aEiou.lower()
如果元音中的s [0]:
vowelcount + = 1
else:






我想出了这个,

  def recVowelCount:
'返回s中元音的数量递归计算'
vowels =aeiouAEOU
if s ==:
return 0
elif s [0] in vowels:
return 1 + recVowelCount s [1:])
else:
return 0 + recVowelCount(s [1:])


<尝试这个,它是一个简单的解决方案:

  def recVowelCount (s):
如果不是s:
返回0
返回(1如果'aeiouAEIOU'中的s [0]否则为0)+ recVowelCount(s [1:])

它考虑到元音为大写或小写时的情况。它可能不是递归地遍历字符串的最有效的方法(因为每个递归调用创建了一个新的切片字符串),但是很容易理解:




    <
  • 递归步骤:如果第一个字符是元音,向解中添加1,否则添加0.任何一种方式



第二步将最终将字符串减少到第一个字符,然后删除第一个字符并继续遍历字符串的其余部分。零长度,因此结束递归。或者,可以使用尾递归执行相同的过程,而不是对性能,因为CPython未实施尾递归消除。 p>

  def recVowelCount:
def loop(s,acc):
如果不是s:
return acc
返回循环(s [1:],(1如果'aeiouAEIOU'的s [0]为0)+ acc)
循环(s,0)

只是为了有趣,如果我们删除解决方案必须递归的限制,这是我将如何解决它: / p>

  def iterVowelCount(s):
vowels = frozenset('aeiouAEIOU')
return sum c in s if c in vowels)

无论如何工作:

  recVowelCount('murcielago')
> 5

iterVowelCount('murcielago')
> 5


I understand that recursion is when a function calls itself, however I can't figure out how exactly to get my function to call it self to get the desired results. I need to simply count the vowels in the string given to the function.

def recVowelCount(s):
    'return the number of vowels in s using a recursive computation'
    vowelcount = 0
    vowels = "aEiou".lower()
    if s[0] in vowels:
        vowelcount += 1
    else:
        ???


I came up with this in the end, thanks to some insight from here.

def recVowelCount(s):
'return the number of vowels in s using a recursive computation'
vowels = "aeiouAEIOU"
if s == "":
    return 0
elif s[0] in vowels:
    return 1 + recVowelCount(s[1:])
else:
    return 0 + recVowelCount(s[1:])

解决方案

Try this, it's a simple solution:

def recVowelCount(s):
    if not s:
        return 0
    return (1 if s[0] in 'aeiouAEIOU' else 0) + recVowelCount(s[1:])

It takes into account the case when the vowels are in either uppercase or lowercase. It might not be the most efficient way to traverse recursively a string (because each recursive call creates a new sliced string) but it's easy to understand:

  • Base case: if the string is empty, then it has zero vowels.
  • Recursive step: if the first character is a vowel add 1 to the solution, otherwise add 0. Either way, advance the recursion by removing the first character and continue traversing the rest of the string.

The second step will eventually reduce the string to zero length, therefore ending the recursion. Alternatively, the same procedure can be implemented using tail recursion - not that it makes any difference regarding performance, given that CPython doesn't implement tail recursion elimination.

def recVowelCount(s):
    def loop(s, acc):
        if not s:
            return acc
        return loop(s[1:], (1 if s[0] in 'aeiouAEIOU' else 0) + acc)
    loop(s, 0)

Just for fun, if we remove the restriction that the solution has to be recursive, this is how I'd solve it:

def iterVowelCount(s):
    vowels = frozenset('aeiouAEIOU')
    return sum(1 for c in s if c in vowels)

Anyway this works:

recVowelCount('murcielago')
> 5

iterVowelCount('murcielago')
> 5

这篇关于使用递归计算字符串中的元音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆