Skip to content

Commit

Permalink
Area of Circle
Browse files Browse the repository at this point in the history
  • Loading branch information
majdadel20 committed Jan 11, 2025
1 parent 262dad7 commit e24946c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions solutions/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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}"
}
]
}
32 changes: 32 additions & 0 deletions solutions/area_circle.py
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions solutions/tests/test_area_circle.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit e24946c

Please sign in to comment.