Skip to content

Instantly share code, notes, and snippets.

@Divya0319
Created August 10, 2024 13:06
Show Gist options
  • Save Divya0319/0149b3f3b458b64ecfa922fc8a1e708d to your computer and use it in GitHub Desktop.
Save Divya0319/0149b3f3b458b64ecfa922fc8a1e708d to your computer and use it in GitHub Desktop.
OOPS-3 Additional problem 3 - Rectangles Python LLD
import copy
from .point import Point
class Rectangle:
def __init__(self, *args):
if len(args) == 2:
# Case 1: Two Point objects
if all(isinstance(arg, Point) for arg in args):
self.topLeft, self.bottomRight = args
else:
raise ValueError("If two arguments are provided, both must be Point objects.")
elif len(args) == 4:
# Case 2: Four coordinates
try:
x1, y1, x2, y2 = args
self.topLeft = Point(x1, y1)
self.bottomRight = Point(x2, y2)
except (TypeError, ValueError):
raise ValueError("Four arguments must be integers or floats representing coordinates.")
elif len(args) == 1 and isinstance(args[0], Rectangle):
# Case 3: Copying from another Rectangle object
original = args[0]
self.topLeft = copy.deepcopy(original.topLeft)
self.bottomRight = copy.deepcopy(original.bottomRight)
else:
raise ValueError("Invalid arguments. Provide either two Point objects, four coordinates, or a single Rectangle object.")
# Error starts from here
Traceback (most recent call last):
File "/config/workspace/tests/testRectangle.py", line 21, in test_constructor_with_points
self.assertIsNot(rectangle.topLeft, topLeft)
AssertionError: unexpectedly identical: <config.point.Point object at 0x7f9903030580>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment