Samuel Anumudu
Samuel Anumudu

Reputation: 1

Python Count Odd and Even in String of Numbers Using Recursion

I am trying to count all the even and odd numbers in a string of numbers using RECURSION in my Python program but it keeps showing me this error: "TypeError: not all arguments converted during string formatting..." Please, can anyone help me out here?

I tried it in JS it worked perfectly...but not on python. I think I am doing something wrong.

BELOW IS MY CODE:

def count_even_odd_recursive(string):
    def helper(helper_input):
        odd = 0
        even = 0
        if len(helper_input) == 0:
            return
        if helper_input[0] % 2 == 0:
            even += 1

        elif helper_input[0] % 2 != 0:
            odd += 1

        helper(helper_input[1::])

        if even > odd:
            return f'There are more even numbers ({even}) that odd.'
        else:
            return f'There are more odd numbers ({odd}) that even.'

    helper(string)
print(count_even_odd_recursive("0987650"))

Upvotes: 0

Views: 1943

Answers (2)

Piturnah
Piturnah

Reputation: 626

There's a couple of problems, the first is that when you're checking whether the digit is even, you're trying to perform modulo operator on a string. You can fix this easily by casting using int().

Secondly, your variables even and odd are local, and you're not actually passing them into the recursive calls to helper. To be honest, I'm not sure why this needs to be recursive, but unfortunately in the code you provided there is no recursion happening. If you want it to be recursive, I've reworked it a bit, also adding in a case where the the even digits are the same count as the odd digits:

def helper(helper_input):
    
    if len(helper_input) == 0:
        return 0
    
    if int(helper_input[0]) % 2 == 0:
        return helper(helper_input[1:]) + 1

    return helper(helper_input[1:]) + 0

def count_even_odd_recursive(string):
    even = 0
    if len(string) == 0:
        return "No input provided"
    else:
        even = helper(string)
        
        if even > len(string) / 2:
            return f'There are more even numbers ({even}) than odd.'
        elif even == len(string) / 2:
            return f'There are more odd numbers ({len(string)-even}) than even.'
        else:
            return "Equal digits odd and even."
    
print(count_even_odd_recursive("0987650"))

Upvotes: 1

Captain Trojan
Captain Trojan

Reputation: 2920

This should fix your error:

def count_even_odd_recursive(string):
    def helper(helper_input):
        if len(helper_input) == 0:
            return 0, 0
            
        odd, even = helper(helper_input[1:])

        if int(helper_input[0]) % 2 == 0:
            even += 1
        else:
            odd += 1  

        return odd, even
            
    odd, even = helper(string)
    
    if even > odd:
        return f'There are more even numbers ({even}) that odd.'
    else:
        return f'There are more odd numbers ({odd}) or the same as even numbers.'
print(count_even_odd_recursive("0987650"))

# There are more even numbers (4) that odd.

I fixed your recursion also, not gonna lie, I had no idea how you intended the recursion to happen there, but this is recursive and it works.

Upvotes: 1

Related Questions