Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
14-liner: 2 loops solution in Clear category for Web Log Sessions by przemyslaw.daniel
def checkio(sessions):
data, out = dict(), []
for i in sessions.split():
pattern = r'(.+?);;(.+?);;https?://(.*\.)?(.+?\..+?)/'
time, user, _, site = __import__('re').search(pattern, i.lower()+'/').groups()
time = __import__('datetime').datetime.strptime(time, '%Y-%m-%d-%H-%M-%S')
data[(user, site)] = data[(user, site)]+[time] if (user, site) in data else [time]
for time, (user, site) in [(data[x], x) for x in data]:
duration, count = 1, 1
for delta in [(y-x).total_seconds() for x, y in zip(time, time[1:])]:
out += [(user, site, int(duration), count)] if delta > 30*60 else []
(count, duration) = (1, 1) if delta > 30*60 else (count+1, duration+delta)
out += [(user, site, int(duration), count)] if duration > 0 else []
return '\n'.join(["%s;;%s;;%s;;%s" % x for x in sorted(out)])
Feb. 28, 2017
Comments: