Method distance_to(other) returning Euclidean distance to another Point
Method translate(dx, dy) that moves the point and returns self
str returning "Point(x, y)"
Example
Input
Point(0, 0).distance_to(Point(3, 4))
Output
5.0
Reasoning
The input builds two points in the plane: Point(0, 0) is the origin and Point(3, 4) sits 3 units right and 4 units up. distance_to asks for the straight-line distance from the first to other.
Per-axis gaps: Δx=3−0=3 and Δy=4−0=4. They are the legs of a right triangle whose hypotenuse joins the points.
Euclidean distance is that hypotenuse: d=Δx2+Δy2=9+16=25=5.
The output is 5.0, a real number rather than the integer 5, since a square root is real-valued even when whole.
Notice the direction doesn't matter: squaring removes the sign, so the distance from (3,4) back to (0,0) is also 5.0. Likewise (1,2) to (4,6) has legs 3 and 4, giving 5.0 again.
Translating shifts both coordinates: (0,0) moved by dx=1, dy=2 becomes (0+1,0+2)=(1,2). The point itself moves.
A class is a blueprint for creating objects that bundle data (attributes) and behavior (methods) together. In Python, classes enable you to model real-world entities—like a 2D point—with both state (the x and y coordinates) and operations (calculating distance, moving the point). The init method is the constructor, automatically called when you create a new instance, allowing you to initialize the object's attributes. Instance methods are functions defined within a class that operate on individual objects; they always take self as their first parameter, which refers to the specific instance calling the method.
String Representations
Python provides two special methods for representing objects as strings: str is intended for end-users and should return a readable, friendly representation, while repr is aimed at developers and ideally returns a string that could recreate the object. For this problem, str should display the point in a human-readable format like "Point(x, y)".
Mathematical Foundation
The Euclidean distance between two points in 2D space is calculated using the distance formula: (x2−x1)2+(y2−y1)2. This comes from the Pythagorean theorem and is fundamental in geometry and many computational applications. The translation operation simply adds offsets to the current coordinates, shifting the point in the 2D plane.
Algorithm/Approach
The solution follows a straightforward object-oriented design pattern:
Encapsulation: Store the point's coordinates as instance attributes set during initialization
Distance Calculation: Implement a method that applies the Euclidean distance formula to compute the distance between two Point objects
State Mutation: Implement a method that modifies the point's coordinates and returns self to enable method chaining
String Formatting: Override str to provide a clean textual representation
Step-by-Step Strategy
Step 1: Define the Class and Constructor
Create a class named Point
Implement init to accept x and y parameters
Store these as instance attributes (e.g., self.x and self.y)
Step 2: Implement the distance_to Method
Accept another Point object as a parameter
Calculate the differences in x and y coordinates between the two points
Apply the Euclidean distance formula using the math module (specifically math.sqrt) or the exponentiation operator
Return the computed distance as a float
Step 3: Implement the translate Method
Accept dx and dy parameters representing displacement amounts
Update self.x by adding dx and self.y by adding dy
Return self to allow method chaining (this is important for the sample output)
Step 4: Implement str
Return a formatted string in the exact format "Point(x, y)"
Use f-strings or .format() for clean string interpolation
Ensure the output matches the expected format exactly
Common Pitfalls
Forgetting to return self in translate: The method must return the modified object itself, not None, to match the expected output and enable chaining
Incorrect distance formula: Double-check that you're squaring the differences before summing them, and taking the square root of the result
String formatting mismatches: The str output must exactly match "Point(x, y)"—watch for spacing and parentheses
Not importing required modules: Remember to import math if using math.sqrt, or use ** 0.5 for the square root operation
Confusing self with the parameter: When comparing two points in distance_to, clearly distinguish between the current point's attributes (self.x, self.y) and the other point's attributes
Time & Space Complexity
Constructor (init): O(1) time and space—simply storing two numeric values
distance_to method: O(1) time and space—performs a fixed number of arithmetic operations regardless of input size
translate method: O(1) time and space—updates two attributes with constant-time operations
str method: O(1) time and space—string formatting is constant relative to the problem size
Overall, all operations on a Point object are constant-time, making this an efficient representation for geometric computations.