asked 150k views
1 vote
Two circles on a cartesian plane hackerrank solution

asked
User Fstennet
by
7.3k points

1 Answer

4 votes

To solve this problem, we need to compare the positions and sizes of two circles, A and B, based on their given descriptors (X, Y, and R). Here's a Python implementation of the circles function that determines the relationship between the circles and returns an array of strings:

  • The function takes an array of strings, info, where each string represents the descriptors for two circles. It then iterates over each string in info and performs the necessary calculations and comparisons to determine the relationship between the circles.
  • def circles(info):
  • result = []
  • for i in info:
  • # Parse the descriptors for circle A and circle B
  • xA, yA, rA, xB, yB, rB = map(int, i.split())
  • # Calculate the distance between the centers of the circles
  • distance = ((xA - xB) ** 2 + (yA - yB) ** 2) ** 0.5
  • # Check the relationship between the circles based on their positions and sizes
  • if distance == rA + rB:
  • result.append("Touching")
  • elif distance == 0 and rA == rB:
  • result.append("Concentric")
  • elif distance < rA + rB:
  • result.append("Intersecting")
  • elif distance > rA + rB:
  • if distance > max(rA, rB):
  • result.append("Disjoint-Outside")
  • else:
  • result.append("Disjoint-Inside")
  • return result

answered
User Anthony D
by
8.3k points