Skip to content

Commit a9ddc61

Browse files
kvadrikpre-commit-ci[bot]cclauss
authored
Update area.py (#14307)
* Update area.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update area.py Added functions for cone and cylinder lateral surface areas * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor: remove unnecessary empty iterable in deque (RUF037) Clean up trailing whitespace and empty list in deque initialization to satisfy Ruff linting rules. * style: update jump_search to use PEP 695 type parameters Updated the jump_search function signature to use the Python 3.12+ generic type parameter syntax [T]. This resolves the UP047 linting error flagged by Ruff. * style: update valid_input to use Python 3.12 type parameters Applied PEP 695 syntax to the valid_input function by moving the 'num' generic into brackets. This resolves the UP047 error in the machine_learning directory. * fix(types): add Comparable bound to jump_search type parameter Updated the PEP 695 type parameter syntax to include the 'Comparable' bound. This resolves the Mypy error regarding unsupported left operand types for the '<' operator, ensuring that the generic type T supports comparisons. * Extra empty space removal Removed an extra empty line at the end of the file. * Update area.py Added a function to calculate the surface area of a regular prism * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update area.py Corrected line length * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Apply batched suggestions from code review Co-authored-by: Christian Clauss <cclauss@me.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent ced4c09 commit a9ddc61

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

‎maths/area.py‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,32 @@ def surface_area_cone(radius: float, height: float) -> float:
135135
return pi * radius * (radius + (height**2 + radius**2) ** 0.5)
136136

137137

138+
def lateral_surface_cone(radius: float, height: float) -> float:
139+
"""
140+
Calculate the Lateral Surface Area of a Cone.
141+
Wikipedia reference: https://en.wikipedia.org/wiki/Cone
142+
Formula: pi * r * (h ** 2 + r ** 2) ** 0.5
143+
144+
>>> lateral_surface_cone(10, 20)
145+
702.4814731040726
146+
>>> lateral_surface_cone(6, 8)
147+
188.49555921538757
148+
>>> lateral_surface_cone(0, 0)
149+
0.0
150+
>>> lateral_surface_cone(1, -2)
151+
Traceback (most recent call last):
152+
...
153+
ValueError: lateral_surface_cone() only accepts non-negative values
154+
>>> lateral_surface_cone(-1, 2)
155+
Traceback (most recent call last):
156+
...
157+
ValueError: lateral_surface_cone() only accepts non-negative values
158+
"""
159+
if radius < 0 or height < 0:
160+
raise ValueError("lateral_surface_cone() only accepts non-negative values")
161+
return pi * radius * (height**2 + radius**2) ** 0.5
162+
163+
138164
def surface_area_conical_frustum(
139165
radius_1: float, radius_2: float, height: float
140166
) -> float:
@@ -202,6 +228,32 @@ def surface_area_cylinder(radius: float, height: float) -> float:
202228
return 2 * pi * radius * (height + radius)
203229

204230

231+
def lateral_surface_cylinder(radius: float, height: float) -> float:
232+
"""
233+
Calculate the Lateral Surface Area of a Cylinder.
234+
Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
235+
Formula: 2 * pi * r * h
236+
237+
>>> lateral_surface_cylinder(10, 20)
238+
1256.6370614359173
239+
>>> lateral_surface_cylinder(6, 8)
240+
301.59289474462014
241+
>>> lateral_surface_cylinder(0, 0)
242+
0.0
243+
>>> lateral_surface_cylinder(1, -2)
244+
Traceback (most recent call last):
245+
...
246+
ValueError: lateral_surface_cylinder() only accepts non-negative values
247+
>>> lateral_surface_cylinder(-1, 2)
248+
Traceback (most recent call last):
249+
...
250+
ValueError: lateral_surface_cylinder() only accepts non-negative values
251+
"""
252+
if radius < 0 or height < 0:
253+
raise ValueError("lateral_surface_cylinder() only accepts non-negative values")
254+
return 2 * pi * radius * height
255+
256+
205257
def surface_area_torus(torus_radius: float, tube_radius: float) -> float:
206258
"""Calculate the Area of a Torus.
207259
Wikipedia reference: https://en.wikipedia.org/wiki/Torus
@@ -554,6 +606,50 @@ def area_reg_polygon(sides: int, length: float) -> float:
554606
return (sides * length**2) / (4 * tan(pi / sides))
555607

556608

609+
def surface_area_reg_prism(sides: int, edge: float, height: float) -> float:
610+
"""
611+
Calculate the surface area of a regular prism.
612+
Wikipedia reference: https://en.wikipedia.org/wiki/Prism
613+
Formula: 2*S(base) + n*a*h
614+
615+
>>> surface_area_reg_prism(3, 10, 20)
616+
686.6025403784439
617+
>>> surface_area_reg_prism(4, 10, 10)
618+
600.0
619+
>>> surface_area_reg_prism(2, 10, 15)
620+
Traceback (most recent call last):
621+
...
622+
ValueError: surface_area_reg_prism() only accepts integers greater than or equal \
623+
to three as number of sides
624+
>>> surface_area_reg_prism(5, -2, 3)
625+
Traceback (most recent call last):
626+
...
627+
ValueError: surface_area_reg_prism() only accepts non-negative values as \
628+
length of an edge or height
629+
>>> surface_area_reg_prism(5, 2, -3)
630+
Traceback (most recent call last):
631+
...
632+
ValueError: surface_area_reg_prism() only accepts non-negative values as \
633+
length of an edge or height
634+
>>> surface_area_reg_prism(2, -10, 15)
635+
Traceback (most recent call last):
636+
...
637+
ValueError: surface_area_reg_prism() only accepts integers greater than or equal \
638+
to three as number of sides
639+
"""
640+
if not isinstance(sides, int) or sides < 3:
641+
raise ValueError(
642+
"surface_area_reg_prism() only accepts integers greater than or \
643+
equal to three as number of sides"
644+
)
645+
elif edge < 0 or height < 0:
646+
raise ValueError(
647+
"surface_area_reg_prism() only accepts non-negative values as \
648+
length of an edge or height"
649+
)
650+
return 2 * area_reg_polygon(sides, edge) + sides * edge * height
651+
652+
557653
if __name__ == "__main__":
558654
import doctest
559655

@@ -575,9 +671,12 @@ def area_reg_polygon(sides: int, length: float) -> float:
575671
print(f"Sphere: {surface_area_sphere(20) = }")
576672
print(f"Hemisphere: {surface_area_hemisphere(20) = }")
577673
print(f"Cone: {surface_area_cone(10, 20) = }")
674+
print(f"Cone, lateral: {lateral_surface_cone(10, 20) = }")
578675
print(f"Conical Frustum: {surface_area_conical_frustum(10, 20, 30) = }")
579676
print(f"Cylinder: {surface_area_cylinder(10, 20) = }")
677+
print(f"Cylinder, lateral: {lateral_surface_cylinder(10, 20) = }")
580678
print(f"Torus: {surface_area_torus(20, 10) = }")
581679
print(f"Equilateral Triangle: {area_reg_polygon(3, 10) = }")
582680
print(f"Square: {area_reg_polygon(4, 10) = }")
583681
print(f"Reqular Pentagon: {area_reg_polygon(5, 10) = }")
682+
print(f"Regular Pentagonal Prism: {surface_area_reg_prism(5, 10, 15) = }")

0 commit comments

Comments
 (0)