Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Web Log Sessions by gyahun_dash
from datetime import datetime
class Session:
def __init__(self, start, user, site):
self.site, self.user, self.start = site, user, start
self.last, self.quantity = self.start, 1
def isfrom(self, other):
within = (self.start - other.last).total_seconds() <= 1800
return within and self.user == other.user and self.site == other.site
@property
def info(self):
duration = int((self.last - self.start).total_seconds()) + 1
return self.user, self.site, duration, self.quantity
def checkio(log):
stamps, names, urls = zip(*(req.split(';;') for req in log.split()))
datetimes = (datetime(*map(int, stamp.split('-'))) for stamp in stamps)
users = (name.lower() for name in names)
sites = ('.'.join(url.split('/')[2].split('.')[-2:]) for url in urls)
sessions = []
for session in map(Session, datetimes, users, sites):
past = next(filter(session.isfrom, sessions), None)
if not past: sessions.append(session)
else:
past.last = session.last
past.quantity += 1
sessions = sorted(sessions, key=lambda s: s.info)
return '\n'.join(';;'.join(map(str, s.info)) for s in sessions)
Nov. 2, 2014