Skip to content

Instantly share code, notes, and snippets.

@rodacato
Created October 12, 2010 00:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodacato/621479 to your computer and use it in GitHub Desktop.
Save rodacato/621479 to your computer and use it in GitHub Desktop.
=begin
Crowdint logical skills test.
=end
require 'rubygems'
require 'faster_csv'
@stats = {}
FasterCSV.foreach("events.csv", :col_sep =>"\t", :headers => :first_row) do |row|
user = row["user_id"]
date = row["when"].to_i
@stats.merge!( user => [{ :start => date, :last => date, :visits => [{:events_per_visit =>0, :visit_duration => 0}] }]) unless @stats.has_key?(user)
start_time = (date - @stats[user].last[:start] > 3600)
current_time = (date - @stats[user].last[:last] > 3600)
@stats[user].last[:visits].last[:events_per_visit] += 1 unless start_time || current_time
@stats[user].last[:visits].last[:visit_duration] = (date - @stats[user].last[:start]).abs
if start_time || current_time
@stats[user].last[:visits].last[:visit_duration] = (@stats[user].last[:last] - @stats[user].last[:start]).abs
@stats[user].last[:last] = @stats[user].last[:start] = date
@stats[user].last[:visits] << {:events_per_visit =>1, :visit_duration => 0}
end
@stats[user].last[:last] = date
end
pp @stats
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment