diff --git a/solutions/.vscode/launch.json b/solutions/.vscode/launch.json new file mode 100644 index 000000000..a75c1414f --- /dev/null +++ b/solutions/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python Debugger: Python File", + "type": "debugpy", + "request": "launch", + "program": "${file}" + } + ] +} \ No newline at end of file diff --git a/solutions/area_circle.py b/solutions/area_circle.py new file mode 100644 index 000000000..d667a0912 --- /dev/null +++ b/solutions/area_circle.py @@ -0,0 +1,32 @@ +""" +This module provides a function to calculate the area of a circle given its radius. +""" + +import math + + +def area_of_circle(radius: float) -> float: + """ + Calculate the area of a circle given its radius. + + Args: + radius (float): The radius of the circle. Must be a non-negative number. + + Returns: + float: The area of the circle. + + Raises: + ValueError: If the radius is negative. + + Examples: + >>> area_of_circle(1) + 3.141592653589793 + >>> area_of_circle(0) + 0.0 + >>> area_of_circle(2.5) + 19.634954084936208 + """ + if radius < 0: + raise ValueError("The radius must be a non-negative number.") + + return math.pi * radius * radius diff --git a/solutions/tests/test_area_circle.py b/solutions/tests/test_area_circle.py new file mode 100644 index 000000000..a80aa5d89 --- /dev/null +++ b/solutions/tests/test_area_circle.py @@ -0,0 +1,35 @@ +""" +Test suite for the area_of_circle function. +""" + +import math +import unittest +from area_circle import area_of_circle + + +class TestAreaOfCircle(unittest.TestCase): + """ + Unit tests for the area_of_circle function. + """ + + def test_positive_radius(self): + """Test area calculation with a positive radius.""" + self.assertAlmostEqual(area_of_circle(1), 3.141592653589793) + self.assertAlmostEqual(area_of_circle(2.5), 19.634954084936208) + + def test_zero_radius(self): + """Test area calculation with a radius of zero.""" + self.assertAlmostEqual(area_of_circle(0), 0.0) + + def test_negative_radius(self): + """Test area calculation with a negative radius.""" + with self.assertRaises(ValueError): + area_of_circle(-1) + + def test_large_radius(self): + """Test area calculation with a large radius.""" + self.assertAlmostEqual(area_of_circle(1e6), math.pi * 1e6 * 1e6) + + +if __name__ == "main": + unittest.main()