Untitled
unknown
plain_text
a year ago
3.5 kB
7
Indexable
Never
def sortLoginCounts(login_counts): sorted_counts = [] for username in sorted(login_counts.keys()): for login_date in sorted(login_counts[username].keys()): login_count = login_counts[username][login_date] sorted_counts.append([username, login_date, login_count]) return sorted_counts from collections import defaultdict def processLogs(logs): login_counts = defaultdict(lambda: defaultdict(int)) for log in logs: username, login_time, login_date = log # Check if the login_time is in the valid format (HH:MM:SS) if len(login_time) != 8 or login_time[2] != ':' or login_time[5] != ':': continue # Check if the login_date is in the valid format (YYYY-MM-DD) if len(login_date) != 10 or login_date[4] != '-' or login_date[7] != '-': continue # Increment the login count for the user on the given day login_counts[username][login_date] += 1 return login_counts def countUserLogins(logs): # Step 1: Process the logs and count logins login_counts = processLogs(logs) # Step 2: Sort the login counts sorted_counts = sortLoginCounts(login_counts) return sorted_counts # Test case logs = [ ["user1", "09:00:00", "2021-01-01"], ["user1", "13:00:00", "2021-01-01"], ["user1", "13:00:00", "2021-01-01"], # Duplicate entry ["user2", "14:00:00", "2021-01-01"], ["user1", "20:00:00", "2021-01-01"], ["user2", "21:00:00", "2021-01-01"], ["user3", "25:00:00", "2021-01-01"], # Invalid time ["user4", "22:00:00", "2021-02-29"], # Invalid date ] result = countUserLogins(logs) print(result)def sortLoginCounts(login_counts): sorted_counts = [] for username in sorted(login_counts.keys()): for login_date in sorted(login_counts[username].keys()): login_count = login_counts[username][login_date] sorted_counts.append([username, login_date, login_count]) return sorted_counts from collections import defaultdict def processLogs(logs): login_counts = defaultdict(lambda: defaultdict(int)) for log in logs: username, login_time, login_date = log # Check if the login_time is in the valid format (HH:MM:SS) if len(login_time) != 8 or login_time[2] != ':' or login_time[5] != ':': continue # Check if the login_date is in the valid format (YYYY-MM-DD) if len(login_date) != 10 or login_date[4] != '-' or login_date[7] != '-': continue # Increment the login count for the user on the given day login_counts[username][login_date] += 1 return login_counts def countUserLogins(logs): # Step 1: Process the logs and count logins login_counts = processLogs(logs) # Step 2: Sort the login counts sorted_counts = sortLoginCounts(login_counts) return sorted_counts # Test case logs = [ ["user1", "09:00:00", "2021-01-01"], ["user1", "13:00:00", "2021-01-01"], ["user1", "13:00:00", "2021-01-01"], # Duplicate entry ["user2", "14:00:00", "2021-01-01"], ["user1", "20:00:00", "2021-01-01"], ["user2", "21:00:00", "2021-01-01"], ["user3", "25:00:00", "2021-01-01"], # Invalid time ["user4", "22:00:00", "2021-02-29"], # Invalid date ] result = countUserLogins(logs) print(result)