Basic Class Creation
Problem Statement
Create a class to represent a 2D point.
Background
Python classes define objects with:
- init: Constructor
- Instance methods: Take self as first argument
- str: String representation
- repr: Developer representation
Your Task
Create a class Point with:
- Constructor taking x and y coordinates
- 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:
Point(0, 0).distance_to(Point(3, 4))
5.0
sqrt((3-0)^2 + (4-0)^2) = sqrt(9+16) = sqrt(25) = 5.0
Constraints:
- Distance uses Euclidean formula: sqrt((x2-x1)^2 + (y2-y1)^2)
- translate modifies the point in place and returns self
Background Knowledge
Classes and Objects in Python
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.