Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
(O_O) I spent 3 days for it (O_O) solution in Speedy category for Count Chains by sytchov1
from typing import List, Tuple
from math import sqrt
def intersection(firstCircle: Tuple[int, int, int], secondCircle: Tuple[int, int, int]) -> bool:
return abs(firstCircle[2] - secondCircle[2]) < \
sqrt(abs(secondCircle[0] - firstCircle[0])**2 + abs(secondCircle[1] - firstCircle[1])**2) < \
firstCircle[2] + secondCircle[2]
def count_chains(circles: List[Tuple[int, int, int]]) -> int:
circleAmount = len(circles)
chained = [0 for c in range(len(circles))]
for i in range(len(circles) - 1):
for j in range(i + 1, len(circles), 1):
if intersection(circles[i], circles[j]):
if chained[j] == 1:
chained[i] = chained[i] or 1
else:
chained[j] = 1
return circleAmount - sum(chained)
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!")
Oct. 17, 2020