Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
bucket_sort,
cocktail_shaker_sort,
quick_sort,
longest_common_subsequence
longest_common_subsequence,
is_ordered
)
__all__.extend(algorithms.__all__)
38 changes: 37 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
'bucket_sort',
'cocktail_shaker_sort',
'quick_sort',
'longest_common_subsequence'
'longest_common_subsequence',
'is_ordered'
]

def _merge(array, sl, el, sr, er, end, comp):
Expand Down Expand Up @@ -787,3 +788,38 @@ 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_ordered(array, start, end, comp):
"""
Checks whether the given array is sorted or not.

Parameters
=========
array : OneDimensionalArray
The array which is to be checked for sort.

start : int
The starting index of the portion of array to be checked.

end : int
The ending index of the portion of array to be checked.

Examples
========

>>> from pydatastructs import OneDimensionalArray, is_ordered
>>> arr = OneDimensionalArray(int, [4, 3, 2, 1])
>>> is_ordered(arr, 0, 3, None)
False
>>> arr1 = OneDimensionalArray(int, [1, 2, 3])
>>> is_ordered(arr1, 0, 2, None)
True

"""
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
return True
35 changes: 34 additions & 1 deletion pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pydatastructs import (
merge_sort_parallel, DynamicOneDimensionalArray,
OneDimensionalArray, brick_sort, brick_sort_parallel,
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort, longest_common_subsequence)
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_sorted)


from pydatastructs.utils.raises_util import raises
Expand Down Expand Up @@ -126,3 +126,36 @@ def test_longest_common_sequence():
Z = ODA(int, [])
output = longest_common_subsequence(Y, Z)
assert str(output) == '[]'

def test_is_sorted():
ODA = OneDimensionalArray

expected_result = True

arr = ODA(int,[1,2,5,6])
output = is_sorted(arr,0,len(arr)-1)
assert output == expected_result
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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


expected_result = False

arr1 = ODA(int,[4,3,2,1])
output = is_sorted(arr1,0,len(arr1)-1)
assert output == expected_result

expected_result = True

arr2 = ODA(int,[6,1,2,3,4,5])
output = is_sorted(arr2,1,5)
assert output == expected_result

expected_result = False

arr3 = ODA(int,[0,-1,-2,-3,-4,4])
output = is_sorted(arr3,0,5)
assert output == expected_result

expected_result = True

arr4 = ODA(int,[1,2,3,4,5,6,7,8,9,10])
output = is_sorted(arr4,0,9)
assert output == expected_result
Copy link
Member

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