CMU Coding Bootcamp
1from math import sqrt
2
3
4def distance(a: tuple[float, float], b: tuple[float, float]) -> float:
5 """Calculate the distance between two points using the Euclidean distance formula."""
6 return sqrt((b[0] - a[0]) ** 2 + (b[1] - a[1]) ** 2)
7
8
9def dotsOverlap(
10 x1: float, y1: float, r1: float, x2: float, y2: float, r2: float
11) -> bool:
12 """Check if two circles overlap."""
13 d = distance((x1, y1), (x2, y2))
14 return d <= (r1 + r2)
15
16
17print("Testing dotsOverlap()...")
18assert dotsOverlap(0, 0, 2, 3, 0, 2) == True
19assert dotsOverlap(0, 0, 2, 5, 0, 2) == False
20assert dotsOverlap(0, 0, 2, 4, 0, 2) == True
21assert dotsOverlap(-4, 5, 2, -3, 5, 5) == True
22assert dotsOverlap(3, 3, 3, 3, -3, 2.99) == False
23assert dotsOverlap(3, 3, 3, 3, -3, 3) == True
24assert dotsOverlap(5, 3, 0, 5, 3, 0) == True
25print("Passed!")