Last active
October 5, 2019 23:30
-
-
Save gyli/917faad02f9ee8b75d082f7a24640f5a to your computer and use it in GitHub Desktop.
Division with Ceil and floor in Python without math module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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