From 11ed706cb7b570b84ea5c1e58677a2b570c6e670 Mon Sep 17 00:00:00 2001 From: rohanr18 <114707091+rohanr18@users.noreply.github.com> Date: Sat, 1 Oct 2022 13:56:35 +0530 Subject: [PATCH 1/5] Add volume of hollow circular cylinder, Exceptions --- maths/volume.py | 177 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 157 insertions(+), 20 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index acaed65f4858..3e69f28aa9f8 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -1,6 +1,5 @@ """ Find Volumes of Various Shapes. - Wikipedia reference: https://en.wikipedia.org/wiki/Volume """ from __future__ import annotations @@ -11,12 +10,17 @@ def vol_cube(side_length: int | float) -> float: """ Calculate the Volume of a Cube. - >>> vol_cube(1) 1.0 >>> vol_cube(3) 27.0 + >>> vol_cube(-1) + Traceback (most recent call last): + ... + ValueError: vol_cube() only accepts non-negative values """ + if side_length < 0: + raise ValueError("vol_cube() only accepts non-negative values") return pow(side_length, 3) @@ -24,10 +28,19 @@ def vol_spherical_cap(height: float, radius: float) -> float: """ Calculate the Volume of the spherical cap. :return 1/3 pi * height ^ 2 * (3 * radius - height) - >>> vol_spherical_cap(1, 2) 5.235987755982988 + >>> vol_spherical_cap(-1, 2) + Traceback (most recent call last): + ... + ValueError: vol_spherical_cap() only accepts non-negative values + >>> vol_spherical_cap(1, -2) + Traceback (most recent call last): + ... + ValueError: vol_spherical_cap() only accepts non-negative values """ + if height < 0 or radius < 0: + raise ValueError("vol_spherical_cap() only accepts non-negative values") return 1 / 3 * pi * pow(height, 2) * (3 * radius - height) @@ -36,7 +49,6 @@ def vol_spheres_intersect( ) -> float: """ Calculate the volume of the intersection of two spheres. - The intersection is composed by two spherical caps and therefore its volume is the sum of the volumes of the spherical caps. First, it calculates the heights (h1, h2) of the spherical caps, then the two volumes and it returns the sum. @@ -49,10 +61,23 @@ def vol_spheres_intersect( / (2 * centers_distance) if centers_distance is 0 then it returns the volume of the smallers sphere :return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1) - >>> vol_spheres_intersect(2, 2, 1) 21.205750411731103 + >>> vol_spheres_intersect(-2, 2, 1) + Traceback (most recent call last): + ... + ValueError: vol_spheres_intersect() only accepts non-negative values + >>> vol_spheres_intersect(2, -2, 1) + Traceback (most recent call last): + ... + ValueError: vol_spheres_intersect() only accepts non-negative values + >>> vol_spheres_intersect(2, 2, -1) + Traceback (most recent call last): + ... + ValueError: vol_spheres_intersect() only accepts non-negative values """ + if radius_1 < 0 or radius_2 < 0 or centers_distance < 0: + raise ValueError("vol_spheres_intersect() only accepts non-negative values") if centers_distance == 0: return vol_sphere(min(radius_1, radius_2)) @@ -74,40 +99,69 @@ def vol_cuboid(width: float, height: float, length: float) -> float: """ Calculate the Volume of a Cuboid. :return multiple of width, length and height - >>> vol_cuboid(1, 1, 1) 1.0 >>> vol_cuboid(1, 2, 3) 6.0 + >>> vol_cuboid(-1, 2, 3) + Traceback (most recent call last): + ... + ValueError: vol_cuboid() only accepts non-negative values + >>> vol_cuboid(1, -2, 3) + Traceback (most recent call last): + ... + ValueError: vol_cuboid() only accepts non-negative values + >>> vol_cuboid(1, 2, -3) + Traceback (most recent call last): + ... + ValueError: vol_cuboid() only accepts non-negative values """ + if width < 0 or height < 0 or length < 0: + raise ValueError("vol_cuboid() only accepts non-negative values") return float(width * height * length) def vol_cone(area_of_base: float, height: float) -> float: """ Calculate the Volume of a Cone. - Wikipedia reference: https://en.wikipedia.org/wiki/Cone :return (1/3) * area_of_base * height - >>> vol_cone(10, 3) 10.0 >>> vol_cone(1, 1) 0.3333333333333333 + >>> vol_cone(-1, 1) + Traceback (most recent call last): + ... + ValueError: vol_cone() only accepts non-negative values + >>> vol_cone(1, -1) + Traceback (most recent call last): + ... + ValueError: vol_cone() only accepts non-negative values """ + if height < 0 or area_of_base < 0: + raise ValueError("vol_cone() only accepts non-negative values") return area_of_base * height / 3.0 def vol_right_circ_cone(radius: float, height: float) -> float: """ Calculate the Volume of a Right Circular Cone. - Wikipedia reference: https://en.wikipedia.org/wiki/Cone :return (1/3) * pi * radius^2 * height - >>> vol_right_circ_cone(2, 3) 12.566370614359172 + >>> vol_right_circ_cone(-1, 1) + Traceback (most recent call last): + ... + ValueError: vol_right_circ_cone() only accepts non-negative values + >>> vol_right_circ_cone(1, -1) + Traceback (most recent call last): + ... + ValueError: vol_right_circ_cone() only accepts non-negative values """ + if height < 0 or radius < 0: + raise ValueError("vol_right_circ_cone() only accepts non-negative values") return pi * pow(radius, 2) * height / 3.0 @@ -116,12 +170,21 @@ def vol_prism(area_of_base: float, height: float) -> float: Calculate the Volume of a Prism. Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry) :return V = Bh - >>> vol_prism(10, 2) 20.0 >>> vol_prism(11, 1) 11.0 + >>> vol_prism(-1, 1) + Traceback (most recent call last): + ... + ValueError: vol_prism() only accepts non-negative values + >>> vol_prism(1, -1) + Traceback (most recent call last): + ... + ValueError: vol_prism() only accepts non-negative values """ + if height < 0 or area_of_base < 0: + raise ValueError("vol_prism() only accepts non-negative values") return float(area_of_base * height) @@ -130,12 +193,21 @@ def vol_pyramid(area_of_base: float, height: float) -> float: Calculate the Volume of a Pyramid. Wikipedia reference: https://en.wikipedia.org/wiki/Pyramid_(geometry) :return (1/3) * Bh - >>> vol_pyramid(10, 3) 10.0 >>> vol_pyramid(1.5, 3) 1.5 + >>> vol_pyramid(-1, 1) + Traceback (most recent call last): + ... + ValueError: vol_pyramid() only accepts non-negative values + >>> vol_pyramid(1, -1) + Traceback (most recent call last): + ... + ValueError: vol_pyramid() only accepts non-negative values """ + if height < 0 or area_of_base < 0: + raise ValueError("vol_pyramid() only accepts non-negative values") return area_of_base * height / 3.0 @@ -144,27 +216,36 @@ def vol_sphere(radius: float) -> float: Calculate the Volume of a Sphere. Wikipedia reference: https://en.wikipedia.org/wiki/Sphere :return (4/3) * pi * r^3 - >>> vol_sphere(5) 523.5987755982989 >>> vol_sphere(1) 4.1887902047863905 + >>> vol_sphere(-1) + Traceback (most recent call last): + ... + ValueError: vol_sphere() only accepts non-negative values """ + if radius < 0: + raise ValueError("vol_sphere() only accepts non-negative values") return 4 / 3 * pi * pow(radius, 3) -def vol_hemisphere(radius: float): +def vol_hemisphere(radius: float) -> float: """Calculate the volume of a hemisphere Wikipedia reference: https://en.wikipedia.org/wiki/Hemisphere Other references: https://www.cuemath.com/geometry/hemisphere :return 2/3 * pi * radius^3 - >>> vol_hemisphere(1) 2.0943951023931953 - >>> vol_hemisphere(7) 718.3775201208659 + >>> vol_hemisphere(-1) + Traceback (most recent call last): + ... + ValueError: vol_hemisphere() only accepts non-negative values """ + if radius < 0: + raise ValueError("vol_hemisphere() only accepts non-negative values") return 2 / 3 * pi * pow(radius, 3) @@ -172,26 +253,79 @@ def vol_circular_cylinder(radius: float, height: float) -> float: """Calculate the Volume of a Circular Cylinder. Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder :return pi * radius^2 * height - >>> vol_circular_cylinder(1, 1) 3.141592653589793 >>> vol_circular_cylinder(4, 3) 150.79644737231007 + >>> vol_circular_cylinder(-1, 1) + Traceback (most recent call last): + ... + ValueError: vol_circular_cylinder only accepts non-negative values + >>> vol_circular_cylinder(1, -1) + Traceback (most recent call last): + ... + ValueError: vol_circular_cylinder only accepts non-negative values """ + if height < 0 or radius < 0: + raise ValueError("vol_circular_cylinder() only accepts non-negative values") return pi * pow(radius, 2) * height -def vol_conical_frustum(height: float, radius_1: float, radius_2: float): +def vol_hollow_circular_cylinder( + inner_radius: float, outer_radius: float, height: float +) -> float: + """Calculate the Volume of a Hollow Circular Cylinder. + >>> vol_hollow_circular_cylinder(1, 2, 3) + 28.274333882308138 + >>> vol_hollow_circular_cylinder(-1, 2, 3) + Traceback (most recent call last): + ... + ValueError: vol_hollow_circular_cylinder() only accepts non-negative values + >>> vol_hollow_circular_cylinder(1, -2, 3) + Traceback (most recent call last): + ... + ValueError: vol_hollow_circular_cylinder() only accepts non-negative values + >>> vol_hollow_circular_cylinder(1, 2, -3) + Traceback (most recent call last): + ... + ValueError: vol_hollow_circular_cylinder() only accepts non-negative values + >>> vol_hollow_circular_cylinder(2, 1, 3) + Traceback (most recent call last): + ... + ValueError: outer_radius must be greater than inner_radius + """ + if inner_radius < 0 or outer_radius < 0 or height < 0: + raise ValueError( + "vol_hollow_circular_cylinder() only accepts non-negative values" + ) + if outer_radius <= inner_radius: + raise ValueError("outer_radius must be greater than inner_radius") + return pi * (pow(outer_radius, 2) - pow(inner_radius, 2)) * height + + +def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> float: """Calculate the Volume of a Conical Frustum. Wikipedia reference: https://en.wikipedia.org/wiki/Frustum :return 1/3 * pi * height * (radius_1^2 + radius_top^2 + radius_1 * radius_2) - >>> vol_conical_frustum(45, 7, 28) 48490.482608158454 - >>> vol_conical_frustum(1, 1, 2) 7.330382858376184 + >>> vol_conical_frustum(-2, 2, 1) + Traceback (most recent call last): + ... + ValueError: vol_conical_frustum() only accepts non-negative values + >>> vol_conical_frustum(2, -2, 1) + Traceback (most recent call last): + ... + ValueError: vol_conical_frustum() only accepts non-negative values + >>> vol_conical_frustum(2, 2, -1) + Traceback (most recent call last): + ... + ValueError: vol_conical_frustum() only accepts non-negative values """ + if radius_1 < 0 or radius_2 < 0 or height < 0: + raise ValueError("vol_conical_frustum() only accepts non-negative values") return ( 1 / 3 @@ -213,6 +347,9 @@ def main(): print("Sphere: " + str(vol_sphere(2))) # ~= 33.5 print("Hemisphere: " + str(vol_hemisphere(2))) # ~= 16.75 print("Circular Cylinder: " + str(vol_circular_cylinder(2, 2))) # ~= 25.1 + print( + "Hollow Circular Cylinder: " + str(vol_hollow_circular_cylinder(1, 2, 3)) + ) # ~= 28.3 print("Conical Frustum: " + str(vol_conical_frustum(2, 2, 4))) # ~= 58.6 print("Spherical cap: " + str(vol_spherical_cap(1, 2))) # ~= 5.24 print("Spheres intersetion: " + str(vol_spheres_intersect(2, 2, 1))) # ~= 21.21 From 7030752806e5c17be0f7527d0a76b2e2dccf4e39 Mon Sep 17 00:00:00 2001 From: rohanr18 <114707091+rohanr18@users.noreply.github.com> Date: Sat, 1 Oct 2022 14:08:36 +0530 Subject: [PATCH 2/5] Update volume.py --- maths/volume.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index 3e69f28aa9f8..f27acbdcd5ef 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -260,11 +260,11 @@ def vol_circular_cylinder(radius: float, height: float) -> float: >>> vol_circular_cylinder(-1, 1) Traceback (most recent call last): ... - ValueError: vol_circular_cylinder only accepts non-negative values + ValueError: vol_circular_cylinder() only accepts non-negative values >>> vol_circular_cylinder(1, -1) Traceback (most recent call last): ... - ValueError: vol_circular_cylinder only accepts non-negative values + ValueError: vol_circular_cylinder() only accepts non-negative values """ if height < 0 or radius < 0: raise ValueError("vol_circular_cylinder() only accepts non-negative values") From 2bff5217eb7ab47b433143e226c700519c40d889 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj <114707091+rohanr18@users.noreply.github.com> Date: Wed, 5 Oct 2022 02:02:57 +0530 Subject: [PATCH 3/5] floats, zeroes tests added --- maths/volume.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/maths/volume.py b/maths/volume.py index f27acbdcd5ef..ebc58dae718f 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -14,6 +14,10 @@ def vol_cube(side_length: int | float) -> float: 1.0 >>> vol_cube(3) 27.0 + >>> vol_cube(0) + 0 + >>> vol_cube(1.6) + 4.096000000000001 >>> vol_cube(-1) Traceback (most recent call last): ... @@ -30,6 +34,10 @@ def vol_spherical_cap(height: float, radius: float) -> float: :return 1/3 pi * height ^ 2 * (3 * radius - height) >>> vol_spherical_cap(1, 2) 5.235987755982988 + >>> vol_spherical_cap(1.6, 2.6) + 16.621119532592402 + >>> vol_spherical_cap(0, 0) + 0.0 >>> vol_spherical_cap(-1, 2) Traceback (most recent call last): ... @@ -63,6 +71,10 @@ def vol_spheres_intersect( :return vol_spherical_cap(h1, radius_2) + vol_spherical_cap(h2, radius_1) >>> vol_spheres_intersect(2, 2, 1) 21.205750411731103 + >>> vol_spheres_intersect(2.6, 2.6, 1.6) + 40.71504079052372 + >>> vol_spheres_intersect(0, 0, 0) + 0.0 >>> vol_spheres_intersect(-2, 2, 1) Traceback (most recent call last): ... @@ -103,6 +115,10 @@ def vol_cuboid(width: float, height: float, length: float) -> float: 1.0 >>> vol_cuboid(1, 2, 3) 6.0 + >>> vol_cuboid(1.6, 2.6, 3.6) + 14.976 + >>> vol_cuboid(0, 0, 0) + 0.0 >>> vol_cuboid(-1, 2, 3) Traceback (most recent call last): ... @@ -130,6 +146,10 @@ def vol_cone(area_of_base: float, height: float) -> float: 10.0 >>> vol_cone(1, 1) 0.3333333333333333 + >>> vol_cone(1.6, 1.6) + 0.8533333333333335 + >>> vol_cone(0, 0) + 0.0 >>> vol_cone(-1, 1) Traceback (most recent call last): ... @@ -151,6 +171,10 @@ def vol_right_circ_cone(radius: float, height: float) -> float: :return (1/3) * pi * radius^2 * height >>> vol_right_circ_cone(2, 3) 12.566370614359172 + >>> vol_right_circ_cone(0, 0) + 0.0 + >>> vol_right_circ_cone(1.6, 1.6) + 4.289321169701265 >>> vol_right_circ_cone(-1, 1) Traceback (most recent call last): ... @@ -174,6 +198,10 @@ def vol_prism(area_of_base: float, height: float) -> float: 20.0 >>> vol_prism(11, 1) 11.0 + >>> vol_prism(1.6, 1.6) + 2.5600000000000005 + >>> vol_prism(0, 0) + 0.0 >>> vol_prism(-1, 1) Traceback (most recent call last): ... @@ -197,6 +225,10 @@ def vol_pyramid(area_of_base: float, height: float) -> float: 10.0 >>> vol_pyramid(1.5, 3) 1.5 + >>> vol_pyramid(1.6, 1.6) + 0.8533333333333335 + >>> vol_pyramid(0, 0) + 0.0 >>> vol_pyramid(-1, 1) Traceback (most recent call last): ... @@ -220,6 +252,10 @@ def vol_sphere(radius: float) -> float: 523.5987755982989 >>> vol_sphere(1) 4.1887902047863905 + >>> vol_sphere(1.6) + 17.15728467880506 + >>> vol_sphere(0) + 0.0 >>> vol_sphere(-1) Traceback (most recent call last): ... @@ -239,6 +275,10 @@ def vol_hemisphere(radius: float) -> float: 2.0943951023931953 >>> vol_hemisphere(7) 718.3775201208659 + >>> vol_hemisphere(1.6) + 8.57864233940253 + >>> vol_hemisphere(0) + 0.0 >>> vol_hemisphere(-1) Traceback (most recent call last): ... @@ -257,6 +297,10 @@ def vol_circular_cylinder(radius: float, height: float) -> float: 3.141592653589793 >>> vol_circular_cylinder(4, 3) 150.79644737231007 + >>> vol_circular_cylinder(1.6, 1.6) + 12.867963509103795 + >>> vol_circular_cylinder(0, 0) + 0.0 >>> vol_circular_cylinder(-1, 1) Traceback (most recent call last): ... @@ -277,6 +321,8 @@ def vol_hollow_circular_cylinder( """Calculate the Volume of a Hollow Circular Cylinder. >>> vol_hollow_circular_cylinder(1, 2, 3) 28.274333882308138 + >>> vol_hollow_circular_cylinder(1.6, 2.6, 3.6) + 47.50088092227767 >>> vol_hollow_circular_cylinder(-1, 2, 3) Traceback (most recent call last): ... @@ -290,6 +336,10 @@ def vol_hollow_circular_cylinder( ... ValueError: vol_hollow_circular_cylinder() only accepts non-negative values >>> vol_hollow_circular_cylinder(2, 1, 3) + Traceback (most recent call last): + ... + ValueError: outer_radius must be greater than inner_radius + >>> vol_hollow_circular_cylinder(0, 0, 0) Traceback (most recent call last): ... ValueError: outer_radius must be greater than inner_radius @@ -311,6 +361,10 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa 48490.482608158454 >>> vol_conical_frustum(1, 1, 2) 7.330382858376184 + >>> vol_conical_frustum(1.6, 2.6, 3.6) + 48.7240076620753 + >>> vol_conical_frustum(0, 0, 0) + 0.0 >>> vol_conical_frustum(-2, 2, 1) Traceback (most recent call last): ... From c90f1534634a453fb3d6042a226d1fd2e1fc3071 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj <114707091+rohanr18@users.noreply.github.com> Date: Wed, 5 Oct 2022 02:10:13 +0530 Subject: [PATCH 4/5] Update volume.py --- maths/volume.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index ebc58dae718f..20a8e2dfef4e 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -15,7 +15,7 @@ def vol_cube(side_length: int | float) -> float: >>> vol_cube(3) 27.0 >>> vol_cube(0) - 0 + 0.0 >>> vol_cube(1.6) 4.096000000000001 >>> vol_cube(-1) @@ -339,7 +339,7 @@ def vol_hollow_circular_cylinder( Traceback (most recent call last): ... ValueError: outer_radius must be greater than inner_radius - >>> vol_hollow_circular_cylinder(0, 0, 0) + >>> vol_hollow_circular_cylinder(0, 0, 0) Traceback (most recent call last): ... ValueError: outer_radius must be greater than inner_radius From 4a4c6a20dcb7731b96842752156159f136084539 Mon Sep 17 00:00:00 2001 From: Rohan R Bharadwaj <114707091+rohanr18@users.noreply.github.com> Date: Wed, 5 Oct 2022 02:22:14 +0530 Subject: [PATCH 5/5] f-strings --- maths/volume.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index 20a8e2dfef4e..97c06d7e1c3a 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -392,21 +392,21 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa def main(): """Print the Results of Various Volume Calculations.""" print("Volumes:") - print("Cube: " + str(vol_cube(2))) # = 8 - print("Cuboid: " + str(vol_cuboid(2, 2, 2))) # = 8 - print("Cone: " + str(vol_cone(2, 2))) # ~= 1.33 - print("Right Circular Cone: " + str(vol_right_circ_cone(2, 2))) # ~= 8.38 - print("Prism: " + str(vol_prism(2, 2))) # = 4 - print("Pyramid: " + str(vol_pyramid(2, 2))) # ~= 1.33 - print("Sphere: " + str(vol_sphere(2))) # ~= 33.5 - print("Hemisphere: " + str(vol_hemisphere(2))) # ~= 16.75 - print("Circular Cylinder: " + str(vol_circular_cylinder(2, 2))) # ~= 25.1 + print(f"Cube: {vol_cube(2) = }") # = 8 + print(f"Cuboid: {vol_cuboid(2, 2, 2) = }") # = 8 + print(f"Cone: {vol_cone(2, 2) = }") # ~= 1.33 + print(f"Right Circular Cone: {vol_right_circ_cone(2, 2) = }") # ~= 8.38 + print(f"Prism: {vol_prism(2, 2) = }") # = 4 + print(f"Pyramid: {vol_pyramid(2, 2) = }") # ~= 1.33 + print(f"Sphere: {vol_sphere(2) = }") # ~= 33.5 + print(f"Hemisphere: {vol_hemisphere(2) = }") # ~= 16.75 + print(f"Circular Cylinder: {vol_circular_cylinder(2, 2) = }") # ~= 25.1 print( - "Hollow Circular Cylinder: " + str(vol_hollow_circular_cylinder(1, 2, 3)) + f"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }" ) # ~= 28.3 - print("Conical Frustum: " + str(vol_conical_frustum(2, 2, 4))) # ~= 58.6 - print("Spherical cap: " + str(vol_spherical_cap(1, 2))) # ~= 5.24 - print("Spheres intersetion: " + str(vol_spheres_intersect(2, 2, 1))) # ~= 21.21 + print(f"Conical Frustum: {vol_conical_frustum(2, 2, 4) = }") # ~= 58.6 + print(f"Spherical cap: {vol_spherical_cap(1, 2) = }") # ~= 5.24 + print(f"Spheres intersetion: {vol_spheres_intersect(2, 2, 1) = }") # ~= 21.21 if __name__ == "__main__":