Question: Your task is to implement a function replace_once(t, d), that takes a text t and a replacement dictionary d, and returns the result of replacing
Your task is to implement a function replace_once(t, d), that takes a text t and a replacement dictionary d, and returns the result of replacing words according to the dictionary: if a word appears as a key, replace it with the dictionary value, and if a word does not appear in the dictionary, leave it alone.
Given a list of words, you can concatenate them together with spaces in between as follows:
word_list = "I love eating bananas".split()
print("Word list:", word_list)
back_together = " ".join(word_list)
print("Back together:", back_together)
# You can also join them in other ways, btw. Just DON'T do it for this homework.
print("You can also put commas:", ", ".join(word_list))
Here is where you can write your replace_once function.
def replace_once(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.
"""
# 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
