Skip to content

Commit 153ce51

Browse files
committed
implemented reverse_array method with tests
1 parent 0347240 commit 153ce51

File tree

4 files changed

+103
-29
lines changed

4 files changed

+103
-29
lines changed

docs/source/pydatastructs/linear_data_structures/algorithms.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,6 @@ Algorithms
4949

5050
.. autofunction:: pydatastructs.shell_sort
5151

52-
.. autofunction:: pydatastructs.radix_sort
52+
.. autofunction:: pydatastructs.radix_sort
53+
54+
.. autofunction:: pydatastructs.reverse_array

pydatastructs/linear_data_structures/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
insertion_sort,
5050
intro_sort,
5151
shell_sort,
52-
radix_sort
52+
radix_sort,
53+
reverse_array
5354
)
5455
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
'insertion_sort',
3333
'intro_sort',
3434
'shell_sort',
35-
'radix_sort'
35+
'radix_sort',
36+
'reverse_array'
3637
]
3738

3839
def _merge(array, sl, el, sr, er, end, comp):
@@ -2013,3 +2014,50 @@ def radix_sort(array, *args, **kwargs):
20132014
array._modify(True)
20142015

20152016
return array
2017+
2018+
def reverse_array(array, *args, **kwargs):
2019+
"""
2020+
Reverses the specified portion of the array in-place.
2021+
2022+
Parameters
2023+
==========
2024+
2025+
array: Array
2026+
The array to be reversed (DynamicOneDimensionalArray or OneDimensionalArray).
2027+
start: int
2028+
The starting index of the portion to reverse.
2029+
Optional, by default 0
2030+
end: int
2031+
The ending index of the portion to reverse.
2032+
Optional, by default the index of the last position filled.
2033+
backend: pydatastructs.Backend
2034+
The backend to be used (PYTHON or CPP).
2035+
Optional, by default, the best available backend is used.
2036+
2037+
Returns
2038+
=======
2039+
2040+
output: Array
2041+
The array with the specified portion reversed.
2042+
"""
2043+
start = int(kwargs.get('start', 0))
2044+
end = int(kwargs.get('end', len(array) - 1))
2045+
2046+
if start >= end:
2047+
return array
2048+
n = end - start + 1
2049+
2050+
elements = [(array[i], i) for i in range(start, end + 1) if array[i] is not None]
2051+
elem_values = [e[0] for e in elements]
2052+
elem_values.reverse()
2053+
2054+
k = 0
2055+
for i in range(start, end + 1):
2056+
if array[i] is not None:
2057+
array[i] = elem_values[k]
2058+
k += 1
2059+
2060+
if _check_type(array, (DynamicArray, _arrays.DynamicOneDimensionalArray)):
2061+
array._modify(True)
2062+
2063+
return array

pydatastructs/linear_data_structures/tests/test_algorithms.py

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
66
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
77
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
8-
selection_sort, insertion_sort, intro_sort, shell_sort, radix_sort, Backend)
8+
selection_sort, insertion_sort, intro_sort, shell_sort, radix_sort, reverse_array, Backend)
99

1010
from pydatastructs.utils.raises_util import raises
1111
import random
@@ -200,15 +200,15 @@ def _test_inner_ordered(*args, **kwargs):
200200
assert output == expected_result
201201

202202
expected_result = True
203-
arr3 = ODA(int, [0, -1, -2, -3, -4, 4])
204-
output = is_ordered(arr3, start=1, end=4,
203+
arr2 = ODA(int, [0, -1, -2, -3, -4, 4])
204+
output = is_ordered(arr2, start=1, end=4,
205205
comp=lambda u, v: u > v, **kwargs)
206206
assert output == expected_result
207207

208208
expected_result = True
209-
arr4 = DODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
210-
arr4.delete(0)
211-
output = is_ordered(arr4, **kwargs)
209+
arr3 = DODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
210+
arr3.delete(0)
211+
output = is_ordered(arr3, **kwargs)
212212
assert output == expected_result
213213

214214
_test_inner_ordered()
@@ -227,13 +227,13 @@ def test_upper_bound():
227227
expected_result = 2
228228
assert expected_result == output
229229

230-
arr3 = ODA(int, [6, 6, 7, 8, 9])
231-
output = upper_bound(arr3, 5, start=2, end=4)
230+
arr2 = ODA(int, [6, 6, 7, 8, 9])
231+
output = upper_bound(arr2, 5, start=2, end=4)
232232
expected_result = 2
233233
assert expected_result == output
234234

235-
arr4 = ODA(int, [3, 4, 4, 6])
236-
output = upper_bound(arr4, 5, start=1, end=3)
235+
arr3 = ODA(int, [3, 4, 4, 6])
236+
output = upper_bound(arr3, 5, start=1, end=3)
237237
expected_result = 3
238238
assert expected_result == output
239239

@@ -242,13 +242,13 @@ def test_upper_bound():
242242
expected_result = 5
243243
assert expected_result == output
244244

245-
arr6 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
246-
output = upper_bound(arr6, 2, start=2, comp=lambda x, y: x > y)
245+
arr4 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
246+
output = upper_bound(arr4, 2, start=2, comp=lambda x, y: x > y)
247247
expected_result = 8
248248
assert expected_result == output
249249

250-
arr7 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
251-
output = upper_bound(arr7, 9, start=3, end=7, comp=lambda x, y: x > y)
250+
arr5 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
251+
output = upper_bound(arr5, 9, start=3, end=7, comp=lambda x, y: x > y)
252252
expected_result = 3
253253
assert expected_result == output
254254

@@ -270,13 +270,13 @@ def test_lower_bound():
270270
expected_result = 3
271271
assert expected_result == output
272272

273-
arr3 = ODA(int, [6, 6, 7, 8, 9])
274-
output = lower_bound(arr3, 5, end=3)
273+
arr2 = ODA(int, [6, 6, 7, 8, 9])
274+
output = lower_bound(arr2, 5, end=3)
275275
expected_result = 0
276276
assert expected_result == output
277277

278-
arr4 = ODA(int, [3, 4, 4, 4])
279-
output = lower_bound(arr4, 5)
278+
arr3 = ODA(int, [3, 4, 4, 4])
279+
output = lower_bound(arr3, 5)
280280
expected_result = 4
281281
assert expected_result == output
282282

@@ -285,13 +285,13 @@ def test_lower_bound():
285285
expected_result = 5
286286
assert expected_result == output
287287

288-
arr6 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
289-
output = lower_bound(arr6, 2, start=4, comp=lambda x, y: x > y)
288+
arr4 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
289+
output = lower_bound(arr4, 2, start=4, comp=lambda x, y: x > y)
290290
expected_result = 8
291291
assert expected_result == output
292292

293-
arr7 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
294-
output = lower_bound(arr7, 9, end=5, comp=lambda x, y: x > y)
293+
arr5 = ODA(int, [7, 6, 6, 6, 6, 5, 4, 3])
294+
output = lower_bound(arr5, 9, end=5, comp=lambda x, y: x > y)
295295
expected_result = 0
296296
assert expected_result == output
297297

@@ -313,13 +313,13 @@ def test_longest_increasing_subsequence():
313313
expected_result = [-1, 2, 3, 7, 9, 10]
314314
assert str(expected_result) == str(output)
315315

316-
arr3 = ODA(int, [6, 6, 6, 19, 9])
317-
output = longest_increasing_subsequence(arr3)
316+
arr2 = ODA(int, [6, 6, 6, 19, 9])
317+
output = longest_increasing_subsequence(arr2)
318318
expected_result = [6, 9]
319319
assert str(expected_result) == str(output)
320320

321-
arr4 = ODA(int, [5, 4, 4, 3, 3, 6, 6, 8])
322-
output = longest_increasing_subsequence(arr4)
321+
arr3 = ODA(int, [5, 4, 4, 3, 3, 6, 6, 8])
322+
output = longest_increasing_subsequence(arr3)
323323
expected_result = [3, 6, 8]
324324
assert str(expected_result) == str(output)
325325

@@ -420,3 +420,26 @@ def test_shell_sort():
420420

421421
def test_radix_sort():
422422
_test_common_sort(radix_sort)
423+
424+
def test_reverse_array():
425+
arr1 = DynamicOneDimensionalArray(int, [1, 2, 3, 4, 5])
426+
reverse_array(arr1)
427+
assert arr1._data == [5, 4, 3, 2, 1]
428+
429+
arr2 = DynamicOneDimensionalArray(int, [1, 2, 3])
430+
original_data = arr2._data.copy()
431+
reverse_array(arr2, start=2, end=1)
432+
assert arr2._data == original_data
433+
434+
arr3 = DynamicOneDimensionalArray(int, [42])
435+
reverse_array(arr3, start=0, end=0)
436+
assert arr3._data == [42]
437+
438+
arr4 = DynamicOneDimensionalArray(int, [5, -3, 1, -10, 7])
439+
reverse_array(arr4, start=1, end=3)
440+
assert arr4._data == [5, -10, 1, -3, 7]
441+
442+
arr5 = OneDimensionalArray(int, 5)
443+
arr5._data = [1, 2, 3, 4, 5]
444+
reverse_array(arr5, start=1, end=3)
445+
assert arr5._data == [1, 4, 3, 2, 5]

0 commit comments

Comments
 (0)