Skip to content

Instantly share code, notes, and snippets.

@krrishd
Last active August 29, 2015 13:56
Show Gist options
  • Save krrishd/9004163 to your computer and use it in GitHub Desktop.
Save krrishd/9004163 to your computer and use it in GitHub Desktop.
# Assume connections are sorted by date
# Calculate score at each month in time since the year 2005.
# This means you have (2014-2005) * 12 + 1 = 109 months of data
# Assume connection array is your sorted list of connections by date
# This creates an array where the value of the index is corresponds to the number of months
# that has passed since Jan. 2005 (arbitrary date, not much social data before then)
# Algorithm is upper-bounded by O(i+j), but average runtime will be more like O(max(i, j))
var i = 0; # Keeps track of number of months that has passed since Jan. 2005
var j = 0; # Keeps track of place in connection array
var userDate = new Date()
var scoreArr = []; # Stores scores at points in time
# make sure you don't go over the connection limit
for(i; i < (userDate.getFullYear() - 2005) + userDate.getMonth(); i++) {
# Re-calculate new date
var year = 2005 + floor(i/12);
var month = i % 12;
# Get the first of every month
var date = getFullYear(year, month, 1);
for(j, j < connections.count; j++) {
connection = connections[j];
if(connection.connected_date < date) {
scoreArr.append(connection.score);
} else {
break;
}
}
# Quick if statement to break out of for loop if we've iterated through the entire connection array
if (j >= connections.count) {
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment