Skip to content

Instantly share code, notes, and snippets.

@stewartpark
Last active August 29, 2015 14:23
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 stewartpark/7e6296b98dedd05da13d to your computer and use it in GitHub Desktop.
Save stewartpark/7e6296b98dedd05da13d to your computer and use it in GitHub Desktop.
Foo.bar zombit monitoring
def intersect(P, i):
ps = []
for p in P:
if p[0] <= i[0] and p[1] >= i[0]:
ps.append(p)
elif p[0] <= i[1] and p[1] >= i[1]:
ps.append(p)
elif p[0] >= i[0] and p[1] <= i[1]:
ps.append(p)
return ps
def add(*ps):
n_min = None
n_max = None
for p in ps:
if n_min is None or n_min > p[0]:
n_min = p[0]
if n_max is None or n_max < p[1]:
n_max = p[1]
return [n_min, n_max]
def reduce(I):
P = []
for i in I:
ps = intersect(P, i)
if len(ps):
new_p = add(i, *ps)
for p in ps:
P.remove(p)
P.append(new_p)
else:
P.append(i)
return P
def answer(I):
P = reduce(I)
s = 0
for p in P:
s+= p[1] - p[0]
return s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment