Command Palette

Search for a command to run...

Hectal
Phase 14: Interviews and Capstone
LLM Engineering · Exercise 2 of 5 PreviousNext
Advanced

Parse a stream that arrives in pieces

Write a class SseParser with a method feed(chunk) that returns a list of the events finished by this chunk. A chunk can end anywhere, even in the middle of a line.

Rules: a line starting with data: adds its value (without one optional leading space) to the current event. A blank line finishes the event (several data: lines are joined with a newline). Lines starting with : are comments and ignored. Strip a trailing \r from each line. An event that is not yet finished is not returned.

What Check tests

  1. Two events in one chunk
  2. A chunk that ends in the middle of a line
  3. The same stream one character at a time
  4. Comments are ignored and two data lines make one event
  5. An unfinished event is not returned

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.