Skip to content

Commit dbbe5a5

Browse files
committed
Minor changes
1 parent 5c352ee commit dbbe5a5

File tree

2 files changed

+45
-31
lines changed

2 files changed

+45
-31
lines changed

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -789,36 +789,52 @@ def longest_common_subsequence(seq1: OneDimensionalArray, seq2: OneDimensionalAr
789789

790790
return OneDimensionalArray(seq1._dtype, check_mat[row][col][-1])
791791

792-
def is_ordered(array, start, end, comp):
792+
def is_ordered(array, **kwargs):
793793
"""
794794
Checks whether the given array is ordered or not.
795795
796796
Parameters
797-
=========
798-
array : OneDimensionalArray
799-
The array which is to be checked for sort.
800-
801-
start : int
802-
The starting index of the portion of array to be checked.
797+
==========
803798
804-
end : int
805-
The ending index of the portion of array to be checked.
799+
array: Array
800+
The array which is to be checked for having
801+
specified ordered among its elements.
802+
start: int
803+
The starting index of the portion
804+
which is to be sorted.
805+
Optional, by default 0
806+
end: int
807+
The ending index of the portion which
808+
is to be sorted.
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 sorting. If the function returns
814+
False then only swapping is performed.
815+
Optional, by default, less than or
816+
equal to is used for comparing two
817+
values.
806818
807819
Examples
808820
========
809821
810822
>>> from pydatastructs import OneDimensionalArray, is_ordered
811-
>>> arr = OneDimensionalArray(int, [4, 3, 2, 1])
812-
>>> is_ordered(arr, 0, 3, None)
813-
False
814-
>>> arr1 = OneDimensionalArray(int, [1, 2, 3])
815-
>>> is_ordered(arr1, 0, 2, None)
823+
>>> arr = OneDimensionalArray(int, [1, 2, 3, 4])
824+
>>> is_ordered(arr)
816825
True
826+
>>> arr1 = OneDimensionalArray(int, [1, 2, 3])
827+
>>> is_ordered(arr1, start=0, end=1, comp=lambda u, v: u > v)
828+
False
817829
818830
"""
819-
if comp is None:
820-
comp = lambda a, b: a < b
821-
for i in range(start + 1, end + 1):
822-
if(comp(array[i], array[i - 1])):
831+
lower = kwargs.get('start', 0)
832+
upper = kwargs.get('end', len(array) - 1)
833+
comp = kwargs.get("comp", lambda u, v: u <= v)
834+
835+
for i in range(lower + 1, upper + 1):
836+
if array[i] is None or array[i - 1] is None:
837+
continue
838+
if comp(array[i], array[i - 1]):
823839
return False
824840
return True

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 11 additions & 13 deletions
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, is_ordered)
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
@@ -129,33 +130,30 @@ def test_longest_common_sequence():
129130

130131
def test_is_ordered():
131132
ODA = OneDimensionalArray
133+
DODA = DynamicOneDimensionalArray
132134

133135
expected_result = True
134-
135136
arr = ODA(int, [1, 2, 5, 6])
136-
output = is_ordered(arr, 0, len(arr)-1, None)
137+
output = is_ordered(arr)
137138
assert output == expected_result
138139

139140
expected_result = False
140-
141141
arr1 = ODA(int, [4, 3, 2, 1])
142-
output = is_ordered(arr1, 0, len(arr1)-1, None)
142+
output = is_ordered(arr1)
143143
assert output == expected_result
144144

145145
expected_result = True
146-
147146
arr2 = ODA(int, [6, 1, 2, 3, 4, 5])
148-
output = is_ordered(arr2, 1, 5, None)
147+
output = is_ordered(arr2, start=1, end=5)
149148
assert output == expected_result
150149

151-
expected_result = False
152-
150+
expected_result = True
153151
arr3 = ODA(int, [0, -1, -2, -3, -4, 4])
154-
output = is_ordered(arr3, 0, 5, None)
152+
output = is_ordered(arr3, start=1, end=4, comp=lambda u, v: u > v)
155153
assert output == expected_result
156154

157155
expected_result = True
158-
159-
arr4 = ODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
160-
output = is_ordered(arr4, 0, 9, None)
156+
arr4 = DODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
157+
arr4.delete(0)
158+
output = is_ordered(arr4)
161159
assert output == expected_result

0 commit comments

Comments
 (0)