Skip to content
Merged
Changes from all 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
@@ -32,6 +32,7 @@
bucket_sort,
cocktail_shaker_sort,
quick_sort,
longest_common_subsequence
longest_common_subsequence,
is_ordered
)
__all__.extend(algorithms.__all__)
52 changes: 51 additions & 1 deletion pydatastructs/linear_data_structures/algorithms.py
Original file line number Diff line number Diff line change
@@ -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):
@@ -787,3 +788,52 @@ 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, **kwargs):
"""
Checks whether the given array is ordered or not.
Parameters
==========
array: Array
The array which is to be checked for having
specified ordering among its elements.
start: int
The starting index of the portion of the array
under consideration.
Optional, by default 0
end: int
The ending index of the portion of the array
under consideration.
Optional, by default the index
of the last position filled.
comp: lambda/function
The comparator which is to be used
for specifying the desired ordering.
Optional, by default, less than or
equal to is used for comparing two
values.
Examples
========
>>> from pydatastructs import OneDimensionalArray, is_ordered
>>> arr = OneDimensionalArray(int, [1, 2, 3, 4])
>>> is_ordered(arr)
True
>>> arr1 = OneDimensionalArray(int, [1, 2, 3])
>>> is_ordered(arr1, start=0, end=1, comp=lambda u, v: u > v)
False
"""
lower = kwargs.get('start', 0)
upper = kwargs.get('end', len(array) - 1)
comp = kwargs.get("comp", lambda u, v: u <= v)

for i in range(lower + 1, upper + 1):
if array[i] is None or array[i - 1] is None:
continue
if comp(array[i], array[i - 1]):
return False
return True
33 changes: 32 additions & 1 deletion pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
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_ordered)


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

def test_is_ordered():
ODA = OneDimensionalArray
DODA = DynamicOneDimensionalArray

expected_result = True
arr = ODA(int, [1, 2, 5, 6])
output = is_ordered(arr)
assert output == expected_result

expected_result = False
arr1 = ODA(int, [4, 3, 2, 1])
output = is_ordered(arr1)
assert output == expected_result

expected_result = True
arr2 = ODA(int, [6, 1, 2, 3, 4, 5])
output = is_ordered(arr2, start=1, end=5)
assert output == expected_result

expected_result = True
arr3 = ODA(int, [0, -1, -2, -3, -4, 4])
output = is_ordered(arr3, start=1, end=4, comp=lambda u, v: u > v)
assert output == expected_result

expected_result = True
arr4 = DODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
arr4.delete(0)
output = is_ordered(arr4)
assert output == expected_result