Processing shapes

Report a typo

You are working on a project that involves processing different types of shapes. You've decided to create an enum to represent the types of shapes you're working with: ShapeType. The ShapeType enum has four values: CIRCLE, SQUARE, TRIANGLE, and RECTANGLE.

Write a function calculate_area(shape_type, *args) that takes in a ShapeType value and a variable number of arguments. The number and type of arguments depend on the ShapeType. For a CIRCLE, the function takes in one argument (the radius). For a SQUARE, TRIANGLE, and RECTANGLE, the function takes in two arguments (the length and width, base and height, or side length respectively).

The function should calculate the area of the shape based on the arguments provided.

Sample Input 1:

CIRCLE, 5

Sample Output 1:

78.53975
Write a program in Python 3
from enum import Enum

class ShapeType(Enum):
CIRCLE = (lambda r: 3.14159 * r * r,)
SQUARE = (lambda l: l * l,)
TRIANGLE = (lambda b, h: 0.5 * b * h,)
RECTANGLE = (lambda l, w: l * w,)

def calculate_area(shape_type, *args):
formula = # Get the formula for the given ShapeType

# Check if the number of arguments matches the number expected by the formula
if len(args) != formula.__code__.co_argcount:
return None

# Call the formula with the arguments and return the result

inp = input()
shape, arguments = inp.split(",")[0], map(int, inp.split(",")[1:])


area = calculate_area(ShapeType[shape], *arguments)
print(area)

Create a free account to access the full topic