How to find the time to iterate over two array?

The function is passed a dictionary containing three lists with timestamps (time in seconds):

  • lesson – the beginning and end of the lesson
  • pupil – intervals of pupil presence
  • tutor – intervals of the teacher’s presence The intervals are arranged as follows – it is always a list of an even number of items. Even indices (starting from 0) are the time of entry to the lesson, and odd ones are the time of leaving the lesson.

How can I calculate the time when both student and teacher are present in class at the same time? That is, the time of their crossing

 dc = {
      'lesson': [1594663200, 1594666800],
      'pupil': [1594663340, 1594663389, 1594663390, 1594663395, 1594663396, 1594666472],
      'tutor': [1594663290, 1594663430, 1594663443, 1594666473]}

My solution, I could only calculate the total time spent by each

 lesson_times = dc['lesson']
    pupil_times = dc['pupil']
    tutor_times = dc['tutor']
    total_time_pupil = 0
    start_time_pupil = 0
    end_time_pupil = 0

    if len(pupil_times) > len(tutor_times):
        for index, times in pupil_times

    for index, time in enumerate(pupil_times):
        if (index % 2) == 0:
           start_time_pupil = time
        else:
            end_time_pupil = time

        if start_time_pupil != 0 and end_time_pupil !=0:
            total_time_pupil += (end_time_pupil - start_time_pupil)
            start_time_pupil = 0
            end_time_pupil = 0

    tutor_times = dc['tutor']
    total_time_tutor = 0
    start_time_tutor = 0
    end_time_tutor = 0
    for index, time in enumerate(tutor_times):
        if (index % 2) == 0:
            start_time_tutor = time
        else:
            end_time_tutor = time

        if start_time_tutor != 0 and end_time_tutor != 0:
            total_time_tutor += (end_time_tutor - start_time_tutor)
            start_time_tutor = 0
            end_time_tutor = 0

Answer

I had written this when you posted the first time. This solves your problem.

import time

dc = {
      'lesson': [1594663200, 1594666800],
      'pupil': [1594663340, 1594663389, 1594663390, 1594663395, 1594663396, 1594666472],
      'tutor': [1594663290, 1594663430, 1594663443, 1594666473]}

puptimes = dc['pupil'][:]
tuttimes = dc['tutor'][:]
pupil_in = False
tutor_in = False

last = 0
together = 0
while puptimes and tuttimes:
    # Pick the event to come next.
    if puptimes[0] < tuttimes[0]:
        evt = puptimes.pop(0)
        pupil_in = not pupil_in
    else:
        evt = tuttimes.pop(0)
        tutor_in = not tutor_in

    tc = time.ctime(evt)
    if pupil_in and tutor_in:
        print( tc, "Both are in the room." )
        last = evt
    else:
        if last:
            print( tc, "No longer both in, together time =", evt-last )
            together += evt-last
            last = 0
        if pupil_in:
            print( tc, "Pupil is in the room alone" )
        elif tutor_in:
            print( tc, "Tutor is in the room alone" )
        else:
            print( tc, "Room is empty" )

print( "Total time together:", together, "seconds" )

Output:

[[email protected]:~/src]$ python x.py
Mon Jul 13 11:01:30 2020 Tutor is in the room alone
Mon Jul 13 11:02:20 2020 Both are in the room.
Mon Jul 13 11:03:09 2020 No longer both in, together time = 49
Mon Jul 13 11:03:09 2020 Tutor is in the room alone
Mon Jul 13 11:03:10 2020 Both are in the room.
Mon Jul 13 11:03:15 2020 No longer both in, together time = 5
Mon Jul 13 11:03:15 2020 Tutor is in the room alone
Mon Jul 13 11:03:16 2020 Both are in the room.
Mon Jul 13 11:03:50 2020 No longer both in, together time = 34
Mon Jul 13 11:03:50 2020 Pupil is in the room alone
Mon Jul 13 11:04:03 2020 Both are in the room.
Mon Jul 13 11:54:32 2020 No longer both in, together time = 3029
Mon Jul 13 11:54:32 2020 Tutor is in the room alone
Total time together: 3117 seconds

[[email protected]:~/src]$ 

Leave a Reply

Your email address will not be published. Required fields are marked *