Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
After All Strategy Design Pattern was helpful solution in Clear category for Geometry Figures by Stensen
from decimal import Decimal as D, getcontext
from math import sqrt, tan, pi as PI
Q_Decimal = lambda expr: float(D(expr).quantize(D(10) ** -2)) # 2 Places Quantized Decimal
class Parameters(object):
def __init__(self, parameter): self.r = parameter; self.figure = None
def choose_figure(self, figure): self.figure = figure
perimeter = lambda self: self.figure.perimeter(self.r)
area = lambda self: self.figure.area(self.r)
volume = lambda self: self.figure.volume(self.r)
class Circle(object):
perimeter = lambda self, r: Q_Decimal(PI * 2 * r)
area = lambda self, r: Q_Decimal(PI * r ** 2)
volume = lambda self, r: 0
class Triangle(object):
perimeter = lambda self, r: Q_Decimal(3 * r)
area = lambda self, r: Q_Decimal(sqrt(3) / 4 * r ** 2)
volume = lambda self, r: 0
class Square(object):
perimeter = lambda self, s: Q_Decimal(4 * s)
area = lambda self, s: Q_Decimal(s ** 2)
volume = lambda self, s: 0
class Pentagon(object):
perimeter = lambda self, s: Q_Decimal(5 * s)
area = lambda self, s: Q_Decimal(D(5 * s ** 2) / D(4 * tan(PI / 5)))
volume = lambda self, s: 0
class Hexagon(object):
perimeter = lambda self, s: Q_Decimal(6 * s)
area = lambda self, s: Q_Decimal(3 * sqrt(3) / 2 * s ** 2)
volume = lambda self, s: 0
class Cube(object):
perimeter = lambda self, s: Q_Decimal(12 * s)
area = lambda self, s: Q_Decimal(6 * s ** 2)
volume = lambda self, s: Q_Decimal(s ** 3)
Oct. 6, 2020