Question: Let's go back now to the simpler version of the problem, where we replace in case-sensitive way. Our original version of the code applies the
Let's go back now to the simpler version of the problem, where we replace in case-sensitive way. Our original version of the code applies the replacement only once. That is, if the dictionary d maps:
d = {"cats": "dogs", "dogs": "pigs"} and our text is t = "I love cats", the function replace_once(t, d) applies the replacement only once, and we obtain
"I love dogs"
Now, you should write a function replace_chain, that keeps applying replacements until no more replacements can be applied, so that replace_chain(t, d) yields:
"I love pigs"
def replace_chain(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,
repeating the replacements until no replacement can be applied.
"""
# YOUR CODE HERE
raise NotImplementedError()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
