Skip to content

Update ceil and floor function #3710

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
Oct 25, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,8 @@
* [Astar](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/astar.py)
* [Data Transformations](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/data_transformations.py)
* [Decision Tree](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/decision_tree.py)
* Forecasting
* [Run](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/forecasting/run.py)
* [Gaussian Naive Bayes](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gaussian_naive_bayes.py)
* [Gradient Boosting Regressor](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gradient_boosting_regressor.py)
* [Gradient Descent](https://github.com/TheAlgorithms/Python/blob/master/machine_learning/gradient_descent.py)
Expand Down Expand Up @@ -630,6 +632,8 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_036/sol1.py)
* Problem 037
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_037/sol1.py)
* Problem 038
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_038/sol1.py)
* Problem 039
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_039/sol1.py)
* Problem 040
Expand Down Expand Up @@ -716,7 +720,6 @@
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py)
* Problem 551
* [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py)
* [Validate Solutions](https://github.com/TheAlgorithms/Python/blob/master/project_euler/validate_solutions.py)

## Quantum
* [Deutsch Jozsa](https://github.com/TheAlgorithms/Python/blob/master/quantum/deutsch_jozsa.py)
Expand Down
9 changes: 6 additions & 3 deletions maths/ceil.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
"""
https://en.wikipedia.org/wiki/Floor_and_ceiling_functions
"""


def ceil(x) -> int:
"""
Return the ceiling of x as an Integral.
Expand All @@ -10,9 +15,7 @@ def ceil(x) -> int:
... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
True
"""
return (
x if isinstance(x, int) or x - int(x) == 0 else int(x + 1) if x > 0 else int(x)
)
return int(x) if x - int(x) <= 0 else int(x) + 1


if __name__ == "__main__":
Expand Down
11 changes: 6 additions & 5 deletions maths/floor.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
"""
https://en.wikipedia.org/wiki/Floor_and_ceiling_functions
"""


def floor(x) -> int:
"""
Return the floor of x as an Integral.

:param x: the number
:return: the largest integer <= x.

>>> import math
>>> all(floor(n) == math.floor(n) for n
... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
True
"""
return (
x if isinstance(x, int) or x - int(x) == 0 else int(x) if x > 0 else int(x - 1)
)
return int(x) if x - int(x) >= 0 else int(x) - 1


if __name__ == "__main__":
Expand Down