 # Compute the maximum number of simultaneous sessions

Screen Link:

https://app.dataquest.io/m/1000353/working-with-dates-and-times-in-python-practice-problems/13/maximum-number-of-simultaneous-sessions

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,row, row, row, row)
end = dt.datetime(row, row, row, row, row)

for row1 in lists:
row1=[int(x) for x in row1]
start1 = dt.datetime(row1,row1, row1, row1, row1)
end1 = dt.datetime(row1, row1, row1, row1, row1)
if start1 >= start and end1 <= end:
max_sessions =+ 1

print(max_sessions)

``````

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:]
f.close()
# 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, row, row, row, row)
end_event = dt.datetime(row, row, row, row, row)
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:
count += 1
elif event == False:
count -= 1
if count > maximum:
maximum = count
print(maximum)
``````
3 Likes

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.