Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/geometric_lib.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches:
- '**'

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ${{matrix.os}}
strategy:
matrix:
os: ["ubuntu-latest", "windows-latest"]

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

# Runs a single command using the runners shell
- name: Run tests
run: python -m unittest
29 changes: 28 additions & 1 deletion circle.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,36 @@


def area(r):
"""
Returns the area of the circle with specified radius

Parameters:
r (int or float): circle`s radius

Return value:
area (float): area of the circus calculated by formula Pi * r * r
"""
if (not(isinstance(r, int) or isinstance(r, float))):
raise ValueError("Radius of the circle must be a number")
if (r < 0):
raise ValueError("Radius of the circle must be a positive number")

return math.pi * r * r


def perimeter(r):
return 2 * math.pi * r
"""
Returns the length of the circle with specified radius

Parameters:
r (int or float): circle`s radius

Return value:
circumference (float): length of the circus calculated by formula 2 * Pi * r
"""
if (not(isinstance(r, int) or isinstance(r, float))):
raise ValueError("Radius of the circle must be a number")
if (r < 0):
raise ValueError("Radius of the circle must be a positive number")

return 2 * math.pi * r
100 changes: 96 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,102 @@
# Math formulas
## Area
# Geometric Lib

<p>This is the official documentation for Geometric Lib</p>

* [General description of the solution](#general_description)
* [Description of functions](#functions_description)
* [Circle](#circle_description)
* [Rectangle](#rectangle_description)
* [Square](#square_description)
* [Triangle](#triangle_description)
* [Project modification history](#modification)

## <a id="general_description">General description of the solution</a>
<p>This library is created to calculate the area and perimeter of geometric shapes. The following formulas are used:</p>

### Area
- Circle: S = πR²
- Rectangle: S = ab
- Square: S = a²
- Triangle: S = ${ah\over 2}$

## Perimeter
### Perimeter
- Circle: P = 2πR
- Rectangle: P = 2a + 2b
- Square: P = 4a
- Square: P = 4a
- Triangle: P = a + b + c

## <a id="functions_description">Description of functions</a>
<p>Here you can see description of the functionality for each geometric shape</p>

### <a id="circle_description">Circle</a>
**area**(r)
&nbsp; Returns the area of the circle with specified radius **r**
**perimeter**(r)
&nbsp; Returns the length of the circle with specified radius **r**

>>> circle_area = area(5)
>>> circle_area
78.53981633974483
>>> circle_perimeter = perimeter(5)
>>> circle_perimeter
31.41592653589793
>>> circle_perimeter = perimeter(2.3)
>>> circle_perimeter
14.451326206513047

### <a id="rectangle_description">Rectangle</a>
**area**(a, b)
&nbsp; Returns the area of the rectangle with specified sides **a** and **b**
**perimeter**(a, b)
&nbsp; Returns the perimeter of the rectangle with specified sides **a** and **b**

>>> rectangle_area = area(5, 8)
>>> rectangle_area
40
>>> rectangle_area = area(3.5, 9.7)
>>> rectangle_area
33.949999999999996
>>> rectangle_perimeter = perimeter(2, 3)
>>> rectangle_perimeter
10
>>> rectangle_perimeter = perimeter(2.3, 6.9)
>>> rectangle_perimeter
18.4

### <a id="square_description">Square</a>
**area**(a)
&nbsp; Returns the area of the square with specified side **a**
**perimeter**(a)
&nbsp; Returns the perimeter of the square with specified side **a**

>>> square_area = area(5)
>>> square_area
25
>>> square_perimeter = perimeter(2.3)
>>> square_perimeter
9.2

### <a id="triangle_description">Triangle</a>
**area**(a, h)
&nbsp; Returns the area of the triangle with specified side **a** and height **h**
**perimeter**(a, b, c)
&nbsp; Returns the perimeter of the triangle with specified sides **a**, **b** and **c**

>>> triangle_area = area(12, 5)
>>> triangle_area
30.0
>>> triangle_perimeter = perimeter(2.5, 3.7, 4)
>>> triangle_perimeter
10.2

## <a id="modification">Project modification history</a>
| Commit hash | Commit message (description) |
| -------------- | ----------------------------------------------------------------------- |
| 8ba9aeb | Circle and square added |
| d078c8d | Docs added |
| 8eed03e | Rectangle added |
| e661c97 | Correct calculation of the rectangle perimeter |
| 477bc4a | Correct calculation of the rectangle perimeter and area |
| c1c5080 | Triangle added |
| ed75673 | Added check arguments in functions in all modules |
| 3f466e6 | Added unit test for all modules(circle, rectangle, square and triangle) |
37 changes: 37 additions & 0 deletions rectangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
def area(a, b):
"""
Returns the area of the rectangle with specified sides

Parameters:
a (int or float): length of the rectangle`s side
b (int or float): length of the other rectangle`s side

Return value:
area (int or float): area of the rectangle calculated by formula a * b
"""
if (not(isinstance(a, int) or isinstance(a, float)) or \
not(isinstance(b, int) or isinstance(b, float))):
raise ValueError("Sides of the rectangle must numbers")
if (a < 0 or b < 0):
raise ValueError("Sides of the rectangle must have positive length")

return a * b

def perimeter(a, b):
"""
Returns the perimeter of the rectangle with specified sides

Parameters:
a (int or float): length of the rectangle`s side
b (int or float): length of the other rectangle`s side

Return value:
perimeter (int or float): perimeter of the rectangle calculated by formula 2 * (a + b)
"""
if (not(isinstance(a, int) or isinstance(a, float)) or \
not(isinstance(b, int) or isinstance(b, float))):
raise ValueError("Sides of the rectangle must be numbers")
if (a < 0 or b < 0):
raise ValueError("Sides of the rectangle must have positive length")

return 2 * (a + b)
29 changes: 28 additions & 1 deletion square.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@

def area(a):
"""
Returns the area of the square with specified side

Parameters:
a (int or float): length of the square`s side

Return value:
area (int or float): area of the square calculated by formula a * a
"""
if (not(isinstance(a, int) or isinstance(a, float))):
raise ValueError("Side of the square must be a number")
if (a < 0):
raise ValueError("Side of the square must have positive length")

return a * a


def perimeter(a):
"""
Returns the perimeter of the square with specified side

Parameters:
a (int or float): length of the square`s side

Return value:
perimeter (int or float): perimeter of the square calculated by formula 4 * a
"""
if (not(isinstance(a, int) or isinstance(a, float))):
raise ValueError("Side of the square must be a number")
if (a < 0):
raise ValueError("Side of the square must have positive length")

return 4 * a
Empty file added test/__init__.py
Empty file.
93 changes: 93 additions & 0 deletions test/test_circle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import math
import unittest

import circle


class CircleTestCase(unittest.TestCase):
delta = 0.001

def test_arguments_instance(self):
tests_count = 5
input_data = ['1', [555], "fjkls", set([1, 2, 1, 3]), "12.5"]

# for area()
for i in range(tests_count):
r = input_data[i]
with self.assertRaises(ValueError):
circle.area(r)

# for perimeter()
for i in range(tests_count):
r = input_data[i]
with self.assertRaises(ValueError):
circle.perimeter(r)

def test_radius_length(self):
tests_count = 3
input_data = [-10, -38.8, -312891284]

# for area()
for i in range(tests_count):
r = input_data[i]
with self.assertRaises(ValueError):
circle.area(r)

# for perimeter()
for i in range(tests_count):
r = input_data[i]
with self.assertRaises(ValueError):
circle.perimeter(r)

def test_zero(self):
self.assertEqual(circle.area(0), 0)
self.assertEqual(circle.perimeter(0), 0)

def test_area(self):
tests_count = 5
input_data = [1, 8, 2.5, 142, 125.6]
expected_results = [math.pi * 1 * 1,
math.pi * 8 * 8,
math.pi * 2.5 * 2.5,
math.pi * 142 * 142,
math.pi * 125.6 * 125.6]

for i in range(tests_count):
r = input_data[i]
result_area = circle.area(r)
expected_area = expected_results[i]

self.assertAlmostEqual(result_area, expected_area, delta=self.delta)

def test_perimeter(self):
tests_count = 5
input_data = [1, 10, 2.5, 5642, 589.67]
expected_results = [2 * math.pi * 1,
2 * math.pi * 10,
2 * math.pi * 2.5,
2 * math.pi * 5642,
2 * math.pi * 589.67]

for i in range(tests_count):
r = input_data[i]
result_perimeter = circle.perimeter(r)
expected_perimeter = expected_results[i]

self.assertAlmostEqual(result_perimeter, expected_perimeter, delta=self.delta)

def test_result_instance(self):
tests_count = 3
input_data_int = [2, 50, 409842354]
input_data_float = [5.2, 77.1, 409842354.67]

for i in range(tests_count):
r = input_data_int[i]

self.assertIsInstance(circle.area(r), float)
self.assertIsInstance(circle.perimeter(r), float)

for i in range(tests_count):
r = input_data_float[i]

self.assertIsInstance(circle.area(r), float)
self.assertIsInstance(circle.perimeter(r), float)
Loading