-
Notifications
You must be signed in to change notification settings - Fork 269
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
upper_bound and lower_bound functions added #351
Conversation
Codecov Report
@@ Coverage Diff @@
## master #351 +/- ##
=============================================
+ Coverage 98.561% 98.574% +0.013%
=============================================
Files 25 25
Lines 3267 3297 +30
=============================================
+ Hits 3220 3250 +30
Misses 47 47
|
@sHiVaNgI821 Please address this #344 (comment) in your PR too |
>>> arr = ODA(int, [7, 6, 5, 5, 4]) | ||
>>> upperBound = upper_bound(arr, 0, 4, 5, lambda x, y: x >= y) | ||
>>> upperBound | ||
4 | ||
|
||
Note | ||
==== | ||
|
||
The OneDimensionalArray must be sorted beforehand | ||
""" | ||
if comp is None: | ||
comp = lambda a, b: (a <= b) |
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.
The order needs to be well defined. i.e, strict inequality is followed in order. You can have a look at the implementation here: https://en.cppreference.com/w/cpp/algorithm/upper_bound.
mid = (start+end)//2 | ||
if comp(array[mid],value): |
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.
mid = (start+end)//2 | |
if comp(array[mid],value): | |
mid = (start + end)//2 | |
if comp(array[mid], value): |
Finds the the index of the first occurence of an element which is not less than value according to an order defined, in the given OneDimensionalArray | ||
|
||
Parameters | ||
======== | ||
array: OneDimensionalArray | ||
The sorted array (sorted according to a custom comparator function) in which the lower bound has to be found | ||
|
||
start: int | ||
The staring index of the portion of the array in which the lower bound of a given value has to be looked for | ||
|
||
end: int | ||
The ending index of the portion of the array in which the lower bound of a given value has to be looked for |
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.
The line width should not exceed 79 characters. Wrap up text in multiple lines not more than 79 chars
output: int | ||
Lower bound of the given value in the given sorted OneDimensionalArray |
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.
output: int | |
Lower bound of the given value in the given sorted OneDimensionalArray | |
output: int | |
Lower bound of the given value in the given sorted OneDimensionalArray |
mid = (start+end)//2 | ||
if comp(array[mid],value): |
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.
mid = (start+end)//2 | |
if comp(array[mid],value): | |
mid = (start + end)//2 | |
if comp(array[mid], value): |
start = mid + 1 | ||
else: | ||
index = mid | ||
end = mid -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.
end = mid -1 | |
end = mid - 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.
This looks good. Just have a look at some suggestions.
output: int | ||
Upper bound of the given value in the given sorted OneDimensionalArray |
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.
Index of upper bound of given value...
output: int | ||
Lower bound of the given value in the given sorted OneDimensionalArray |
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.
Same
output = upper_bound(arr1, 0, len(arr1) - 1, 3, None) | ||
expected_result = 2 | ||
assert expected_result == output |
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.
From, here I think there needs a slight change in the algo. That is output 2
isn't correct mathematically for this in the given range. Hence, we should consider the interval as [start, end). And so, I expect the output of it as 3
. (You can verify it by yourself, we are making it similar to cpp)
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.
Also, after making this interval change, please add tests apart from [0, len(arr)) interval too.
Looks good to me. |
@czgdp1807 Have a look at it |
@sHiVaNgI821 Please resolve merge conflicts |
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.
Apply these suggestions to both functions. Apart from this, it looks good to me.
start: int | ||
The staring index of the portion of the array in which the upper bound | ||
of a given value has to be looked for | ||
|
||
end: int | ||
The ending index of the portion of the array in which the upper bound | ||
of a given value has to be looked for |
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.
start: int | |
The staring index of the portion of the array in which the upper bound | |
of a given value has to be looked for | |
end: int | |
The ending index of the portion of the array in which the upper bound | |
of a given value has to be looked for | |
start: int, optional. | |
The staring index of the portion of the array in which the upper bound | |
of a given value has to be looked for. By default is 0. | |
end: int, optional | |
The ending index of the portion of the array in which the upper bound | |
of a given value has to be looked for. By default is len(array) | |
comp: boolean funtion, optional | |
A function that takes two paramters and returns True if 1st parameter is strictly | |
smaller than 2nd parameter |
# if comp is None: | ||
# comp = lambda a, b: (a < b) |
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.
Remove this comment
Looks Good. Will polish it and push back again. |
@czgdp1807 This can be merged. |
Thanks. |
References to other Issues or PRs or Relevant literature
Added the upper_bound and lower_bound functions to the algorithms library. Upper_bound returns the index of the first occurence of an element greater than value, in the given sorted OneDimensionalArray. Lower_bound returns the index of the first occurence of an element which is not less than value, in the given OneDimensionalArray.
Brief description of what is fixed or changed
Other comments