Measuring Mixed Shapes

Python for AIMedium+10 XP+10

Measuring Mixed Shapes

A floor planner holds a mix of shapes. The starter code gives you Shape(name). Don't change it.

Write three children of Shape. Each has its own __init__ that passes its name up to the parent, and its own area():

ClassMade withnamearea()
SquareSquare(side)"square"side × side
RectangleRectangle(width, height)"rectangle"width × height
TriangleTriangle(base, height)"triangle"base × height ÷ 2

Then write two functions that work on a list of any mix of these shapes:

  • total_area(shapes) returns the sum of all the areas, rounded to 2 decimal places. An empty list gives 0.
  • largest_name(shapes) returns the name of the shape with the biggest area. If two shapes tie, return the one that comes first in the list. An empty list gives "none".

Examples

shapes = [Square(2), Rectangle(2, 3)]
returns 10
shapes = [Square(2), Triangle(4, 3)]
returns 'triangle'

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run