Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
integers only solution in Clear category for Count Chains by pokosasa
from typing import List, Tuple
def count_chains(circles: List[Tuple[int, int, int]]) -> int:
prev_groups=[{circles.pop()}]
while circles:
circle_1=circles.pop()
x1,y1,r1=circle_1
new_group={circle_1}
next_groups=[]
for group in prev_groups:
chained=False
for circle_2 in group:
x2,y2,r2=circle_2
if (r1-r2)**2<(x1-x2)**2+(y1-y2)**2<(r1+r2)**2:
new_group.update(group)
chained=True
break
if not chained:
next_groups.append(group)
next_groups.append(new_group)
prev_groups=next_groups
return len(prev_groups)
if __name__ == '__main__':
print("Example:")
print(count_chains([(1, 1, 1), (4, 2, 1), (4, 3, 1)]))
# These "asserts" are used for self-checking and not for an auto-testing
assert count_chains([(1, 1, 1), (4, 2, 1), (4, 3, 1)]) == 2, 'basic'
assert count_chains([(1, 1, 1), (2, 2, 1), (3, 3, 1)]) == 1, 'basic #2'
assert count_chains([(2, 2, 2), (4, 2, 2), (3, 4, 2)]) == 1, 'trinity'
assert count_chains([(2, 2, 1), (2, 2, 2)]) == 2, 'inclusion'
assert count_chains([(1, 1, 1), (1, 3, 1), (3, 1, 1), (3, 3, 1)]) == 4, 'adjacent'
assert count_chains([(0, 0, 1), (-1, 1, 1), (1, -1, 1), (-2, -2, 1)]) == 2, 'negative coordinates'
print("Coding complete? Click 'Check' to earn cool rewards!")
May 28, 2020