Maximum number of sessions occurring at the same time. Can you please give feedback?

Hello everyone,

screenlink: Learn data science with Python and R projects

Can someone please give feedback to me on why my code not giving the right answer? I know it’s a more complicated approach than the answer provided but I’d like to understand what is wrong with this logic.

Here is my logic:

There are 3 possible scenarios for 2 sessions to be active at the same time:
1- Another session can start before the first session.
2- Another session can start after the first session.
3- Another session can start at the same time with the first session.

If 1 is True, the end time of that other session must be after the start time of the first session for our sessions to be active at the same time.

If 2 is True, the start time of that other session must be before the first session’s end time for our sessions to be active at the same time.

If 3 is True no other conditions are necessary for our sessions to be active at the same time.

My Code:

import datetime as dt

# Write your solution below

session_logs = list(csv.reader(open('session_log.csv')))
session_logs = session_logs[1:]

session_start_end_times = []

for row in session_logs:
    row = [int(x) for x in row]
    session_start = dt.datetime(row[1], row[2], row[3], row[4], row[5])
    session_end = dt.datetime(row[6], row[7], row[8], row[9], row[10])
    session_start_end_times.append([session_start, session_end])
    
    
max_sessions = None



for session in session_start_end_times:
    
    current_sessions = 0

    
    for session_2 in session_start_end_times:
        if session_2[0] < session[0] and session_2[1] > session[0]:
            current_sessions += 1

        elif session_2[0] > session[0] and session_2[0] < session[1]:
            current_sessions += 1
            
        elif session_2[0] == session[0]:
            current_sessions += 1 

    if max_sessions == None or current_sessions > max_sessions:
        max_sessions = current_sessions
            
 # we counted the first session twice so we subtract that.        
max_sessions = max_sessions - 1   

What I expected to happen:

max_sessions to be equal to 19

What actually happened:

max_sessions is equal to 34

Hi @yigitsoykaan, can you please provide a link to the screen you’re working on? You have provided a lot of great info here (including your code, thank you!) but some context on your problem can only help others to be able to help you.

1 Like

Hey Mike!

Thank you for your reply. I just edited my post and added the screen link. I share it here as well.

Link: Maximum number of sessions occurring at the same time. Can you please give feedback?

1 Like

I understood my mistake…

Even though all 3 scenarios are correct for the session I’m comparing to, it does not mean they overlap between them as well. Therefore, I count some sessions not overlapping with all other sessions. Thats why my max_sessions are greater than the answer.

2 Likes

Nicely done @yigitsoykaan on solving it yourself! I just got back from a long walk with my wife through the park and was going to see if I could figure out your issue…but no need, you got it! :sunglasses:

1 Like