Created
July 31, 2009 17:17
-
-
Save cdent/159332 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from time import time | |
starts = [ | |
1248637121, | |
1248818005, | |
1248819043, | |
1248819661, | |
1248820029, | |
1248862668, | |
1248976328, | |
1248989370, | |
1249036736, | |
] | |
stops = [ | |
1248817256, | |
1248817974, | |
1248819674, | |
1248819680, | |
1248819813, | |
1248819936, | |
1248819977, | |
1248820035, | |
1248820188, | |
1248873042, | |
1248978031, | |
1248992420, | |
1249036746, | |
] | |
def zip_to_type(starts, stops): | |
tuples = [] | |
for start in starts: | |
tuples.append(('start', start)) | |
for stop in stops: | |
tuples.append(('stop', stop)) | |
tuples.sort(lambda x, y: cmp(x[1],y[1])) | |
return tuples | |
def flush_spurious(tuples): | |
good = [] | |
last_seen = None | |
for flag, timestamp in tuples: | |
if not last_seen and flag == 'stop': # keep going until we hit a start | |
continue | |
if flag == last_seen: # Already saw one of these, skip it | |
continue | |
good.append((flag, timestamp)) | |
last_seen = flag | |
if good[-1][0] != 'stop': # if the last thing is a start, end it with now | |
good.append(('stop', time())) | |
return good | |
def totaller(tuples): | |
total = 0 | |
for i in range(len(tuples))[::2]: # take even indices of tuples | |
start = tuples[i][1] | |
stop = tuples[i+1][1] | |
total += (stop - start) | |
return total | |
def count_times(starts, stops): | |
return totaller(flush_spurious(zip_to_type(starts, stops))) | |
if __name__ == '__main__': | |
print "with stopper" | |
seconds = count_times(starts, stops) | |
print float(seconds)/(60*60), 'hours' | |
print "without stopper" | |
stops.pop() | |
seconds = count_times(starts, stops) | |
print float(seconds)/(60*60), 'hours' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment