Skip to content

floor_or_ceil_using_binary_search.py #12075

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

Closed
wants to merge 3 commits into from
Closed
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
48 changes: 48 additions & 0 deletions searches/floor_or_ceil_using_binary_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
def find_floor(arr, n, x):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: find_floor. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file searches/floor_or_ceil_using_binary_search.py, please provide doctest for the function find_floor

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: n

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

low = 0
high = n - 1
ans = -1

while low <= high:
mid = (low + high) // 2
# maybe an answer
if arr[mid] <= x:
ans = arr[mid]
# look for smaller index on the left
low = mid + 1
else:
high = mid - 1 # look on the right

return ans


def find_ceil(arr, n, x):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: find_ceil. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file searches/floor_or_ceil_using_binary_search.py, please provide doctest for the function find_ceil

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: n

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

low = 0
high = n - 1
ans = -1

while low <= high:
mid = (low + high) // 2
# maybe an answer
if arr[mid] >= x:
ans = arr[mid]
# look for smaller index on the left
high = mid - 1
else:
low = mid + 1 # look on the right

return ans


def get_floor_and_ceil(arr, n, x):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: get_floor_and_ceil. If the function does not return a value, please provide the type hint as: def function() -> None:

As there is no test file in this pull request nor any test function or class in the file searches/floor_or_ceil_using_binary_search.py, please provide doctest for the function get_floor_and_ceil

Please provide type hint for the parameter: arr

Please provide type hint for the parameter: n

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

f = find_floor(arr, n, x)
c = find_ceil(arr, n, x)
return (f, c)


# sample test cas
# arr = [3, 4, 4, 7, 8, 10]
# n = 6
# x = 5
# ans = get_floor_and_ceil(arr, n, x)
# print("The floor and ceil are:", ans[0], ans[1])