diff --git a/solutions/area_of_circle.py b/solutions/area_of_circle.py new file mode 100644 index 000000000..c3562cfca --- /dev/null +++ b/solutions/area_of_circle.py @@ -0,0 +1,47 @@ +""" +A module for calculating the area of a circle. + +Module contents: + - area_of_circle: calculates the area of a circle given its radius. + +Created on 01 04 2025 +@author: Raghad +""" + +import math + + +# Define a function to calculate the area of a circle. +def area_of_circle(radius: float) -> float: + """Calculate the area of a circle given its radius. + + Parameters: + radius: float, the radius of the circle + + Returns -> float: area of the circle + + Raises: + ValueError: if the radius is negative + + Examples: + >>> area_of_circle(5) + 78.53981633974483 + >>> area_of_circle(0) + 0.0 + >>> area_of_circle(3.5) + 38.48451000647496 + """ + # Raise an error if the radius is negative. + if radius < 0: + raise ValueError("Radius cannot be negative") + + # Calculate and return the area using the formula: area = π * r^2 + return math.pi * radius**2 + + +# Entry point for script execution +if __name__ == "__main__": + # Example usage: Calculate the area of a circle with a radius of 5. + radius = 5 + area = area_of_circle(radius) + print("Area of the circle:", area) diff --git a/solutions/remove_duplicates.py b/solutions/remove_duplicates.py index 3b72fd373..8f3d46346 100644 --- a/solutions/remove_duplicates.py +++ b/solutions/remove_duplicates.py @@ -32,9 +32,9 @@ def remove_duplicates(nums: list) -> list: ['Heba', 'Noor'] """ assert isinstance(nums, list), "The input should be a list" - result = [] # List to store the result without duplicates + result = [] # List to store the result without duplicates. for num in nums: if num not in result: - result.append(num) # Add the number to the result list + result.append(num) # Add the number to the result list. return result diff --git a/solutions/tests/test_area_of_circle.py b/solutions/tests/test_area_of_circle.py new file mode 100644 index 000000000..82d575aae --- /dev/null +++ b/solutions/tests/test_area_of_circle.py @@ -0,0 +1,38 @@ +import unittest +from solutions.area_of_circle import area_of_circle + + +class TestCircleArea(unittest.TestCase): + """Unit tests for the area_of_circle function.""" + + def test_positive_radius(self): + """Test the function with positive radius values.""" + self.assertAlmostEqual(area_of_circle(5), 78.53981633974483) + self.assertAlmostEqual(area_of_circle(10), 314.1592653589793) + + def test_zero_radius(self): + """Test the function with a zero radius.""" + self.assertEqual(area_of_circle(0), 0.0) + + def test_large_radius(self): + """Test the function with a very large radius.""" + self.assertAlmostEqual(area_of_circle(1000), 3141592.653589793) + + def test_negative_radius(self): + """Test the function with a negative radius.""" + with self.assertRaises(ValueError) as context: + area_of_circle(-5) + self.assertEqual(str(context.exception), "Radius cannot be negative") + + def test_invalid_type(self): + """Test the function with an invalid type for radius.""" + with self.assertRaises(TypeError): + area_of_circle("string") + with self.assertRaises(TypeError): + area_of_circle([1, 2, 3]) + with self.assertRaises(TypeError): + area_of_circle(None) + + +if __name__ == "__main__": + unittest.main() diff --git a/solutions/tests/test_remove_duplicates.py b/solutions/tests/test_remove_duplicates.py index 33bcd99e4..9416ab8b2 100644 --- a/solutions/tests/test_remove_duplicates.py +++ b/solutions/tests/test_remove_duplicates.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ -Test module for remove_duplicates function. +Test module for remove_duplicates function Created on 2024-12-30 Author: Heba Shaheen @@ -17,17 +17,17 @@ class TestRemoveDuplicates(unittest.TestCase): # Standard test cases def test_numbers_list(self): - """It should remove duplicates numbers""" + """It should remove duplicates numbers. give [1, 2, 3, 4]""" self.assertEqual(remove_duplicates([1, 2, 3, 2, 3, 4]), [1, 2, 3, 4]) def test_letters_list(self): - """It should remove duplicates letters""" + """It should remove duplicates letters. give ["a", "v", "e", "q"]""" self.assertEqual( remove_duplicates(["a", "v", "e", "e", "q"]), ["a", "v", "e", "q"] ) def test_mix_list(self): - """It should remove duplicates items""" + """It should remove duplicates items. give [1, 2, 3, "e", 5, "a"]""" self.assertEqual( remove_duplicates([1, 2, 3, "e", 2, 1, "e", 5, "a"]), [1, 2, 3, "e", 5, "a"] )