I am trying to create a recursive function that, given a string, will return a list [consonants, vowels, other]. The problem I have is that once the base case is called and a list is returned, the function repeats one more time and gives me a list with values double of what was expected (ex: countChars("a") will return [0, 2, 0] not [0, 1, 0]) Is there something I'm missing?
def countChars(s, list=[0, 0, 0]):
vowels = ["a", "e", "i", "o", "u"]
if len(s) < 1:
return list
if s[0] in vowels:
list[1] += 1
elif s[0].isalpha():
list[0] += 1
else:
list[2] += 1
return countChars(s[1:], list)