Sound Track Iterator
AirbnbPhone screenMedium
A sound track is read in pieces. The class SoundTrack is provided in the starter code: track.read(x, buf) takes up to x characters from where the track last stopped, appends them to the list buf one character per element, and returns how many it appended. A track that has fewer than x characters left appends them all, so a result below x means the track is exhausted, and an exhausted track appends nothing and returns 0. For a track holding "abcde", read(3, buf) appends a, b, c and returns 3, and a following read(2, buf) appends d, e and returns 2.
Build SoundTrackIterator, which plays a list of tracks one after another as a single stream. Its constructor receives the tracks' contents as a list of strings, and the starter already wraps each one in a SoundTrack. Your code must get the characters only by calling read(x, buf) on those SoundTrack objects.
play(x) is provided: it calls your read(x, buf) with a new empty buf and returns [count, characters], the count read returned and the characters it appended.
Part 1: Read across tracks
Implement read(x, buf). It appends to buf, one character per element, at most x characters of the stream: the next characters, taken from the first track in the list that is not exhausted and continuing into the following tracks when a track runs out, in order. It returns how many characters it appended. That is fewer than x only when the stream has ended, and 0 once nothing is left. Each character is appended by exactly one call, so the next call carries on right after where this one stopped. Tracks can be empty.
- Example 1
- init
[["a","bcd","ef"]]- operations
[["play",[2]],["play",[1]],["play",[3]]]- Output
[[2,"ab"],[1,"c"],[3,"def"]]
Why: Two characters come from 'a' and 'bcd'. One more is the 'c' of 'bcd'. The last read asks for 3 and gets 'd' from the rest of 'bcd', then 'ef', so it returns 'def'.
Constraints
0 <= len(tracks) <= 1000- The total number of characters over all tracks is at most
10^5 1 <= len(operations) <= 10^40 <= x <= 10^5