Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using len instead of size causes incorrect slicing.
Using i instead of size results in wrong chunk lengths.
✗ Incorrect
The chunk size is controlled by the 'size' parameter, so we slice text using size.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
We compare the length of the current chunk plus the next word to max_len to avoid breaking words.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding overlap to size causes gaps instead of overlaps.
Using overlap alone moves too little, causing repeated chunks.
✗ Incorrect
To create overlapping chunks, we move the start by size minus overlap each time.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' or '//' causes type errors in slicing.
Using '-' results in negative slice indices.
✗ Incorrect
We use '+' to add chunk_size to i for slicing the chunk correctly.
5fill in blank
hardFill 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'
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.
✗ Incorrect
We slice chunks of length size, then move i by min_overlap if it is less than size, else by size.