PIXELBANKv9.1.0
Menu

Split text into overlapping chunks for RAG.

Given a text (list of words), split it into chunks of size chunk_size with overlap_size words overlapping between consecutive chunks.

Input:

  • Line 1: chunk_size overlap_size
  • Line 2: text (space-separated words)

Output: One chunk per line (space-separated words).

Example:

Input:
4 2
a b c d e f g h
Output:
a b c d
c d e f
e f g h
Reasoning:
  • The input values are chunk_size = 4 and overlap_size = 2, which means each chunk will have 4 words and overlap with the next chunk by 2 words.
  • The text is split into chunks of size 4, starting from the beginning: a b c d is the first chunk.
  • The next chunk starts overlap_size = 2 words after the beginning of the previous chunk, so it starts with c d and includes the next 2 words e f, resulting in c d e f.
  • This process continues, with the next chunk starting with e f (the overlap) and including the next 2 words g h, resulting in e f g h.

Constraints:

  • chunk_size > overlap_size >= 0
  • Last chunk may be smaller than chunk_size
  • Don't create empty chunks
solution.py

Test Results

0/0
Run code to see test results.
Chunk Text with Overlap - Easy | PixelBank