Define the alter_the_list() function which is passed a string of text and a list
ID: 3872436 • Letter: D
Question
Define the alter_the_list() function which is passed a string of text and a list of words as parameters. The function removes any word from the parameter list of words which is a separate word in the string of text. The string of text should be converted to lower case before you do any checking as the elements of the parameter list of words are all in lower case. Note that there is no punctuation in the parameter string of text. Note that each word in the parameter list of word is unique, i.e., it occurs only once. For example, the following code:
Test
Expected
Explanation / Answer
def alter_the_list(s,w):
s = s.lower()
list1 = s.split();
for i in range(len(list1)):
for j in range(len(w)):
if w[j] == list1[i]:
w.remove(w[j])
break
return w
word_list = ["a", "is", "bus", "on", "the"]
alter_the_list("A bus station is where a bus stops A train station is where a train stops On my desk I have a work station", word_list)
print("1.", word_list)
word_list = ["a", 'up', "you", "it", "on", "the", 'is']
alter_the_list("It is up to YOU", word_list)
print("2.", word_list)
word_list = ["easy", "come", "go"]
alter_the_list("Easy come easy go go go", word_list)
print("3.", word_list)
word_list = ["a", "is", "i", "on"]
alter_the_list("", word_list)
print("4.", word_list)
word_list = ["a", "is", "i", "on", "the"]
alter_the_list("May your coffee be strong and your Monday be short", word_list)
print("5.", word_list)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.