Skip to content

Commit 5488001

Browse files
authored
Added a function to check ordering in ODA and DODA (#344)
1 parent e95bb5c commit 5488001

File tree

3 files changed

+85
-3
lines changed

3 files changed

+85
-3
lines changed

pydatastructs/linear_data_structures/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
bucket_sort,
3333
cocktail_shaker_sort,
3434
quick_sort,
35-
longest_common_subsequence
35+
longest_common_subsequence,
36+
is_ordered
3637
)
3738
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
'bucket_sort',
1515
'cocktail_shaker_sort',
1616
'quick_sort',
17-
'longest_common_subsequence'
17+
'longest_common_subsequence',
18+
'is_ordered'
1819
]
1920

2021
def _merge(array, sl, el, sr, er, end, comp):
@@ -787,3 +788,52 @@ def longest_common_subsequence(seq1: OneDimensionalArray, seq2: OneDimensionalAr
787788
check_mat[i][j] = check_mat[i][j-1]
788789

789790
return OneDimensionalArray(seq1._dtype, check_mat[row][col][-1])
791+
792+
def is_ordered(array, **kwargs):
793+
"""
794+
Checks whether the given array is ordered or not.
795+
796+
Parameters
797+
==========
798+
799+
array: Array
800+
The array which is to be checked for having
801+
specified ordering among its elements.
802+
start: int
803+
The starting index of the portion of the array
804+
under consideration.
805+
Optional, by default 0
806+
end: int
807+
The ending index of the portion of the array
808+
under consideration.
809+
Optional, by default the index
810+
of the last position filled.
811+
comp: lambda/function
812+
The comparator which is to be used
813+
for specifying the desired ordering.
814+
Optional, by default, less than or
815+
equal to is used for comparing two
816+
values.
817+
818+
Examples
819+
========
820+
821+
>>> from pydatastructs import OneDimensionalArray, is_ordered
822+
>>> arr = OneDimensionalArray(int, [1, 2, 3, 4])
823+
>>> is_ordered(arr)
824+
True
825+
>>> arr1 = OneDimensionalArray(int, [1, 2, 3])
826+
>>> is_ordered(arr1, start=0, end=1, comp=lambda u, v: u > v)
827+
False
828+
829+
"""
830+
lower = kwargs.get('start', 0)
831+
upper = kwargs.get('end', len(array) - 1)
832+
comp = kwargs.get("comp", lambda u, v: u <= v)
833+
834+
for i in range(lower + 1, upper + 1):
835+
if array[i] is None or array[i - 1] is None:
836+
continue
837+
if comp(array[i], array[i - 1]):
838+
return False
839+
return True

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from pydatastructs import (
22
merge_sort_parallel, DynamicOneDimensionalArray,
33
OneDimensionalArray, brick_sort, brick_sort_parallel,
4-
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort, cocktail_shaker_sort, quick_sort, longest_common_subsequence)
4+
heapsort, matrix_multiply_parallel, counting_sort, bucket_sort,
5+
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered)
56

67

78
from pydatastructs.utils.raises_util import raises
@@ -126,3 +127,33 @@ def test_longest_common_sequence():
126127
Z = ODA(int, [])
127128
output = longest_common_subsequence(Y, Z)
128129
assert str(output) == '[]'
130+
131+
def test_is_ordered():
132+
ODA = OneDimensionalArray
133+
DODA = DynamicOneDimensionalArray
134+
135+
expected_result = True
136+
arr = ODA(int, [1, 2, 5, 6])
137+
output = is_ordered(arr)
138+
assert output == expected_result
139+
140+
expected_result = False
141+
arr1 = ODA(int, [4, 3, 2, 1])
142+
output = is_ordered(arr1)
143+
assert output == expected_result
144+
145+
expected_result = True
146+
arr2 = ODA(int, [6, 1, 2, 3, 4, 5])
147+
output = is_ordered(arr2, start=1, end=5)
148+
assert output == expected_result
149+
150+
expected_result = True
151+
arr3 = ODA(int, [0, -1, -2, -3, -4, 4])
152+
output = is_ordered(arr3, start=1, end=4, comp=lambda u, v: u > v)
153+
assert output == expected_result
154+
155+
expected_result = True
156+
arr4 = DODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
157+
arr4.delete(0)
158+
output = is_ordered(arr4)
159+
assert output == expected_result

0 commit comments

Comments
 (0)