BasicsRAGChunkingOpenAIAnthropicGoogle
Problem
Implement fixed_char_chunks(text, chunk_size) that splits a string text into consecutive non-overlapping slices of length chunk_size (the final chunk may be shorter if len(text) is not divisible by chunk_size).
Why It Matters: Fixed-size chunking is the baseline document ingestion step in RAG pipelines before upgrading to overlapping or recursive semantic splitters.
Function Signature
def fixed_char_chunks(text: str, chunk_size: int) -> list[str]:
"""Split text into non-overlapping chunks of size chunk_size."""
Sample Input & Output
Example 1: Uneven Final Chunk
Input:
text = "abcdefgh", chunk_size = 3
Expected Output:
["abc", "def", "gh"]
Explanation: First two chunks have length 3; the remaining 2 characters form the final chunk 'gh'.
Example 2: Empty String & Oversized chunk_size
Input:
fixed_char_chunks("", 4) | fixed_char_chunks("hi", 10)
Expected Output:
[] | ["hi"]
Explanation: Empty text produces an empty list []; when chunk_size >= len(text), a single chunk is returned.
Constraints & Edge Cases
- Empty text returns
[]. chunk_size >= 1.chunk_sizeis guaranteed to be a positive integer (>= 1).- Order of characters across joined chunks must reconstruct
textexactly.
Constraints & Evaluation Criteria
- Empty text returns `[]`.
- `chunk_size >= 1`.
Hints
Learn the Concept First
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
'abcdefgh', chunk_size=3
Expected Output / Criteria
['abc', 'def', 'gh']