Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Geometry Figures by brownie57
from math import pi, tan
class Parameters():
def __init__(self, l):
self.l = l
def choose_figure(self, figures):
self.figures = figures
def perimeter(self):
return round(self.figures.perimeter(self.l), 2)
def area (self):
return round(self.figures.area(self.l), 2)
def volume(self):
return round(self.figures.volume(self.l), 2)
class Circle():
def perimeter(self, l):
return l * 2 * pi
def area(self, l):
return l ** 2 * pi
def volume(self, l):
return 0
class Triangle:
def perimeter(self, l):
return 3 * l
def area(self, l):
return l * l * tan(pi/3) / 4
def volume(self, l):
return 0
class Square:
def perimeter(self, l):
return l * 4
def area(self, l):
return l ** 2
def volume(self, l):
return 0
class Pentagon:
def perimeter(self, l):
return l * 5
def area(self, l):
return l * l * tan(pi * 3 / 10) / 4 * 5
def volume(self, l):
return 0
class Hexagon:
def perimeter(self, l):
return l * 6
def area(self, l):
return l * l * tan(pi/ 3) / 2 * 3
def volume(self, l):
return 0
class Cube:
def perimeter(self, l):
return l * 12
def area(self, l):
return l ** 2 * 6
def volume(self, l):
return l ** 3
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
figure = Parameters(10)
figure.choose_figure(Circle())
assert figure.area() == 314.16
figure.choose_figure(Triangle())
assert figure.perimeter() == 30
figure.choose_figure(Square())
assert figure.area() == 100
figure.choose_figure(Pentagon())
assert figure.perimeter() == 50
figure.choose_figure(Hexagon())
assert figure.perimeter() == 60
figure.choose_figure(Cube())
assert figure.volume() == 1000
print("Coding complete? Let's try tests!")
Dec. 8, 2018
Comments: