Skip to content

Commit ced4c09

Browse files
kvadrikpre-commit-ci[bot]cclauss
authored
Update volume.py (#14291)
* Update volume.py * [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 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. * style: update generic functions to Python 3.12 syntax (UP047) Refactor jump_search to use PEP 695 type parameters [T] as required by the latest Ruff configuration. * 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. * 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 fff3acb commit ced4c09

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

maths/volume.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,32 @@ def vol_torus(torus_radius: float, tube_radius: float) -> float:
510510
return 2 * pow(pi, 2) * torus_radius * pow(tube_radius, 2)
511511

512512

513+
def vol_octahedron(oct_side: float) -> float:
514+
"""
515+
| Calculate the Volume of an Octahedron.
516+
| Wikipedia reference: https://en.wikipedia.org/wiki/Regular_octahedron
517+
518+
>>> from math import isclose
519+
>>> isclose(vol_octahedron(2.5), 7.36569563735987)
520+
True
521+
>>> isclose(vol_octahedron(10), 471.40452079103168)
522+
True
523+
>>> isclose(vol_octahedron(5), 58.92556509887896)
524+
True
525+
>>> isclose(vol_octahedron(3.5), 20.21146882891548)
526+
True
527+
>>> vol_octahedron(0)
528+
0.0
529+
>>> vol_octahedron(-1)
530+
Traceback (most recent call last):
531+
...
532+
ValueError: vol_octahedron() only accepts non-negative values
533+
"""
534+
if oct_side < 0:
535+
raise ValueError("vol_octahedron() only accepts non-negative values")
536+
return oct_side**3 * 2**0.5 / 3
537+
538+
513539
def vol_icosahedron(tri_side: float) -> float:
514540
"""
515541
| Calculate the Volume of an Icosahedron.
@@ -560,6 +586,7 @@ def main():
560586
print(
561587
f"Hollow Circular Cylinder: {vol_hollow_circular_cylinder(1, 2, 3) = }"
562588
) # ~= 28.3
589+
print(f"Octahedron: {vol_octahedron(2.5) = }") # ~=7.37
563590
print(f"Icosahedron: {vol_icosahedron(2.5) = }") # ~=34.09
564591

565592

0 commit comments

Comments
 (0)