Skip to content

update volumes with type hints + some refactoring #1353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 5, 2020
Merged
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
98 changes: 67 additions & 31 deletions maths/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,116 @@

Wikipedia reference: https://en.wikipedia.org/wiki/Volume
"""
from typing import Union
from math import pi, pow

from math import pi

def vol_cube(side_length: Union[int, float]) -> float:
"""
Calculate the Volume of a Cube.

def vol_cube(side_length):
"""Calculate the Volume of a Cube."""
# Cube side_length.
return float(side_length ** 3)
>>> vol_cube(1)
1.0
>>> vol_cube(3)
27.0
"""
return pow(side_length, 3)


def vol_cuboid(width, height, length):
"""Calculate the Volume of a Cuboid."""
# Multiply lengths together.
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
"""
return float(width * height * length)


def vol_cone(area_of_base, height):
def vol_cone(area_of_base: float, height: float) -> float:
"""
Calculate the Volume of a Cone.

Wikipedia reference: https://en.wikipedia.org/wiki/Cone
volume = (1/3) * area_of_base * height
:return (1/3) * area_of_base * height

>>> vol_cone(10, 3)
10.0
>>> vol_cone(1, 1)
0.3333333333333333
"""
return (float(1) / 3) * area_of_base * height
return area_of_base * height / 3.0


def vol_right_circ_cone(radius, height):
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
volume = (1/3) * pi * radius^2 * height
"""
:return (1/3) * pi * radius^2 * height

return (float(1) / 3) * pi * (radius ** 2) * height
>>> vol_right_circ_cone(2, 3)
12.566370614359172
"""
return pi * pow(radius, 2) * height / 3.0


def vol_prism(area_of_base, height):
def vol_prism(area_of_base: float, height: float) -> float:
"""
Calculate the Volume of a Prism.

V = Bh
Wikipedia reference: https://en.wikipedia.org/wiki/Prism_(geometry)
:return V = Bh

>>> vol_prism(10, 2)
20.0
>>> vol_prism(11, 1)
11.0
"""
return float(area_of_base * height)


def vol_pyramid(area_of_base, height):
def vol_pyramid(area_of_base: float, height: float) -> float:
"""
Calculate the Volume of a Prism.

V = (1/3) * Bh
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
"""
return (float(1) / 3) * area_of_base * height
return area_of_base * height / 3.0


def vol_sphere(radius):
def vol_sphere(radius: float) -> float:
"""
Calculate the Volume of a Sphere.

V = (4/3) * pi * r^3
Wikipedia reference: https://en.wikipedia.org/wiki/Sphere
:return (4/3) * pi * r^3

>>> vol_sphere(5)
523.5987755982989
>>> vol_sphere(1)
4.1887902047863905
"""
return (float(4) / 3) * pi * radius ** 3
return 4 / 3 * pi * pow(radius, 3)


def vol_circular_cylinder(radius, height):
def vol_circular_cylinder(radius: float, height: float) -> float:
"""Calculate the Volume of a Circular Cylinder.

Wikipedia reference: https://en.wikipedia.org/wiki/Cylinder
volume = pi * radius^2 * height
:return pi * radius^2 * height

>>> vol_circular_cylinder(1, 1)
3.141592653589793
>>> vol_circular_cylinder(4, 3)
150.79644737231007
"""
return pi * radius ** 2 * height
return pi * pow(radius, 2) * height


def main():
Expand Down