One Loop, Many Shapes

Medium ⏱ 12 min 56% acceptance ★★★★★ 4.5
Define Rectangle(w, h) and Circle(r), each with its own area(). Then write the plain function total_area(shapes) that sums area() over a mixed list without any isinstance checks — polymorphism means the loop never asks what it is holding. Use math.pi and round the total to 2 decimals.

Examples

Example 1
Input
total_area([Rectangle(2, 3), Circle(1)])
Output
9.14
Explanation

6 + π rounds to 9.14.

Constraints

  • No isinstance in total_area.
  • Round the final sum only.

Topics

OOPpolymorphism

Companies

GoogleMetaOracle

Hints

Hint 1

Each class owns its formula.

Hint 2

sum(s.area() for s in shapes).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.