Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Decorator solution in Clear category for Simple Areas by veky
def simple_areas(*args):
import math
areas = []
def half(x):
return x / 2
@areas.append
def circle(diameter):
radius = half(diameter)
return radius ** 2 * math.pi
@areas.append
def rectangle(width, height):
return width * height
@areas.append
def triangle(a, b, c):
"""Heron's formula"""
perimeter = a + b + c
s = half(perimeter)
return math.sqrt(s*(s - a)*(s - b)*(s - c))
for area in areas:
try:
return area(*args)
except TypeError:
continue
May 16, 2014
Comments: