easy +10 pts

Rectangle class

Model a rectangle with area, perimeter, and scaling capabilities.

Create a class `Rectangle` that models a rectangle with a given `width` and `height`. The class must support the following: - Constructor: `__init__(self, width, height)` stores the width and height as instance attributes. Assume width and height are non-negative numbers (int or float). No validation is required. - Properties: `area` and `perimeter` as read-only properties (no setter). - `area` returns width * height. - `perimeter` returns 2 * (width + height). - Method: `scale(self, factor)` returns a new `Rectangle` with width and height multiplied by `factor`. The original rectangle is unchanged. - Special methods: - `__repr__`: return a string `Rectangle(width, height)` with the exact numeric values (no rounding). - `__eq__(self, other)`: return `True` if `other` is a `Rectangle` with the same width and height, otherwise `False`. You must implement the class exactly with the specified method and property names. The automated tests will call helper functions that use your class. Note: The helper functions `area`, `perimeter`, `rectangle_repr`, `scale`, and `eq` are used by the test harness and are provided in the skeleton. Your task is to implement the `Rectangle` class so that these helper functions work correctly.

Constraints

- Width and height are non-negative numbers (int or float). - The scale factor is a non-negative number. - Input sizes are small; time complexity is not a concern.

Example

>>> r = Rectangle(3, 4)
>>> r.area
12
>>> r.perimeter
14
>>> repr(r)
'Rectangle(3, 4)'
>>> r2 = r.scale(2)
>>> r2.width, r2.height
(6, 8)
>>> r == r2
False
10 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use `@property` decorator to make area and perimeter read-only.
In `__repr__`, return a string that looks like a constructor call.
In `__eq__`, check that the other object is also a Rectangle using `isinstance`.
The `scale` method should create a new Rectangle instance, not modify self.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.