From 2b8026a73f846cb5bd04f2d4468f03a193bb79af Mon Sep 17 00:00:00 2001 From: Advik Sharma <70201060+advik-student-dev@users.noreply.github.com> Date: Wed, 12 Oct 2022 20:13:16 -0700 Subject: [PATCH 1/7] Added some more comments added some more comments (to formulas which need it) which make the code more readable and understandable. might make a list of all the formulas on the top, later --- maths/volume.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index 97c06d7e1c3a..8ad2f23ee3ab 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -2,11 +2,14 @@ Find Volumes of Various Shapes. Wikipedia reference: https://en.wikipedia.org/wiki/Volume """ + +# Imports from __future__ import annotations from math import pi, pow +# Functions for calculating volumes of shapes def vol_cube(side_length: int | float) -> float: """ Calculate the Volume of a Cube. @@ -30,8 +33,7 @@ def vol_cube(side_length: int | float) -> float: 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) + Calculate the Volume of the spherical cap >>> vol_spherical_cap(1, 2) 5.235987755982988 >>> vol_spherical_cap(1.6, 2.6) @@ -47,6 +49,7 @@ def vol_spherical_cap(height: float, radius: float) -> float: ... ValueError: vol_spherical_cap() only accepts non-negative values """ + # Volume - 1/3 pi * height squared * (3 * radius - height) 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) @@ -261,6 +264,7 @@ def vol_sphere(radius: float) -> float: ... ValueError: vol_sphere() only accepts non-negative values """ + # Volume - radius cubed * pi * 4/3 if radius < 0: raise ValueError("vol_sphere() only accepts non-negative values") return 4 / 3 * pi * pow(radius, 3) @@ -284,6 +288,7 @@ def vol_hemisphere(radius: float) -> float: ... ValueError: vol_hemisphere() only accepts non-negative values """ + # Volume - 4/3 * pi * radius cubed if radius < 0: raise ValueError("vol_hemisphere() only accepts non-negative values") return 2 / 3 * pi * pow(radius, 3) @@ -310,6 +315,7 @@ def vol_circular_cylinder(radius: float, height: float) -> float: ... ValueError: vol_circular_cylinder() only accepts non-negative values """ + # Volume - radius squared * height * pi if height < 0 or radius < 0: raise ValueError("vol_circular_cylinder() only accepts non-negative values") return pi * pow(radius, 2) * height @@ -344,6 +350,7 @@ def vol_hollow_circular_cylinder( ... ValueError: outer_radius must be greater than inner_radius """ + # Volume - (outer_radius squared - inner_radius squared) * pi * height if inner_radius < 0 or outer_radius < 0 or height < 0: raise ValueError( "vol_hollow_circular_cylinder() only accepts non-negative values" @@ -356,7 +363,7 @@ def vol_hollow_circular_cylinder( 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) @@ -378,6 +385,7 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa ... ValueError: vol_conical_frustum() only accepts non-negative values """ + # Volume - 1/3 * pi * height * (radius_1 squared + radius_2 squared + radius_1 * radius_2) if radius_1 < 0 or radius_2 < 0 or height < 0: raise ValueError("vol_conical_frustum() only accepts non-negative values") return ( From 722fb7dba1e8f29107e1e9e7abb6a4526366b536 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 03:15:35 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/volume.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/volume.py b/maths/volume.py index 8ad2f23ee3ab..ac84c4432532 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -363,7 +363,7 @@ def vol_hollow_circular_cylinder( 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 - + >>> vol_conical_frustum(45, 7, 28) 48490.482608158454 >>> vol_conical_frustum(1, 1, 2) From 0c718d2d95fdbefc4ace5ce14f4141bb76dc0aac Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 13 Oct 2022 08:54:54 +0200 Subject: [PATCH 3/7] Apply suggestions from code review --- maths/volume.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index ac84c4432532..4bba47e5c821 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -1,15 +1,13 @@ """ -Find Volumes of Various Shapes. -Wikipedia reference: https://en.wikipedia.org/wiki/Volume +Find the volume of various shapes. +* https://en.wikipedia.org/wiki/Volume +* https://en.wikipedia.org/wiki/Spherical_cap """ - -# Imports from __future__ import annotations from math import pi, pow -# Functions for calculating volumes of shapes def vol_cube(side_length: int | float) -> float: """ Calculate the Volume of a Cube. @@ -33,7 +31,7 @@ def vol_cube(side_length: int | float) -> float: def vol_spherical_cap(height: float, radius: float) -> float: """ - Calculate the Volume of the spherical cap + Calculate the volume of the spherical cap. >>> vol_spherical_cap(1, 2) 5.235987755982988 >>> vol_spherical_cap(1.6, 2.6) @@ -49,9 +47,9 @@ def vol_spherical_cap(height: float, radius: float) -> float: ... ValueError: vol_spherical_cap() only accepts non-negative values """ - # Volume - 1/3 pi * height squared * (3 * radius - height) if height < 0 or radius < 0: raise ValueError("vol_spherical_cap() only accepts non-negative values") + # Volume is 1/3 pi * height squared * (3 * radius - height) return 1 / 3 * pi * pow(height, 2) * (3 * radius - height) @@ -264,10 +262,10 @@ def vol_sphere(radius: float) -> float: ... ValueError: vol_sphere() only accepts non-negative values """ - # Volume - radius cubed * pi * 4/3 if radius < 0: raise ValueError("vol_sphere() only accepts non-negative values") - return 4 / 3 * pi * pow(radius, 3) + # Volume is radius cubed * pi * 4/3 + return pow(radius, 3) * pi * 4 / 3 def vol_hemisphere(radius: float) -> float: @@ -288,10 +286,10 @@ def vol_hemisphere(radius: float) -> float: ... ValueError: vol_hemisphere() only accepts non-negative values """ - # Volume - 4/3 * pi * radius cubed if radius < 0: raise ValueError("vol_hemisphere() only accepts non-negative values") - return 2 / 3 * pi * pow(radius, 3) + # Volume is radius cubed * pi * 2/3 + return pow(radius, 3) * pi * 2 / 3 def vol_circular_cylinder(radius: float, height: float) -> float: @@ -315,10 +313,10 @@ def vol_circular_cylinder(radius: float, height: float) -> float: ... ValueError: vol_circular_cylinder() only accepts non-negative values """ - # Volume - radius squared * height * pi if height < 0 or radius < 0: raise ValueError("vol_circular_cylinder() only accepts non-negative values") - return pi * pow(radius, 2) * height + # Volume is radius squared * height * pi + return pow(radius, 2) * height * pi def vol_hollow_circular_cylinder( From 4f0cc17be26fc0dccfe9131bc7559aa6fc64effd Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 13 Oct 2022 09:03:19 +0200 Subject: [PATCH 4/7] The order changes the result --- maths/volume.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index 4bba47e5c821..989bbd1b3794 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -264,8 +264,8 @@ def vol_sphere(radius: float) -> float: """ if radius < 0: raise ValueError("vol_sphere() only accepts non-negative values") - # Volume is radius cubed * pi * 4/3 - return pow(radius, 3) * pi * 4 / 3 + # Volume is 4/3 * pi * radius cubed + return 4 / 3 * pi * pow(radius, 3) def vol_hemisphere(radius: float) -> float: From 9f378645a593155e4cf61c48ad42406618d286ce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 13 Oct 2022 07:06:23 +0000 Subject: [PATCH 5/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/volume.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/volume.py b/maths/volume.py index 989bbd1b3794..9e563dcc5be3 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -265,7 +265,7 @@ def vol_sphere(radius: float) -> float: if radius < 0: raise ValueError("vol_sphere() only accepts non-negative values") # Volume is 4/3 * pi * radius cubed - return 4 / 3 * pi * pow(radius, 3) + return 4 / 3 * pi * pow(radius, 3) def vol_hemisphere(radius: float) -> float: From ef25838de97e9a311ef7be575d3c5513726159bd Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 16 Oct 2022 10:14:12 +0200 Subject: [PATCH 6/7] Fix long line --- maths/volume.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/maths/volume.py b/maths/volume.py index 9e563dcc5be3..cb7df50f3fe9 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -276,7 +276,7 @@ def vol_hemisphere(radius: float) -> float: >>> vol_hemisphere(1) 2.0943951023931953 >>> vol_hemisphere(7) - 718.3775201208659 + 718.377520120866 >>> vol_hemisphere(1.6) 8.57864233940253 >>> vol_hemisphere(0) @@ -383,7 +383,8 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa ... ValueError: vol_conical_frustum() only accepts non-negative values """ - # Volume - 1/3 * pi * height * (radius_1 squared + radius_2 squared + radius_1 * radius_2) + # Volume is 1/3 * pi * height * + # (radius_1 squared + radius_2 squared + radius_1 * radius_2) if radius_1 < 0 or radius_2 < 0 or height < 0: raise ValueError("vol_conical_frustum() only accepts non-negative values") return ( From 3971eebeb1f177f1c5b74580810f0f3add04e682 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 16 Oct 2022 08:15:23 +0000 Subject: [PATCH 7/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/volume.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/volume.py b/maths/volume.py index cb7df50f3fe9..a594e1b90feb 100644 --- a/maths/volume.py +++ b/maths/volume.py @@ -383,7 +383,7 @@ def vol_conical_frustum(height: float, radius_1: float, radius_2: float) -> floa ... ValueError: vol_conical_frustum() only accepts non-negative values """ - # Volume is 1/3 * pi * height * + # Volume is 1/3 * pi * height * # (radius_1 squared + radius_2 squared + radius_1 * radius_2) if radius_1 < 0 or radius_2 < 0 or height < 0: raise ValueError("vol_conical_frustum() only accepts non-negative values")