Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Black Holes by Sim0000
from math import pi, sqrt, hypot, acos
def checkio(data):
# calculate area of data[i]
area = lambda i: pi * data[i][2]**2
# calculate area of intersection of data[i] and data[j]
def intersection(i, j):
x1, y1, r1 = data[i]
x2, y2, r2 = data[j]
d = distance[i, j]
if r1 + r2 <= d: return 0 # no intersection
s = ((2*d*r1)**2 - (d**2 + r1**2 - r2**2)**2) / 4
if s <= 0: return min(area(i), area(j)) # include other
th1 = (d**2 + r1**2 - r2**2) / (2*d*r1)
th2 = (d**2 + r2**2 - r1**2) / (2*d*r2)
return r1**2*acos(th1) + r2**2*acos(th2) - sqrt(s)
data = list(map(list, data))
while True:
distance = {(j, i): hypot(d2[0]-d1[0], d2[1]-d1[1])
for i, d1 in enumerate(data) for j, d2 in enumerate(data[:i])}
for i, j in sorted(distance, key=distance.get):
s1 = area(i)
s2 = area(j)
s = intersection(i, j)
if s1 > s2: i, j, s1, s2 = j, i, s2, s1
if s / s1 >= 0.55 and s2 / s1 >= 1.20: # found
data[j][2] = sqrt((s1 + s2) / pi) # new radius
del data[i]
break # escape for loop
else:
break # not found, escape while loop
return data
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
def checker(user_ans, right_ans):
if not isinstance(user_ans, (list, tuple)) or \
any(not isinstance(line, (list, tuple)) for line in user_ans):
return False
if any(not isinstance(x, (float, int)) for l in user_ans for x in l):
return False
precision = 0.01
return all(abs(right_coord - user_coord) <= precision
for right_line, user_line in zip(right_ans, user_ans)
for right_coord, user_coord in zip(right_line, user_line))
assert checker(checkio([(2, 4, 2), (3, 9, 3)]), [(2, 4, 2), (3, 9, 3)])
assert checker(checkio([(0, 0, 2), (-1, 0, 2)]), [(0, 0, 2), (-1, 0, 2)])
assert checker(checkio([(4, 3, 2), (2.5, 3.5, 1.4)]), [(4, 3, 2.44)])
assert checker(checkio([(3, 3, 3), (2, 2, 1), (3, 5, 1.5)]), [(3, 3, 3.5)])
Oct. 19, 2015