Merging history with same result

This program is trying to merge history which has only a timestamp.

Assumption:

  1. The history data are continuous.
  2. The latest data is just before current time.
  3. The timestamp is the starting time of the record.

Sample data:

1
2
3
4
5
6
7
8
9
history_list = [{"start_time": "0920", "result": 1},
{"start_time": "0940", "result": 2},
{"start_time": "1000", "result": 2},
{"start_time": "1020", "result": 2},
{"start_time": "1040", "result": 1},
{"start_time": "1100", "result": 3},
{"start_time": "1120", "result": 1},
{"start_time": "1140", "result": 1},
{"start_time": "1200", "result": 2},]

Sample Output:

1
2
3
4
5
6
Time: 0920-0940, Result: 1
Time: 0940-1040, Result: 2
Time: 1040-1100, Result: 1
Time: 1100-1120, Result: 3
Time: 1120-1200, Result: 1
Time: 1200-1300, Result: 2

Function to Merge history with same result:

1
2
3
4
5
6
7
8
9
10
11
def merge_history_with_same_result(history_list):
length = len(history_list)
c_index = 0
while (c_index < length-1):
e_index = c_index
while e_index < length-1 and history_list[c_index]["result"] == history_list[e_index+1]["result"]:
e_index = e_index + 1
history_list = history_list[:c_index+1] + history_list[e_index+1:]
length = length - e_index + c_index
c_index = c_index + 1
return history_list

Function to calculating the duration:

1
2
3
4
5
def cal_end_time(history_list, now):
for index in range(len(history_list)-1):
history_list[index]["end_time"] = history_list[index+1]["start_time"]
history_list[-1]["end_time"] = now
return history_list

Main program

1
2
3
4
history_list = merge_history_with_same_result(history_list)
history_list = cal_end_time(history_list, "1300")
for history in history_list:
print("Time: {}-{}, Result: {}".format(history["start_time"], history["end_time"], history["result"]))