Your question is Utility Function With Edge Cases. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
Descript stores transcript pieces as timed segments. Implement a utility that validates these segments, orders them by start time, and merges consecutive segments from the same speaker when the gap between them is no greater than max_gap.
Implement merge_transcript_segments(segments, max_gap). segments is a list of dictionaries with these keys:
start: non-negative number representing the start time in secondsend: number greater than or equal to startspeaker: non-empty stringtext: stringReturn a new list of dictionaries in ascending start order. When two mergeable segments are combined, use the earliest start, the latest end, the same speaker, and join non-empty text values with one space. Segments merge when they have the same speaker and next.start <= current.end + max_gap.
Raise TypeError if segments is not a list, max_gap is not numeric, or a segment is not a dictionary with valid field types. Raise ValueError if max_gap is negative, a timestamp is negative, start > end, or speaker is empty. An empty input returns an empty list. Do not mutate the input list or its dictionaries.
def merge_transcript_segments(segments, max_gap):