Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to filter out bad words from the text.
Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake'] text = 'This is a spam message.' filtered = [word for word in text.split() if [1] not in bad_words] print(filtered)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' instead of 'word' causes a type error.
Using 'bad_words' inside the condition is incorrect because it's the list, not the current word.
✗ Incorrect
We check each word in the text to see if it is NOT in the list of bad words. So the blank should be 'word'.
2fill in blank
mediumComplete the code to replace bad words with asterisks in the text.
Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake'] text = 'This is a scam message.' filtered_text = ' '.join([[1] if word in bad_words else word for word in text.split()]) print(filtered_text)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of the mask string causes no replacement.
Adding spaces inside the mask string causes formatting issues.
✗ Incorrect
We replace any bad word with '****' to mask it. So the blank should be the string '****'.
3fill in blank
hardFix the error in the code to correctly detect if the text contains any bad words.
Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake'] text = 'Beware of fake news.' contains_bad = any(word [1] bad_words for word in text.split()) print(contains_bad)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' compares the whole list to a word, which is incorrect.
Using 'not in' reverses the logic and gives wrong results.
✗ Incorrect
To check if any word is in the bad_words list, use 'in'.
4fill in blank
hardFill both blanks to create a dictionary counting occurrences of bad words in the text.
Prompt Engineering / GenAI
bad_words = ['spam', 'scam', 'fake'] text = 'spam spam scam fake fake fake' counts = [1](word for word in text.split() if word [2] bad_words) print(counts)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dict' instead of 'Counter' does not count occurrences automatically.
Using 'not in' reverses the filter and counts good words instead.
✗ Incorrect
We use Counter to count words, and check if word is in bad_words.
5fill in blank
hardFill all three blanks to create a function that filters and replaces bad words with a mask.
Prompt Engineering / GenAI
def filter_text(text, bad_words, mask): return ' '.join([[1] if word [2] bad_words else word for word in text.split()]) text = 'This is a fake scam message.' result = filter_text(text, ['spam', 'scam', 'fake'], [3]) print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'mask' causes no replacement.
Using 'not in' reverses the logic and replaces good words.
✗ Incorrect
Inside the function, replace words in bad_words with mask. The mask argument is passed as '****'.