kennedy kolute
kennedy kolute

Reputation: 23

count upper and lowercase letters in python using recursion

I need help coming up with this python function. A recursive function count_upper_lower() that takes a non-empty string as its argument and returns a tuple containing the counts of how many letters in the string are uppercase and how many are lowercase (in that order). For instance

print (count_upper_lower(’Town Hall University’)) 

would return

(3, 15)

this is what i have so far

def count_upper_lower(word):
    upper = 0
    lower = 0
    if word == "":
        upper = 0
        lower = 0
        return upper, lower
    elif word[0].isupper():
        upper = 1 + count_upper_lower(word[1:])
        return upper , lower


    elif word[0].islower():
        lower = 1 + count_upper_lower(word[1:])
        return upper , lower

    else:
        upper = 0 + count_upper_lower(word[1:])
        lower = 0 + count_upper_lower(word[1:])
        return upper, lower

i am getting the following error:

TypeError: unsupported operand type(s) for +: 'int' and 'tuple'

Upvotes: 0

Views: 5004

Answers (5)

J. Goedhart
J. Goedhart

Reputation: 209

If you want to do it with two lines of code:

lst = [1 if letter.islower() else 0 for letter in word if letter.islower() or letter.isupper()]
print (lst.count(0),lst.count(1))

Upvotes: 1

Matthias Fischer
Matthias Fischer

Reputation: 553

As pointed out, you're trying to add ints to tuples, which is why you are getting the error message. Consider the following example instead:

def count_upper_lower(word):
    if not word:
        return 0, 0
    else:
        upper, lower = count_upper_lower(word[1:])
        if word[0].isupper():
            return upper+1, lower
        elif word[0].islower():
            return upper, lower+1
        else:
            # make sure that this is what you want
            return upper, lower

Here, the function recursively calls itself until the string is used up. The else case catches the case that the character is neither an upper nor a lower character (e.g. a digit), in which case neither counter is increased.

Upvotes: 1

Maurice Meyer
Maurice Meyer

Reputation: 18116

In your else block, you are trying to add the returning tuple from count_upper_lower() to a number.

class myCounter():
    def __init__(self):
        self.lower = 0
        self.upper = 0

    def count(self, s):
        if not s:
            return (self.upper, self.lower)

        firstCharacter = s[0]

        if firstCharacter.islower():
            self.lower += 1
        elif firstCharacter.isupper():
            self.upper += 1
        else:
            pass #do nothing !

        return self.count(s[1:])


c = myCounter()
print(c.count("Town Hall University"))
>>> (3, 15)

Upvotes: 0

VeGABAU
VeGABAU

Reputation: 158

There is a problem with using tuple as it's an immutable data structure. Used list instead.

def count_letters(phrase):
    if phrase:
        letter = phrase[0]
        result = count_letters(phrase[1:])
        if letter.isupper():
            result[0] += 1
        elif letter.islower():
            result[1] += 1
        return result
    else:
        return [0, 0]


>>> count_letters("LaLaa")
[2, 3]

Upvotes: 0

J. Goedhart
J. Goedhart

Reputation: 209

I would use a loop and ascii. In the ascii table 65 untill 91 is a upper case and 97 untill 123 is a lower case.

def count_upper_lower(word):
    upper = 0
    lower = 0
    for letter in word: #runs through all the letter if empty nothing happens
       if 65 <= ord(letter) <= 90:
           upper += 1
       elif 97 <= ord(letter) <= 122:
           lower += 1
    return upper,lower

This is also possible.

def count_upper_lower(word):
    upper = 0
    lower = 0
    for letter in word: #runs through all the letter if empty nothing happens
       if letter.isupper():
           upper += 1
       elif letter.islower():
           lower += 1
    return upper,lower

Upvotes: 1

Related Questions