From 03d4c5f43c9e923ba780ba57821d91fae9667a37 Mon Sep 17 00:00:00 2001 From: CherryKitten Date: Sun, 27 Nov 2022 15:01:33 +0100 Subject: [PATCH] Add polygon area calculator --- .../4-polygon-area-calculator/.replit | 2 + .../4-polygon-area-calculator/README.md | 3 + .../4-polygon-area-calculator/main.py | 20 +++ .../shape_calculator.py | 52 ++++++++ .../4-polygon-area-calculator/test_module.py | 116 ++++++++++++++++++ 5 files changed, 193 insertions(+) create mode 100644 8-scientific-computing-python/4-polygon-area-calculator/.replit create mode 100644 8-scientific-computing-python/4-polygon-area-calculator/README.md create mode 100644 8-scientific-computing-python/4-polygon-area-calculator/main.py create mode 100644 8-scientific-computing-python/4-polygon-area-calculator/shape_calculator.py create mode 100644 8-scientific-computing-python/4-polygon-area-calculator/test_module.py diff --git a/8-scientific-computing-python/4-polygon-area-calculator/.replit b/8-scientific-computing-python/4-polygon-area-calculator/.replit new file mode 100644 index 0000000..c88f45f --- /dev/null +++ b/8-scientific-computing-python/4-polygon-area-calculator/.replit @@ -0,0 +1,2 @@ +language = "python3" +run = "python main.py" \ No newline at end of file diff --git a/8-scientific-computing-python/4-polygon-area-calculator/README.md b/8-scientific-computing-python/4-polygon-area-calculator/README.md new file mode 100644 index 0000000..408734a --- /dev/null +++ b/8-scientific-computing-python/4-polygon-area-calculator/README.md @@ -0,0 +1,3 @@ +# Polygon Area Calculator + +This is the boilerplate for the Polygon Area Calculator project. Instructions for building your project can be found at https://www.freecodecamp.org/learn/scientific-computing-with-python/scientific-computing-with-python-projects/polygon-area-calculator diff --git a/8-scientific-computing-python/4-polygon-area-calculator/main.py b/8-scientific-computing-python/4-polygon-area-calculator/main.py new file mode 100644 index 0000000..d4354fa --- /dev/null +++ b/8-scientific-computing-python/4-polygon-area-calculator/main.py @@ -0,0 +1,20 @@ +# This entrypoint file to be used in development. Start by reading README.md +import shape_calculator +from unittest import main + + +rect = shape_calculator.Rectangle(5, 10) +print(rect.get_area()) +rect.set_width(3) +print(rect.get_perimeter()) +print(rect) + +sq = shape_calculator.Square(9) +print(sq.get_area()) +sq.set_side(4) +print(sq.get_diagonal()) +print(sq) + + +# Run unit tests automatically +main(module='test_module', exit=False) \ No newline at end of file diff --git a/8-scientific-computing-python/4-polygon-area-calculator/shape_calculator.py b/8-scientific-computing-python/4-polygon-area-calculator/shape_calculator.py new file mode 100644 index 0000000..74fa25d --- /dev/null +++ b/8-scientific-computing-python/4-polygon-area-calculator/shape_calculator.py @@ -0,0 +1,52 @@ +import math + + +class Rectangle(): + + def __init__(self, w, h): + self.width = w + self.height = h + + def __str__(self): + return "Rectangle(width=" + str(self.width) + ", height=" + str(self.height) + ")" + + def set_width(self, w): + self.width = w + if type(self) == "Square": self.height = w + + def set_height(self, h): + self.height = h + if type(self) == "Square": self.width = h + + def get_area(self): + return self.width * self.height + + def get_perimeter(self): + return 2* self.width + 2 * self.height + + def get_diagonal(self): + return (self.width ** 2 + self.height ** 2) ** .5 + + def get_picture(self): + picture = "" + if self.width > 50 or self.height > 50: return "Too big for picture." + for i in range(0, self.height): + picture = picture + (self.width * "*") + "\n" + return picture + + def get_amount_inside(self, shape): + return math.trunc(self.get_area() / shape.get_area()) + + +class Square(Rectangle): + + def __init__(self, side): + self.width = side + self.height = side + + def __str__(self): + return "Square(side=" + str(self.width) + ")" + + def set_side(self, side): + self.width = side + self.height = side diff --git a/8-scientific-computing-python/4-polygon-area-calculator/test_module.py b/8-scientific-computing-python/4-polygon-area-calculator/test_module.py new file mode 100644 index 0000000..58034f5 --- /dev/null +++ b/8-scientific-computing-python/4-polygon-area-calculator/test_module.py @@ -0,0 +1,116 @@ +import unittest +import shape_calculator + + +class UnitTests(unittest.TestCase): + maxDiff = None + def setUp(self): + self.rect = shape_calculator.Rectangle(3, 6) + self.sq = shape_calculator.Square(5) + + def test_subclass(self): + actual = issubclass(shape_calculator.Square, shape_calculator.Rectangle) + expected = True + self.assertEqual(actual, expected, 'Expected Square class to be a subclass of the Rectangle class.') + + def test_distinct_classes(self): + actual = shape_calculator.Square is not shape_calculator.Rectangle + expected = True + self.assertEqual(actual, expected, 'Expected Square class to be a distinct class from the Rectangle class.') + + def test_square_is_square_and_rectangle(self): + actual = isinstance(self.sq, shape_calculator.Square) and isinstance(self.sq, shape_calculator.Rectangle) + expected = True + self.assertEqual(actual, expected, 'Expected square object to be an instance of the Square class and the Rectangle class.') + + def test_rectangle_string(self): + actual = str(self.rect) + expected = "Rectangle(width=3, height=6)" + self.assertEqual(actual, expected, 'Expected string representation of rectangle to be "Rectangle(width=3, height=6)"') + + def test_square_string(self): + actual = str(self.sq) + expected = "Square(side=5)" + self.assertEqual(actual, expected, 'Expected string representation of square to be "Square(side=5)"') + + def test_area(self): + actual = self.rect.get_area() + expected = 18 + self.assertEqual(actual, expected, 'Expected area of rectangle to be 18') + actual = self.sq.get_area() + expected = 25 + self.assertEqual(actual, expected, 'Expected area of square to be 25') + + + def test_perimeter(self): + actual = self.rect.get_perimeter() + expected = 18 + self.assertEqual(actual, expected, 'Expected perimeter of rectangle to be 18') + actual = self.sq.get_perimeter() + expected = 20 + self.assertEqual(actual, expected, 'Expected perimeter of square to be 20') + + def test_diagonal(self): + actual = self.rect.get_diagonal() + expected = 6.708203932499369 + self.assertEqual(actual, expected, 'Expected diagonal of rectangle to be 6.708203932499369') + actual = self.sq.get_diagonal() + expected = 7.0710678118654755 + self.assertEqual(actual, expected, 'Expected diagonal of square to be 7.0710678118654755') + + def test_set_attributes(self): + self.rect.set_width(7) + self.rect.set_height(8) + self.sq.set_side(2) + actual = str(self.rect) + expected = "Rectangle(width=7, height=8)" + self.assertEqual(actual, expected, 'Expected string representation of rectangle after setting new values to be "Rectangle(width=7, height=8)"') + actual = str(self.sq) + expected = "Square(side=2)" + self.assertEqual(actual, expected, 'Expected string representation of square after setting new values to be "Square(side=2)"') + self.sq.set_width(4) + actual = str(self.sq) + expected = "Square(side=4)" + self.assertEqual(actual, expected, 'Expected string representation of square after setting width to be "Square(side=4)"') + + def test_rectangle_picture(self): + self.rect.set_width(7) + self.rect.set_height(3) + actual = self.rect.get_picture() + expected = "*******\n*******\n*******\n" + self.assertEqual(actual, expected, 'Expected rectangle picture to be different.') + + def test_square_picture(self): + self.sq.set_side(2) + actual = self.sq.get_picture() + expected = "**\n**\n" + self.assertEqual(actual, expected, 'Expected square picture to be different.') + + def test_big_picture(self): + self.rect.set_width(51) + self.rect.set_height(3) + actual = self.rect.get_picture() + expected = "Too big for picture." + self.assertEqual(actual, expected, 'Expected message: "Too big for picture."') + + def test_get_amount_inside(self): + self.rect.set_height(10) + self.rect.set_width(15) + actual = self.rect.get_amount_inside(self.sq) + expected = 6 + self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 6.') + + def test_get_amount_inside_two_rectangles(self): + rect2 = shape_calculator.Rectangle(4, 8) + actual = rect2.get_amount_inside(self.rect) + expected = 1 + self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 1.') + + def test_get_amount_inside_none(self): + rect2 = shape_calculator.Rectangle(2, 3) + actual = rect2.get_amount_inside(self.rect) + expected = 0 + self.assertEqual(actual, expected, 'Expected `get_amount_inside` to return 0.') + +if __name__ == "__main__": + unittest.main()