This is my first post here on stackoverflow, I am working on a programming assignment for school and am working on the following problem which I am stuck on.
Write a program that reads in events from STDIN and outputs the events back to toSTDOUT with the “overlap” flag flipped for the events that overlap with others. The firstline of the input will be the number of events to follow, N. N will be 1 million or more.The subsequent N lines will contain events in the following format:
{ ‘start_time’: string format, ‘end_time’: string format, ‘overlap’: boolean represented as 1 or 0 }
Sample input:
{ ‘start_time’: “2016-01-01 00:00:00”, ‘end_time’: “2016-05-01 00:00:00”, ‘overlap’: 0}
{ ‘start_time’: “2016-02-01 00:00:00”, ‘end_time’: “2016-06-01 00:00:00”, ‘overlap’: 0}
{ ‘start_time’: “2012-01-01 00:00:00”, ‘end_time’: “2012-05-01 00:00:00”, ‘overlap’: 0}
Sample output:
{ ‘start_time’: “2016-01-01 00:00:00”, ‘end_time’: “2016-05-01 00:00:00”, ‘overlap’: 1}
{ ‘start_time’: “2016-02-01 00:00:00”, ‘end_time’: “2016-06-01 00:00:00”, ‘overlap’: 1}
{ ‘start_time’: “2012-01-01 00:00:00”, ‘end_time’: “2012-05-01 00:00:00”, ‘overlap’: 0}
The input would be given in a "timestamp.txt", which I will read using the python io library, I would then input the lines into an array and using either the sorted() method or the .sort() method to sort the lines by timestamp order, Once I have a sorted array, I would then compare each successive events end time and start time to see if there is an overlap with the end_time of the first event with the start_time of the second event.
What I am currently stuck on is how to actual retrieve the value from each line text.
Since the file comes in a format
{ ‘start_time’: “2016-01-01 00:00:00”, ‘end_time’: “2016-05-01 00:00:00”, ‘overlap’: 0}
It is not json format exactly, so I cannot do something like line = json.loads(line) and get the value by line['start_time'].
Anybody have any suggestion for this problem set? Thank you.