Question: Write a function replace_case_insensitive(t, d) that applies the replacement irrespective of whether the original word is capitalized or not, and that preserves the capitalization: a

Write a function replace_case_insensitive(t, d) that applies the replacement irrespective of whether the original word is capitalized or not, and that preserves the capitalization: a capitalized word is replaced with another capitalized word. For example, if

d = {"dogs": "cats"}

then replace_case_insensitive("Dogs are nice", d) is:

"Cats are nice"

and replace_case_insensitive("I like dogs", d) is:

"I like cats"

When writing your solution, you can assume that both keys and values in the replacement dictionary appear in lowercase. Note also that you only need to concern yourselves with words that are either all in lowercase, like "dog", or capitalized, like "Dog"; you don't have to worry about words like "DOG" or "DoG".

To write your solution, note that you can capitalize a word w via:

w.capitalize()

and you can test whether a word w is capitalized via:

w.capitalize() == w

Also, given a word w, you can turn it into lowercase via:

w.lower()

You can write your solution below.

def replace_case_insensitive(t, d):

"""

@param t: a string

@param d: a dictionary, mapping words to their replacements

@returns: a string, where words in d have been replaced according to the dictionary mapping,

in case insensitive way, and preserving capitalization.

"""

# YOUR CODE HERE

raise NotImplementedError()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!