Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

Contextual compression in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a simple function that compresses text by removing vowels.

Prompt Engineering / GenAI
def compress_text(text):
    vowels = 'aeiouAEIOU'
    return ''.join(char for char in text if [1])
Drag options to blanks, or click blank then click option'
Achar.isdigit()
Bchar not in vowels
Cchar in vowels
Dchar == ' '
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'char in vowels' will keep vowels instead of removing them.
2fill in blank
medium

Complete the code to calculate the compression ratio of original and compressed text lengths.

Prompt Engineering / GenAI
def compression_ratio(original, compressed):
    return len(compressed) / [1]
Drag options to blanks, or click blank then click option'
Alen(original) - len(compressed)
Blen(compressed)
Clen(original) + len(compressed)
Dlen(original)
Attempts:
3 left
💡 Hint
Common Mistakes
Dividing by compressed length instead of original length.
3fill in blank
hard

Fix the error in the function that compresses text by replacing spaces with underscores.

Prompt Engineering / GenAI
def compress_spaces(text):
    return text.replace(' ', [1])
Drag options to blanks, or click blank then click option'
A' '
B'-'
C'_'
D''
Attempts:
3 left
💡 Hint
Common Mistakes
Using empty string '' removes spaces instead of replacing them.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their compressed forms by removing vowels.

Prompt Engineering / GenAI
compressed_dict = {word: ''.join(char for char in word if [1]) for word in words if [2]
Drag options to blanks, or click blank then click option'
Achar not in 'aeiouAEIOU'
Bchar in 'aeiouAEIOU'
Clen(word) > 3
Dlen(word) < 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'char in vowels' keeps vowels instead of removing them.
Filtering words shorter than 3 instead of longer.
5fill in blank
hard

Fill all three blanks to create a function that compresses text by removing vowels, replacing spaces with underscores, and calculating compression ratio.

Prompt Engineering / GenAI
def full_compress(text):
    no_vowels = ''.join(char for char in text if [1])
    replaced = no_vowels.replace(' ', [2])
    ratio = len(replaced) / [3]
    return replaced, ratio
Drag options to blanks, or click blank then click option'
Achar not in 'aeiouAEIOU'
B'_'
Clen(text)
D' '
Attempts:
3 left
💡 Hint
Common Mistakes
Replacing spaces with spaces instead of underscores.
Dividing by compressed length instead of original length.

Practice

(1/5)
1. What is the main goal of contextual compression in AI?
easy
A. Keep only the most important information to save space and time
B. Increase the size of the data for better accuracy
C. Remove all data except the first sentence
D. Add random noise to the data to improve learning

Solution

  1. Step 1: Understand the purpose of contextual compression

    Contextual compression aims to reduce data size by keeping only key information.
  2. Step 2: Compare options with this purpose

    Only Keep only the most important information to save space and time matches this goal by saving space and time through important info retention.
  3. Final Answer:

    Keep only the most important information to save space and time -> Option A
  4. Quick Check:

    Contextual compression = Keep important info [OK]
Hint: Remember: compression means keeping key info, not deleting all [OK]
Common Mistakes:
  • Thinking compression means deleting everything
  • Confusing compression with data expansion
  • Assuming random data removal improves results
2. Which of the following is the correct way to describe a simple contextual compression method?
easy
A. Remove all punctuation from the text
B. Select key sentences and remove less useful details
C. Translate text into another language
D. Add extra words to make text longer

Solution

  1. Step 1: Identify what simple contextual compression does

    It selects important parts and removes less useful details to reduce size.
  2. Step 2: Match options to this description

    Select key sentences and remove less useful details correctly describes selecting key sentences and removing less useful details.
  3. Final Answer:

    Select key sentences and remove less useful details -> Option B
  4. Quick Check:

    Simple compression = select key parts [OK]
Hint: Focus on keeping key parts, not random removal [OK]
Common Mistakes:
  • Confusing compression with translation
  • Thinking punctuation removal equals compression
  • Adding words instead of removing
3. Given the following text: 'The cat sat on the mat. It was sunny outside. The dog barked loudly.' Which compressed version best shows contextual compression?
medium
A. 'It was sunny outside. The dog barked loudly.'
B. 'The dog barked loudly.'
C. 'The cat sat on the mat. It was sunny outside. The dog barked loudly.'
D. 'The cat sat on the mat. The dog barked loudly.'

Solution

  1. Step 1: Identify key information in the text

    The cat sitting and the dog barking are key events; the weather is less important.
  2. Step 2: Choose the option that keeps key info and removes less useful details

    'The cat sat on the mat. The dog barked loudly.' keeps the cat and dog events, removing the less important weather sentence.
  3. Final Answer:

    'The cat sat on the mat. The dog barked loudly.' -> Option D
  4. Quick Check:

    Keep key events, drop less useful info = 'The cat sat on the mat. The dog barked loudly.' [OK]
Hint: Keep main events, drop side details [OK]
Common Mistakes:
  • Keeping all sentences without compression
  • Removing too much and losing key info
  • Choosing only one sentence when more is needed
4. You have a compression function that removes all sentences containing the word 'not'. The input is: 'I do not like rain. The sun is bright. It is not cold.' What is the output?
medium
A. '' (empty string)
B. 'I do not like rain. It is not cold.'
C. 'The sun is bright.'
D. 'I do not like rain. The sun is bright. It is not cold.'

Solution

  1. Step 1: Identify sentences containing 'not'

    Sentences 1 and 3 contain 'not' and should be removed.
  2. Step 2: Remove those sentences and keep the rest

    Only 'The sun is bright.' remains after removal.
  3. Final Answer:

    'The sun is bright.' -> Option C
  4. Quick Check:

    Remove 'not' sentences = 'The sun is bright.' [OK]
Hint: Remove sentences with 'not' only [OK]
Common Mistakes:
  • Keeping sentences with 'not'
  • Removing all sentences
  • Returning original text unchanged
5. You want to compress a conversation by keeping only sentences with keywords: ['urgent', 'meeting', 'deadline']. Given the conversation: 'We have a meeting tomorrow. The weather is nice. The deadline is next week. Let's grab lunch.' Which compressed output is correct?
hard
A. 'We have a meeting tomorrow. The deadline is next week.'
B. 'The weather is nice. Let's grab lunch.'
C. 'We have a meeting tomorrow. The weather is nice.'
D. 'Let's grab lunch. The deadline is next week.'

Solution

  1. Step 1: Identify sentences containing keywords

    Sentences with 'meeting' and 'deadline' are the first and third sentences.
  2. Step 2: Keep only those sentences and remove others

    Keep 'We have a meeting tomorrow.' and 'The deadline is next week.'
  3. Final Answer:

    'We have a meeting tomorrow. The deadline is next week.' -> Option A
  4. Quick Check:

    Keep keyword sentences = 'We have a meeting tomorrow. The deadline is next week.' [OK]
Hint: Keep sentences with keywords only [OK]
Common Mistakes:
  • Keeping sentences without keywords
  • Removing all sentences
  • Mixing unrelated sentences