MediumPro challengePythonJavaScriptTypeScript
OOP — Shape Inheritance
PythonOOPClasses
Build a small class hierarchy, then answer one question about it per call.
solve(aspect) returns { aspect: value } — a single-key dict naming what was
asked. The grader compares JSON, so it asks for one aspect at a time rather than
for the classes themselves.
The hierarchy
Shape (base class)
- Constructor:
__init__(self, color: str) - Method:
describe(self) -> strreturns"I am a {color} shape."
Circle (extends Shape)
- Constructor:
__init__(self, color: str, radius: float) - Method:
area(self) -> floatreturnsπ × radius²(usemath.pi) - Overrides
describe(self)→"I am a {color} circle with radius {radius}."
Rectangle (extends Shape)
- Constructor:
__init__(self, color: str, width: float, height: float) - Method:
area(self) -> floatreturnswidth × height - Overrides
describe(self)→"I am a {color} rectangle of {width}x{height}."
The aspects
| aspect | value to return |
|---|---|
shape_describe | Shape("blue").describe() |
circle_describe | Circle("red", 5).describe() |
circle_area_approx | Circle("red", 5).area() rounded to 2 decimals |
circle_area_unit | Circle("red", 1).area() |
rectangle_describe | Rectangle("green", 4, 6).describe() |
rectangle_area | Rectangle("green", 4, 6).area() |
circle_is_shape | whether a Circle is an instance of Shape |
rectangle_is_shape | whether a Rectangle is an instance of Shape |
Example
solve("circle_describe") # {"circle_describe": "I am a red circle with radius 5."}
solve("rectangle_area") # {"rectangle_area": 24}In JavaScript / TypeScript — Use class … extends Shape and super(color); the area is Math.PI * radius ** 2, rounded to two decimals for circle_area_approx, and instanceof answers the *_is_shape aspects.
Sample tests
Test #1Shape base describe
Input: ["shape_describe"]
Output: {"shape_describe":"I am a blue shape."}
Test #2Circle describe override
Input: ["circle_describe"]
Output: {"circle_describe":"I am a red circle with radius 5."}
Test #3Circle area ≈ π×5², rounded
Input: ["circle_area_approx"]
Output: {"circle_area_approx":78.54}
Test #4Rectangle describe override
Input: ["rectangle_describe"]
Output: {"rectangle_describe":"I am a green rectangle of 4x6."}
Test #5Rectangle area 4×6
Input: ["rectangle_area"]
Output: {"rectangle_area":24}