0
0
Prompt Engineering / GenAIml~10 mins

Text chunking strategies 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 split text into chunks of fixed size.

Prompt Engineering / GenAI
def chunk_text(text, size):
    return [text[i:i+[1]] for i in range(0, len(text), size)]
Drag options to blanks, or click blank then click option'
Alen
Btext
Csize
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using len instead of size causes incorrect slicing.
Using i instead of size results in wrong chunk lengths.
2fill in blank
medium

Complete the code to split text into chunks without breaking words.

Prompt Engineering / GenAI
def chunk_text_words(text, max_len):
    words = text.split()
    chunks = []
    current = ''
    for word in words:
        if len(current) + len(word) + 1 > [1]:
            chunks.append(current.strip())
            current = word + ' '
        else:
            current += word + ' '
    if current:
        chunks.append(current.strip())
    return chunks
Drag options to blanks, or click blank then click option'
Amax_len
Blen(word)
Clen(current)
Dtext
Attempts:
3 left
💡 Hint
Common Mistakes
Using len(current) instead of max_len causes wrong chunk size checks.
Comparing to len(word) alone does not limit chunk length.
3fill in blank
hard

Fix the error in the code to create overlapping text chunks.

Prompt Engineering / GenAI
def overlapping_chunks(text, size, overlap):
    chunks = []
    start = 0
    while start < len(text):
        end = start + size
        chunks.append(text[start:end])
        start += [1]
    return chunks
Drag options to blanks, or click blank then click option'
Asize
Bsize - overlap
Coverlap
Dsize + overlap
Attempts:
3 left
💡 Hint
Common Mistakes
Adding overlap to size causes gaps instead of overlaps.
Using overlap alone moves too little, causing repeated chunks.
4fill in blank
hard

Fill both blanks to create a dictionary of chunk indices and their text.

Prompt Engineering / GenAI
def chunk_dict(text, chunk_size):
    return {i: text[i[1]i+[2]] for i in range(0, len(text), chunk_size)}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '//' causes type errors in slicing.
Using '-' results in negative slice indices.
5fill in blank
hard

Fill all three blanks to create a list of chunks with a minimum overlap.

Prompt Engineering / GenAI
def min_overlap_chunks(text, size, min_overlap):
    chunks = []
    i = 0
    while i < len(text):
        chunk = text[i:i+[1]]
        chunks.append(chunk)
        i += [2] if [3] < size else size
    return chunks
Drag options to blanks, or click blank then click option'
Asize
Bmin_overlap
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of size for chunk length causes wrong slices.
Not comparing min_overlap to size causes infinite loops.