Compute the maximum number of simultaneous sessions

Screen Link:

My Code:

import datetime as dt

# Write your solution below

import csv

file = open("session_log.csv")
reader = csv.reader(file)
lists = list(reader)[1:]

#Calculate maximum number of sessions
max_sessions = 0
for row in lists:
    row=[int(x) for x in row]
    start = dt.datetime(row[1],row[2], row[3], row[4], row[5])
    end = dt.datetime(row[6], row[7], row[8], row[9], row[10])
    for row1 in lists:
        row1=[int(x) for x in row1]
        start1 = dt.datetime(row1[1],row1[2], row1[3], row1[4], row1[5])
        end1 = dt.datetime(row1[6], row1[7], row1[8], row1[9], row1[10])
        if start1 >= start and end1 <= end:
            max_sessions =+ 1


What I expected to happen:
a number corresponding to max_sessions

What actually happened:

My code run has timed out.

Can someone please tell me where I did wrong? Thank you.

Alternative approach

I am sharing my code as at first, I struggled to understand the new class initiation in the provided answer. I hope this will help those who may feel the same. Cheers

import datetime as dt

# Write your solution below
from csv import reader
f = open('datasets/session_log.csv', 'r')
data = list(reader(f))[1:]
# initiate a list of list of each event, start is True and end is False
max_sessions = None
events = []
for row in data:
    row = [int(x) for x in row]
    start_event = dt.datetime(row[1], row[2], row[3], row[4], row[5])
    end_event = dt.datetime(row[6], row[7], row[8], row[9], row[10])
    events.append([start_event, True])
    events.append([end_event, False])
# sort events by date and time they accoured 
events = sorted(events)
count = 0
maximum = 0
# itterate ove list of events( start and ends) 
for event in events:
    if event[1]:
        count += 1
    elif event[1] == False:
        count -= 1
    if count > maximum:
        maximum = count

Hi @Donglai:

Have you tried these troubleshooting steps? This is a known bug in the community.

1 Like

Hi @Donglai,

Your problem is at the nested for-loop here:

Though your code should work, your lab times-out because your code results in an infinite loop. The reason is this, You have 9689 logs (rows) in server_log.csv and you are looping through all of them. In each loop, you are then looping through the same number of logs again meaning you are looping through all 9689 rows 9689 times. This gives you a total of 93876721 individual loops for the block for-loops.

If we include the inline second for-loop that converts each cell in the row to integers, this one:

We’ll end up increasing the loops by a multiple of 11, meaning 93876721 * 11, which gives over a billion loops by my estimate. That is way too many loops (could be considered as infinite) and that’s why the lab keeps timing out.

@reza_45 solution works and is more efficient because it reduces the number of loops to just 29067 (which is 3*9689). This is because you have two separate for-loops, not nested but distinct, with the first, this one:

looping through the original data to convert it to datetime and separate the start from end times only once. The second, this one:

loops, just once, though an ordered list of start and end times (each start and end time have unique rows) for all events in our original data giving us twice the number of rows as we initially had and a total of three times the initial number of rows. This is how we arrived at 3*9689 loops which gives 29067 loops, a much smaller number than the first one of over a billion. I hope this was helpful to you.

1 Like

Hi adewalade and reza_45,

Thanks a lot for your help. This is very helpful.