Skip to content

Instantly share code, notes, and snippets.

@gyli
Last active October 5, 2019 23:30
Show Gist options
  • Save gyli/917faad02f9ee8b75d082f7a24640f5a to your computer and use it in GitHub Desktop.
Save gyli/917faad02f9ee8b75d082f7a24640f5a to your computer and use it in GitHub Desktop.
Division with Ceil and floor in Python without math module
def division(x: int, y: int, method: str = 'float') -> Union[int, float]:
"""
:param x: dividend
:param y: divisor
:param method: {'float', 'round', 'ceil', 'floor'}
"""
method_mapping = {
'float': lambda a, b: a / b,
'nearest': lambda a, b: round(a / b),
'ceil': lambda a, b: int(a // b + bool(a % b)),
'floor': lambda a, b: a // b,
}
return method_mapping[method](x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment