Vivek Pandey's Blog
Apr 16, 2014
Nov 17, 2012
An exercise in simplification
Problem
There is a file which contains pairs of user ids and timestamps. Each line contains one pair. user ids are sorted in ascending order, and for each user id, timestamps are sorted in ascending order. Here is an example file:
user1 2012-09-18-14:30:29 user1 2012-09-18-14:30:55 user1 2012-09-18-14:31:12 user1 2012-09-18-15:31:12 user1 2012-09-18-15:31:12 user1 2012-09-18-16:31:12 user2 2012-09-19-14:30:29 user2 2012-09-19-14:30:55 user2 2012-09-19-14:31:12 user2 2012-09-19-15:31:12 user2 2012-09-19-15:31:13 user2 2012-09-19-15:31:16For each user, we need to output various session lengths. A session is defined as a sequence of consecutive visits so that no two visits are more than 30 minutes apart. We need not output the user ids.
For instance, in above example, user1 has three sessions: consisting of first three visits, next two visits and the next visit.
Output for above file would be:
3, 2, 1 3, 3First cut
Initial thought is to
- Scan through the file checking when the user id changes
- For a given user id, keep checking if next timestamp is more than 30 minutes after the previous one.
from datetime import datetime, timedelta
NEW = 'new'
def are_close(t1, t2):
fmt = '%Y-%m-%d-%H:%M:%S'
diff = datetime.strptime(t2, fmt) - datetime.strptime(t1, fmt)
return diff < timedelta(minutes = 30)
def write_details(sessions):
print ', '.join([str(len(session)) for session in sessions])
def write_one_id_details(viz_id, page_views):
if viz_id == NEW:
return
if len(page_views) == 0:
return
sessions = []
session = [page_views[0]]
if len(page_views) == 1:
sessions.append(session)
write_details(session)
return
for i in range(1, len(page_views)):
if are_close(page_views[i - 1], page_views[i]):
session.append(page_views[i])
else:
sessions.append(session)
session = [page_views[i]]
sessions.append(session)
write_details(sessions)
def process():
f = open('visits.txt')
curr_viz_id = NEW
page_views = []
for line in f:
viz_id, time = line.strip().split('\t')
if viz_id != curr_viz_id:
write_one_id_details(curr_viz_id, page_views)
curr_viz_id = viz_id
page_views = []
page_views.append(time)
write_one_id_details(curr_viz_id, page_views)
f.close()
if __name__ == '__main__':
process()
While this works, there are several problems with this code:- We have a variable called NEW to boostrap the process
- In write_one_id_details(), we need to initialize sessions to [page_views[0]]. This is hindrance in achieving harmony.
- When a loop ranges from 1 to n - 1, I do not like it. Loops should range from 0< to n - 1.
- sessions = [] or page_views = [] are indications of procedural programming. I would rather want easy to understand declarative style
Mar 30, 2009
Just Chill Chill
I just heard the song Just Chill Chill Just Chill.
I think I am hearing it for the first time in India.
I heard it last in Cbhatt's Dabba!
When once I got lost on 237 (when I was a new driver),
this was the song playing as I was getting frustrated
over losing my way and having low gas in Cbhatt's
dabba! I tried initially to get motivated by the song,
but soon gave up and shut up the song.
http://www.youtube.com/watch?v=1g_6TmOyXAM
Just Chill Chill,
--Pandey
Mar 19, 2009
Impossible triangles
In Macroeconomics, there is an impossible triangle: You cannot simultaneously control
- Interest rate
- Inflation rate
- Exchange rate
- Number of hidden page faults
- Context switch cost
- (I forgot what it was.)
Oct 17, 2008
Cypress Talk
Today, there was pre placement talk by Cypress Semiconductors. The person who presented (his name was Pashupathy, I think) was a VP at Cypress. He had worked around 14 years at Cypress in San Jose and then returned to India to start the Chennai office.
He seemed very excited about his job. He told us that Chennai office was not just a BPO/KPO type of thing. It handled things that were important for Cypress. He mostly seemed to handle investments, M&A etc. for Cypress.
This looks like a nice company to work for. However, given my background, if I decide to go to a Tech company, I would rather go for yahoo/google/microsoft etc. which are software companies.
It is a complicated tradeoff to make between earning a lot of money and having a relatively cool life. (Work is interesting in any case, I believe)
May 17, 2008
अजीब दुनिया
ये दुनिया अजीब है:
1. अगर आप बैंक से पैसे लेने जाएं, तो अगर आप की credit history अच्छी है तो आपको कम ब्याज पर पैसे मिल जायेंगे, और अगर ख़राब है तो आपको ज्यादा ब्याज देना पड़ेगा। मतलब ये कि गरीब आदमी ज्यादा ब्याज देगा और अमीर को सस्ते में उधार मिलेगा। वाह!
2. चलो आपने credit history देखकर ब्याज की दर निर्धारित कर ली। ब्याज की दर निर्धारित करते समय आपने ये ध्यान रखा था कि कुछ लोग default कर जायेंगे। मतलब ये कि जो लोग default करेंगे उनको जो पैसे देने चाहिए थे, वो आपने उन लोगों से ले लिए जिन्होंने ने ईमानदारी से पैसे दिए। मतलब ईमानदार लोग बेईमान (या बदकिस्मत) लोगों की भरपाई करें। अगर आप पैसे लेकर भाग गए टू भाग गए, मगर अगर आपने वापस किए तो जो भागे हैं, उनकी भी भरपाई करें. वाह!
कुल मिला कर शायद system का optimal configuration यही है, मगर थोड़ा अजीब लगता है.




