Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculating area of circle #61

Merged
merged 13 commits into from
Jan 11, 2025
41 changes: 41 additions & 0 deletions solutions/area_of_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
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


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

>>> area_of_circle(5)
78.53981633974483
>>> area_of_circle(0)
0.0
>>> area_of_circle(3.5)
38.48451000647496
"""
if radius < 0:
raise ValueError("Radius cannot be negative")

return math.pi * radius**2

Moealfadil marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == "__main__":
radius = 5
area = area_of_circle(radius)
print("Area of the circle:", area)
6 changes: 3 additions & 3 deletions solutions/cumulative_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def cumulative_sum(numbers: list) -> list:
# Validate input
assert numbers is not None, "Input cannot be None."
assert isinstance(numbers, list), "Input must be a list of numeric values."
assert all(
isinstance(num, (int, float)) for num in numbers
), "All elements in the list must be numeric."
assert all(isinstance(num, (int, float)) for num in numbers), (
"All elements in the list must be numeric."
)
# Compute cumulative sums
cumulative_list = []
current_sum = 0
Expand Down
23 changes: 23 additions & 0 deletions solutions/tests/test_area_of_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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)

Moealfadil marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == "__main__":
unittest.main()
Loading