0

文字列内の母音の数を見つけるには、再帰を使用する必要があります。したがって、hello入力された場合は、それを返したいと思い2ます。

私が抱えている問題は、文字列の次の文字に移動することです。

 def recVowelCount(i):
    count = 0
    if i in 'aeiou':
        count +=1
        reVowelCount(i)
    else:
        reVowelCount(i)
        return count
4

4 に答える 4

4

Here's one way to do it using recursion :)

def recVowelCount(i, chars_to_find='aeiou'):
    if not chars_to_find:
        return 0
    return i.count(chars_to_find[0]) + recVowelCount(i, chars_to_find[1:])

Now, the problem in your code is that

if i in 'aeiou':

would be asking if 'hello' in 'aeiou':, which isn't very useful. You need to check if i[0] in 'aeiou' where i[0] will be each letter of "hello" each time the function is called recursively

Start with the simple case. What happens if the input string is empty? You'd just return 0 right?

def recVowelCount(i):
    if not i:
        return 0

So we're half done. Now you need to think about what happens in the case the i isn't empty. If the first character is a vowel, we'll count 1 and then pass the rest of the string into the function recursively

def recVowelCount(i):
    if not i:
        return 0
    if i[0] in 'aeiou':
        count = 1
    else:
        count = 0
    return count + recVowelCount(i[1:])

ok.. that can be refactored a little

def recVowelCount(i):
    if not i:
        return 0
    count = 'aeiou'.count(i[0])
    return count + recVowelCount(i[1:])

and finally

def recVowelCount(i):
    if not i:
        return 0
    return 'aeiou'.count(i[0]) + recVowelCount(i[1:])
于 2012-10-08T04:30:28.493 に答える
2
def recVowelCount(s):
    ''' Return number of vowels in string s'''
    if len(s) == 0:
        return 0
    letter = s[0]
    if letter in 'aeiou':
        return 1 + recVowelCount(s[1:])
    return recVowelCount(s[1:])


print recVowelCount('hello')

There are 3 basic steps in any recursive program:

  1. base case
  2. you need to progress towards base case
  3. recursive call
于 2012-10-08T04:31:05.943 に答える
1

first of all its not clear what argument you are passing def countVowels(my_string): is probably a better way to start

next you need a base case

 if len(my_string) == 1:
    if my_string in "aeiou": return 1
    else:return 0

then you need your recursion

 elif my_string[0] in "aeiou":
     return 1 + countVowels(my_string[1:])
 else:
      return 0 + countVowels(my_string[1:])
于 2012-10-08T04:32:52.897 に答える
0
def find_vowels(word=None, count=0):
    if word:
        if word[0] in ('A','E','I','O','U','a','e','i','o','u'):
            count += 1
        return find_vowels(word=word[1:], count=count)
    else:
        return count

find_vowels('python is awesome')

find_vowels関数は2つのパラメーターを取ります-1つはword、ルックアップする実際の文字列です。もう1つはcount、母音の全出現数を含むです。の初期値countは0に設定されています。

空の場合word、関数はカウント値を返します。これはword、母音が完全にチェックされたときです。 if word:

次のブロックには、実際のロジックが含まれています。最初の文字は繰り返しチェックインされwordます。母音の場合、count引数がインクリメントされます。

return find_vowels(word=word[1:], count=count)再帰が発生する場所です。word=word[1:]チェックされているので、最初の文字をスライスします。

させてword ='Python'

wordこれは、後続の呼び出しでの値がどのように見えるかです。

Python-1回目の呼び出し-2回目の
ython 呼び出し
thon -3
hon 回目の呼び出し-4回目の呼び出し
on -5回目の呼び出し
n -6回目の呼び出し
-最後の呼び出し(空)

最後に、文字列が空の場合、countが返されます。

これは末尾再帰と呼ばれます:http://en.wikipedia.org/wiki/Tail_call

于 2012-10-08T04:47:50.513 に答える