-
Notifications
You must be signed in to change notification settings - Fork 313
Added a function to check ordering in ODA and DODA #344
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
Conversation
OneDimensionalArray. Appended algorithms file with is_sorted function. This will be useful to check whether the array given is sorted or not.
Codecov Report
@@ Coverage Diff @@
## master #344 +/- ##
=============================================
+ Coverage 98.550% 98.561% +0.010%
=============================================
Files 25 25
Lines 3243 3267 +24
=============================================
+ Hits 3196 3220 +24
Misses 47 47
|
>>> arr = OneDimensionalArray(int,[4,3,2,1]) | ||
>>> is_sorted(arr,0,3) |
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.
>>> arr = OneDimensionalArray(int,[4,3,2,1]) | |
>>> is_sorted(arr,0,3) | |
>>> arr = OneDimensionalArray(int, [4, 3, 2, 1]) | |
>>> is_sorted(arr, 0, 3) |
Make suggested changes wherever appicable. Add a space after comma
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 apply this changes wherever required
for i in range(start+1,end+1): | ||
if(array[i]<array[i-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.
for i in range(start+1,end+1): | |
if(array[i]<array[i-1]): | |
for i in range(start + 1, end + 1): | |
if(array[i] < array[i - 1]): |
This is a bit trivial. I mean checking if an array sorted or not in O(N) time isn't really helpful. For keeping time complexity constant we may need to maintain a state inside ODA class to track when the sort function was called on an array object. More or less it would be just like maintaining the history of the array. We can have this feature as something extra. |
|
||
""" | ||
for i in range(start+1,end+1): | ||
if(array[i]<array[i-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 will not work with DODA
(the dynamic arrays). An operator is defined in _comp
(or something similar) in misc_util.py
. Please use that here.
Moreover, we should allow a custom comparator (defaulting to <=
) to check if an array is ordered according to some criteria specified by the passed comparator. The function can also be renamed to is_ordered
.
What if the user has'nt call sort function and has already given an sorted array as input?
Presently, C++, also uses linear time complexity to check sortedness: http://www.cplusplus.com/reference/algorithm/is_sorted/
This is something that would be good to have instead of using operators directly |
Taking references from https://en.cppreference.com/w/cpp/algorithm/lower_bound and https://en.cppreference.com/w/cpp/algorithm/is_sorted_until: def is_sorted(array, start, end, cmp=None):
# cmp is a function that takes (a, b) and returns true if a is strictly less than b (a<b)
if cmp is None:
cmp = lambda a, b: (a < b) # we will use python defined <
# algo
return True/False Similar thing can be done on #351 |
@@ -787,3 +788,35 @@ def longest_common_subsequence(seq1: OneDimensionalArray, seq2: OneDimensionalAr | |||
check_mat[i][j] = check_mat[i][j-1] | |||
|
|||
return OneDimensionalArray(seq1._dtype, check_mat[row][col][-1]) | |||
|
|||
def is_sorted(array, start, end): |
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 rename this function to is_ordered
and accept an extra parameter comp
.
We should add |
""" | ||
for i in range(start + 1, end + 1): | ||
if(array[i] < array[i - 1]): | ||
return False |
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.
""" | |
for i in range(start + 1, end + 1): | |
if(array[i] < array[i - 1]): | |
return False | |
""" | |
if comp is None: | |
comp = lambda a, b: a < b | |
for i in range(start + 1, end + 1): | |
if(comp(array[i], array[i - 1])): | |
return False |
arr4 = ODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], None) | ||
output = is_ordered(arr4, 0, 9) | ||
assert output == expected_result |
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 add test cases for custom defined comp
@Nora2412 Please use |
If you have trailing whitespace it will show errors. This one will fix unwanted spaces: |
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. Please address few suggestions
arr = ODA(int,[1,2,5,6]) | ||
output = is_sorted(arr,0,len(arr)-1) | ||
assert output == expected_result |
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.
arr = ODA(int,[1,2,5,6]) | |
output = is_sorted(arr,0,len(arr)-1) | |
assert output == expected_result | |
arr = ODA(int, [1, 2, 5, 6]) | |
output = is_sorted(arr, 0, len(arr) - 1) | |
assert output == expected_result |
Apply this change whereever necessary
arr4 = ODA(int,[1,2,3,4,5,6,7,8,9,10]) | ||
output = is_sorted(arr4,0,9) | ||
assert output == expected_result |
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 add tests for user-defined comp
@Smit-create can you please review and let me know if any further changes are required. |
Will merge once the tests pass. |
Appended algorithms file with is_sorted function. This will be useful to
check whether the array given is sorted or not.
References to other Issues or PRs or Relevant literature
Brief description of what is fixed or changed
Other comments