diff --git a/challenge.py b/challenge.py index ffdfcce..68bf5a1 100644 --- a/challenge.py +++ b/challenge.py @@ -1,54 +1,81 @@ import math +ERROR_MESSAGE = "I can't calculate this area, please verify your data.\n\t I only accept positive numbers." + def square_area(side): """Returns the area of a square""" - # You have to code here - # REMEMBER: Tests first!!! - pass + side = float(side) + + if side > 0.0: + return side**2 + else: + raise ValueError(ERROR_MESSAGE) def rectangle_area(base, height): """Returns the area of a rectangle""" - # You have to code here - # REMEMBER: Tests first!!! - pass + base = float(base) + height = float(height) + + if base > 0.0 and height > 0.0: + return base * height + else: + raise ValueError(ERROR_MESSAGE) def triangle_area(base, height): """Returns the area of a triangle""" - # You have to code here - # REMEMBER: Tests first!!! - pass + base = float(base) + height = float(height) + + if base > 0.0 and height > 0.0: + return (base * height) / 2 + else: + raise ValueError(ERROR_MESSAGE) def rhombus_area(diagonal_1, diagonal_2): """Returns the area of a rhombus""" - # You have to code here - # REMEMBER: Tests first!!! - pass + diagonal_1 = float(diagonal_1) + diagonal_2 = float(diagonal_2) + + if diagonal_1 > 0.0 and diagonal_2 > 0.0: + return (diagonal_1 * diagonal_2) / 2 + else: + raise ValueError(ERROR_MESSAGE) def trapezoid_area(base_minor, base_major, height): """Returns the area of a trapezoid""" - # You have to code here - # REMEMBER: Tests first!!! - pass + base_minor = float(base_minor) + base_major = float(base_major) + + if base_major > 0.0 and base_minor > 0.0: + return ((base_minor + base_major) / 2) * height + else: + raise ValueError(ERROR_MESSAGE) def regular_polygon_area(perimeter, apothem): """Returns the area of a regular polygon""" - # You have to code here - # REMEMBER: Tests first!!! - pass + perimeter = float(perimeter) + apothem = float(apothem) + + if perimeter > 0.0 and apothem > 0.0: + return (perimeter * apothem) / 2 + else: + raise ValueError(ERROR_MESSAGE) def circumference_area(radius): """Returns the area of a circumference""" - # You have to code here - # REMEMBER: Tests first!!! - # Use math.pi for π value - pass + radius = float(radius) + + if radius > 0.0: + return round(math.pi * (radius ** 2), 2) + else: + raise ValueError(ERROR_MESSAGE) if __name__ == '__main__': @@ -57,32 +84,52 @@ def circumference_area(radius): class GeometrySuite(unittest.TestCase): def setUp(self): - # Initialize the needed values for the tests - pass + self.values = { + 'side': 4, + 'base': 10, + 'height': 10, + 'diagonal_1': 10, + 'diagonal_2': 10, + 'base_minor': 10, + 'base_major': 10, + 'perimeter': 10, + 'apothem': 10, + 'radius': 1, + } def test_square_area(self): - # Make this test first... + self.assertEqual(16.0, square_area(self.values['side'])) def test_rectangle_area(self): - # Make this test first... + self.assertEqual(100, rectangle_area(self.values['base'], + self.values['height'] + )) def test_triangle_area(self): - # Make this test first... + self.assertEqual(50, triangle_area(self.values['base'], + self.values['height'] + )) def test_rhombus_area(self): - # Make this test first... + self.assertEqual(50, rhombus_area(self.values['diagonal_1'], + self.values['diagonal_2'] + )) def test_trapezoid_area(self): - # Make this test first... + self.assertEqual(100, trapezoid_area(self.values['base_minor'], + self.values['base_major'], + self.values['height'] + )) def test_regular_polygon_area(self): - # Make this test first... + self.assertEqual(50, regular_polygon_area(self.values['perimeter'], + self.values['apothem'] + )) def test_circumference_area(self): - # Make this test first... + self.assertEqual(3.14, circumference_area(self.values['radius'])) def tearDown(self): - # Delete the needed values for the tests - pass + del self.values unittest.main()