Question

Write a recursive function named get_first_capital(word) that takes a string as a parameter and returns the...

Write a recursive function named get_first_capital(word) that takes a string as a parameter and returns the first capital letter that exists in a string using recursion. This function has to be recursive; you are not allowed to use loops to solve this problem.

Test:                                                 Except:
print(get_first_capital('helLo'))                     L
s = 'great'             
print(get_first_capital(s))                           None

And

Write a recursive function named count_uppercase(my_string) that finds the number of uppercase letters in the parameter string. Note: This function has to be recursive; you are not allowed to use loops to solve this problem!

You may want to use a helper function to do the counting. The helper function header is:

def count_uppercase_helper(a_string, index):

Test: Except:

print(count_uppercase('This is a Test'))               2

Homework Answers

Answer #1
def get_first_capital(word):
    if(len(word)==0):
        return None
    else:
        if(word[0].isupper()):
            return word[0]
        else:
            return get_first_capital(word[1:])

# Testing
print(get_first_capital('helLo'))
s = 'great'
print(get_first_capital(s))

======================================

def count_uppercase(my_string):
    if (len(my_string) == 0):
        return 0
    else:
        if (my_string[0].isupper()):
            return 1 + count_uppercase(my_string[1:])
        else:
            return count_uppercase(my_string[1:])

# Testing
print(count_uppercase('This is a Test'))
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "i" replaced with the character "n python
##1.Write a function named shout. The function should accept a string argument ##and display it in...
##1.Write a function named shout. The function should accept a string argument ##and display it in uppercase with an exclamation mark concatenated to the end. ## ##2. Examine the following function header, then write a statement that calls ##the function, passing 12 as an argument. ##def show_value(quantity): ##3. Look at the following function header: ##def my_function(a, b, c): ##Now look at the following call to my_function: ##my_function(3, 2, 1) ##When this call executes, what value will be assigned to a?...
#Write a function called has_a_vowel. has_a_vowel should have #one parameter, a string. It should return True...
#Write a function called has_a_vowel. has_a_vowel should have #one parameter, a string. It should return True if the string #has any vowels in it, False if it does not. def has_a_vowel(a_str): print("Starting...") for letter in a_str: print("Checking", letter) if letter in "aeiou": print(letter, "is a vowel, returning True") return True else: print(letter, "is not a vowel, returning False") return False print("Done!")       #Below are some lines of code that will test your function. #You can change the value of...
In JAVA Write a RECURSIVE method that receives as a parameter an integer named n. The...
In JAVA Write a RECURSIVE method that receives as a parameter an integer named n. The method will output n # of lines of stars. For example, the first line will have one star, the second line will have two stars, and so on. The line number n will have "n" number of ****** (stars) so if n is 3 it would print * ** *** The method must not have any loops!
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
Write a recursive method that takes as input a string s and returns a list containing...
Write a recursive method that takes as input a string s and returns a list containing all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For example, the word ‘binary’ is an anagram of ‘brainy’. Note that the output list should not contain duplicates. Java
C++ Write a recursive function that reverses the given input string. No loops allowed, only use...
C++ Write a recursive function that reverses the given input string. No loops allowed, only use recursive functions. Do not add more or change the parameters to the original function. Do not change the main program. I had asked this before but the solution I was given did not work. #include #include using namespace std; void reverse(string &str) { /*Code needed*/ } int main() {    string name = "sammy";    reverse(name);    cout << name << endl; //should display...
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list...
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '("abc" "ab" "a")) should evaluate to '("ab" "a") because these are the only strings shorter than 3. Your solution must not be recursive. You will...
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Write a function called score that meets the specifications below. def score(word, f): """ word, a...
Write a function called score that meets the specifications below. def score(word, f): """ word, a string of length > 1 of alphabetical characters (upper and lowercase) f, a function that takes in two int arguments and returns an int Returns the score of word as defined by the method: 1) Score for each letter is its location in the alphabet (a=1 ... z=26) times its distance from start of word. Ex. the scores for the letters in 'adD' are...