Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a list of events which is always sorted chronologically. The start time i

ID: 647831 • Letter: I

Question

I have a list of events which is always sorted chronologically. The start time is always followed by the end time. Times are strings formatted as 'HHmmss'.

// list of events

var events = [
    '010000', // start
    '013000', // end...
    '053000',
    '060000',
    '161500',
    '184500']

// desired output

var spares = [
    '000000', // start
    '010000', // end...
    '013000',
    '053000',
    '060000',
    '161500',
    '184500',
    '235959']
How can I programmatically create a new list of "spare time" from 000000 to 235959?

PS I'm trying to do this in Javascript, but any conceptual answer or pseudo code would be helpful too.

Explanation / Answer

If "spare time" is defined as the time in between the events, then your list format makes it very easy to convert you list of events to a list of spare times:

Add midnight to both the start and end of your list
Go over the new list and remove any interval of less than 0 seconds (or whatever threshold you want).
With your example events, this would give you after step 1:

000000
010000
013000
053000
060000
161500
184500
235959
and after step 2:

000000
010000
013000
053000
060000
161500
184500
235959