HappyQuest
HappyQuest

Reputation: 27

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

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.

Upvotes: 0

Views: 699

Answers (3)

cdlane
cdlane

Reputation: 41870

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.

Upvotes: 0

Justin P. Williams
Justin P. Williams

Reputation: 1

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.

Upvotes: 0

Ohad Sharet
Ohad Sharet

Reputation: 1142

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 :)

Upvotes: 1

Related Questions