easy +10 pts

Dataclass with slots

Build a frozen, slotted dataclass for immutable 2D points with ordering support.

Define a class `Point` using `@dataclass` with the following requirements: - The dataclass must be **frozen** (immutable) and use **slots** (`slots=True`). - It should have two fields: `x` (int) and `y` (int), in that order. - The dataclass must be **orderable** by comparing the tuple `(x, y)` lexicographically. That is, a point `p1` is less than `p2` if `p1.x < p2.x` or (`p1.x == p2.x` and `p1.y < p2.y`). Implement the ordering by setting the `order=True` parameter of the dataclass decorator (do not manually define comparison methods). - Ensure that instances do **not** have an instance dictionary (`__dict__`). Because `slots=True` is used, `Point.__slots__` will contain the field names (`('x', 'y')`). Do not attempt to override `__slots__` manually. - Instance attributes must be accessible as `point.x` and `point.y`. Write the class definition exactly as specified. Do not add extra methods or attributes. The constructor must accept `x` and `y` as positional arguments in that order. Additionally, implement the following helper functions (they will be tested): - `make_point_1_2()` returns `[Point(1, 2).x, Point(1, 2).y]`. - `make_point_0_0()` returns `[Point(0, 0).x, Point(0, 0).y]`. - `make_point_neg_1_neg_2()` returns `[Point(-1, -2).x, Point(-1, -2).y]`. - `make_point_10_20()` returns `[Point(10, 20).x, Point(10, 20).y]`. Make sure all these functions are defined in your submitted code.

Constraints

- `x` and `y` are integers. - The class must be a dataclass with `frozen=True` and `slots=True`. - The class must have `order=True` for comparison operators. - Instances must not have a `__dict__` attribute. - Do not manually define comparison methods. - Helper functions must be named exactly as listed and return lists of integers.

Example

>>> p = Point(1, 2)
>>> p.x
1
>>> p.y
2
>>> Point(1, 2) == Point(1, 2)
True
>>> Point(1, 2) < Point(2, 0)
True
>>> Point(2, 0) < Point(1, 2)
False
>>> hasattr(p, '__dict__')
False
>>> make_point_1_2()
[1, 2]
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use the `@dataclass(frozen=True, slots=True, order=True)` decorator.
Declare the two fields with type annotations: `x: int` and `y: int`.
Each helper function should construct a `Point` with the given coordinates and return a list of its `x` and `y` attributes.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.