-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
def find_floor(arr, n, 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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide descriptive name for the parameter: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide descriptive name for the parameter: Please provide type hint for the parameter: Please provide descriptive name for the parameter: |
||
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]) |
There was a problem hiding this comment.
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 functionfind_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