Home > Enterprise >  Recursive function that takes a string as argument and determines if the string has more vowels than
Recursive function that takes a string as argument and determines if the string has more vowels than

Time:06-17

Why doesn't this work?:

def recursiveVowels(str):
    vowels = ['a','e','i','o','u']
    vowels_count =0
    if len(str) == 0:
        return vowels_count
    elif str[0] in vowels:
        vowels_count  = 1

    total_vowels =   vowels_count   recursiveVowels(str[1:])
    # check if no of vowels are more than half the length of string
    return total_vowels > (len(str)/2)

this will return False.

print(recursiveVowels("Targeiout"))

but if I do this it works fine:

def recursiveVowels(str):
    vowels = ['a','e','i','o','u']
    vowels_count =0
    if len(str) == 0:
        return vowels_count
    elif str[0] in vowels:
        vowels_count  = 1

    total_vowels =   vowels_count   recursiveVowels(str[1:])
    return total_vowels

# made a seperate function just to check if vowels are more than consonants
def isVowelsMore(str):
    vowels = recursiveVowels(str)
    return (len(str)/2) < vowels

this will return True:

print(isVowelsMore("Targeiout"))

Need some conceptual clarity.

CodePudding user response:

your first recursion returns boolean so when you are calling this line

total_vowels =   vowels_count   recursiveVowels(str[1:])

what actually happens is

 total_vowels =   vowels_count   False

or

total_vowels =   vowels_count   True

and that means that total_vowels value is at most vowels_count 1 (in case recursiveVowels(str[1:]) returned True), which means its at most 2 so your function will return false for any string longer than 4

hope I could help, feel free to ask for any clarification in the comments :)

CodePudding user response:

In your first example, you're returning a boolean. When used with greater than or less than, False is interpreted to 0, True is interpreted to 1. (See the documentation on Numeric Types for more about how Booleans are a subtype of integers).

So your first recursive function is adding 1 or 0 based on ... total_vowels > (len(str)/2), which is clearly not what you intend.

I'd suggest using the second version with the wrapper function.

CodePudding user response:

I agree with others that the second version of your function is the way to go, but I would write it like:

def isVowelsMore(string):
    vowels = {'a', 'e', 'i', 'o', 'u'}

    def recursiveVowels(string, index=0):
        vowels_count = 0

        if index >= len(string):
            return vowels_count

        if string[index] in vowels:
            vowels_count  = 1

        return vowels_count   recursiveVowels(string, index   1)

    vowels = recursiveVowels(string)

    return len(string) / 2 < vowels

The index variable was added to avoid creating a new string on every recursive call -- you can continue doing that and still use the above framework. I also switched your list of vowels to a set for improved performance. And I renamed your str variable as that's a Python builtin name which shouldn't be redefined.

  • Related