Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE CODE IN PYTHON Write a recursive function count upper lower() that takes

ID: 3828572 • Letter: P

Question

PLEASE CODE IN PYTHON

Write 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). To solve this problem you will need to write a function that returns a pair of values (i.e., a tuple). For example, suppose we wanted to write a function that returned the sum and product of two values passed as arguments to the function. We might write this code: def sum_prod(a, b): return a+b, a*b Here is an example of how we might call the function and access the return values. s, p = sum_prod(3, 6) print(s,p) In this example, the two return values will be assigned to s and p accordingly based on their position in the tuple. In other words, a+b will be assigned to s, and a*b will be assigned to p.

Examples:

Function Call : Return Value

count upper lower(’Stony Brook University’) (3, 17)

count upper lower(’HAPPINESS’) (9, 0)

count upper lower(’no more labs, yay!’) (0, 13)

count upper lower(’&@*:!@88??’) (0, 0)

Explanation / Answer

def count_upper_lower(string):
if len(string) == 0:
return (0, 0)
  
if string[0].isalpha():
if string[0].isupper():
(upper, lower) = count_upper_lower(string[1:])
return (upper + 1, lower)
else:
(upper, lower) = count_upper_lower(string[1:])
return (upper, lower+1)
else:
return count_upper_lower(string[1:])

print(count_upper_lower('Stony Brook University'))
print(count_upper_lower('HAPPINESS'))
print(count_upper_lower('no more labs, yay!'))
print(count_upper_lower('&@*:!@88??'))

# code link: https://paste.ee/p/nXIX7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote