0
0
Prompt Engineering / GenAIml~10 mins

Content filtering in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Aword
Btext
Cbad_words
Dfiltered
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.
2fill in blank
medium

Complete 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'
A'****'
B'####'
C'****', word
D'**** '
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.
3fill in blank
hard

Fix 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'
Anot in
Bin
C==
D!=
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.
4fill in blank
hard

Fill 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'
ACounter
Bin
Cnot in
Ddict
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.
5fill in blank
hard

Fill 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'
Amask
Bin
C'****'
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'mask' causes no replacement.
Using 'not in' reverses the logic and replaces good words.