Last active
August 29, 2015 14:23
-
-
Save stewartpark/7e6296b98dedd05da13d to your computer and use it in GitHub Desktop.
Foo.bar zombit monitoring
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
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