-
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
82f69fd
upper_bound and lower_bound functions added
sHiVaNgI821 03adc0d
upper_bound and lower_bound functions changed
sHiVaNgI821 bee4f49
upper_bound updated and refactored
sHiVaNgI821 ac94fbc
minor refactoring
sHiVaNgI821 f412327
interval changed from closed to semi-open
sHiVaNgI821 086606b
merged conflicts
sHiVaNgI821 275906d
resolved merge conflicts
sHiVaNgI821 750b7f6
Merge branch 'shivangi' of https://github.com/sHiVaNgI821/pydatastruc…
sHiVaNgI821 d83de0c
variable arguements added
sHiVaNgI821 9bfad38
minor refactoring
sHiVaNgI821 842b0d7
updated docstring
sHiVaNgI821 25ea3cd
polished
czgdp1807 523344e
Apply suggestions from code review
czgdp1807 c676829
Apply suggestions from code review
czgdp1807 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ | |
'cocktail_shaker_sort', | ||
'quick_sort', | ||
'longest_common_subsequence', | ||
'upper_bound', | ||
'lower_bound', | ||
'is_ordered' | ||
] | ||
|
||
|
@@ -843,3 +845,123 @@ def is_ordered(array, **kwargs): | |
if comp(array[i], array[i - 1]): | ||
return False | ||
return True | ||
|
||
def upper_bound(array, value, **kwargs): | ||
""" | ||
Finds the index of the first occurence of an element greater than value according | ||
to an order defined,in the given sorted OneDimensionalArray | ||
|
||
Parameters | ||
======== | ||
array: OneDimensionalArray | ||
The sorted array (sorted according to a custom comparator function) in which the | ||
upper bound has to be found | ||
|
||
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 | ||
|
||
Returns | ||
======= | ||
|
||
output: int | ||
czgdp1807 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Index of the upper bound of the given value in the given sorted OneDimensionalArray | ||
czgdp1807 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Examples | ||
======== | ||
>>> from pydatastructs import upper_bound, OneDimensionalArray as ODA | ||
>>> arr = ODA(int, [4, 5, 5, 6, 7]) | ||
>>> upperBound = upper_bound(arr, 5, start = 0, end = 4) | ||
>>> upperBound | ||
3 | ||
>>> arr = ODA(int, [7, 6, 5, 5, 4]) | ||
>>> upperBound = upper_bound(arr, 5, comp = lambda x, y: x > y) | ||
>>> upperBound | ||
4 | ||
|
||
Note | ||
==== | ||
|
||
The OneDimensionalArray must be sorted beforehand | ||
""" | ||
start = kwargs.get('start', 0) | ||
end = kwargs.get('end', len(array)) | ||
comp = kwargs.get('comp', lambda x,y: x < y) | ||
# if comp is None: | ||
# comp = lambda a, b: (a < b) | ||
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. Remove this comment |
||
index = end | ||
inclusive_end = end - 1 | ||
if comp(value, array[start]): | ||
index = start | ||
while start <= inclusive_end: | ||
mid = (start + inclusive_end)//2 | ||
if not comp(value, array[mid]): | ||
start = mid + 1 | ||
else: | ||
index = mid | ||
inclusive_end = mid - 1 | ||
return index | ||
|
||
def lower_bound(array, value, **kwargs): | ||
""" | ||
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 | ||
|
||
Returns | ||
======= | ||
output: int | ||
czgdp1807 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Index of the lower bound of the given value in the given sorted OneDimensionalArray | ||
|
||
Examples | ||
======== | ||
|
||
>>> from pydatastructs import lower_bound, OneDimensionalArray as ODA | ||
>>> arr = ODA(int, [4, 5, 5, 6, 7]) | ||
>>> lowerBound = lower_bound(arr, 5, end = 4, comp = lambda x, y : x < y) | ||
>>> lowerBound | ||
1 | ||
>>> arr = ODA(int, [7, 6, 5, 5, 4]) | ||
>>> lowerBound = lower_bound(arr, 5, start = 0, comp = lambda x, y : x > y) | ||
>>> lowerBound | ||
2 | ||
|
||
Note | ||
==== | ||
|
||
The OneDimensionalArray must be sorted beforehand | ||
""" | ||
start = kwargs.get('start', 0) | ||
end = kwargs.get('end', len(array)) | ||
comp = kwargs.get('comp', lambda x, y: x < y) | ||
# if comp is None: | ||
# comp = lambda a, b: (a < b) | ||
index = end | ||
inclusive_end = end - 1 | ||
if not comp(array[start], value): | ||
index = start | ||
while start <= inclusive_end: | ||
mid = (start + inclusive_end)//2 | ||
if comp(array[mid], value): | ||
start = mid + 1 | ||
else: | ||
index = mid | ||
inclusive_end = mid - 1 | ||
return index |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.