Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to store a generated response in the cache dictionary.
Prompt Engineering / GenAI
cache[[1]] = generated_response Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the response as the key instead of the prompt.
Using a variable not defined as the key.
✗ Incorrect
The prompt is used as the key to store the generated response in the cache.
2fill in blank
mediumComplete the code to check if the prompt is already cached before generating a new response.
Prompt Engineering / GenAI
if [1] in cache: return cache[prompt]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for the response instead of the prompt.
Using an undefined variable in the condition.
✗ Incorrect
We check if the prompt exists in the cache to avoid redundant generation.
3fill in blank
hardFix the error in the code to retrieve a cached response safely.
Prompt Engineering / GenAI
cached_response = cache.get([1], None)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the response as the key in .get().
Using an undefined variable as the key.
✗ Incorrect
The prompt is the key used to get the cached response safely with .get().
4fill in blank
hardFill both blanks to create a cache dictionary comprehension that stores prompt-response pairs only if the response length is greater than 10.
Prompt Engineering / GenAI
filtered_cache = {prompt: response for prompt, response in cache.items() if len(response) [1] [2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using a smaller number like 5 instead of 10.
✗ Incorrect
We filter cache items where the response length is greater than 10.
5fill in blank
hardFill all three blanks to implement a function that returns a cached response if available, otherwise generates, caches, and returns a new response.
Prompt Engineering / GenAI
def get_response(prompt): if prompt [1] cache: return cache[prompt] response = [2](prompt) cache[[3]] = response return response
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'not in' instead of 'in' in the if condition.
Using wrong variable names for keys or functions.
✗ Incorrect
We check if prompt is in cache, generate response if not, store it with prompt as key, then return it.