Skip to content

Commit 7c8e8fa

Browse files
committed
Added longest alternating subsequence to linear algorithms
1 parent f4c1677 commit 7c8e8fa

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

docs/source/pydatastructs/linear_data_structures/algorithms.rst

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

4646
.. autofunction:: pydatastructs.jump_search
4747

48-
.. autofunction:: pydatastructs.intro_sort
48+
.. autofunction:: pydatastructs.intro_sort
49+
50+
.. autofunction:: pydatastructs.longest_increasing_subsequence

pydatastructs/linear_data_structures/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
jump_search,
4848
selection_sort,
4949
insertion_sort,
50-
intro_sort
50+
intro_sort,
51+
longest_alternating_subsequence,
5152
)
5253
__all__.extend(algorithms.__all__)

pydatastructs/linear_data_structures/algorithms.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
'jump_search',
3131
'selection_sort',
3232
'insertion_sort',
33-
'intro_sort'
33+
'intro_sort',
34+
'longest_alternating_subsequence',
3435
]
3536

3637
def _merge(array, sl, el, sr, er, end, comp):
@@ -1850,3 +1851,69 @@ def partition(array, lower, upper):
18501851
intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold)
18511852

18521853
return array
1854+
1855+
def longest_alternating_subsequence(array: OneDimensionalArray, **kwargs) -> OneDimensionalArray:
1856+
"""
1857+
Finds the longest alternating subsequence in the given array.
1858+
An alternating sequence is one in which the elements are in alternating order,
1859+
i.e., a sequence where the differences between consecutive elements alternate
1860+
between positive and negative.
1861+
1862+
Parameters
1863+
==========
1864+
1865+
array: OneDimensionalArray
1866+
The array from which the longest alternating subsequence is to be found.
1867+
backend: pydatastructs.Backend
1868+
The backend to be used.
1869+
Optional, by default, the best available backend is used.
1870+
1871+
Returns
1872+
=======
1873+
1874+
output: OneDimensionalArray
1875+
The longest alternating subsequence.
1876+
1877+
Examples
1878+
========
1879+
1880+
>>> from pydatastructs import OneDimensionalArray, longest_alternating_subsequence
1881+
>>> arr = OneDimensionalArray(int, [1, 5, 4])
1882+
>>> las = longest_alternating_subsequence(arr)
1883+
>>> str(las)
1884+
'[1, 5, 4]'
1885+
>>> arr = OneDimensionalArray(int, [1, 5, 4, 3, 2, 6, 7])
1886+
>>> las = longest_alternating_subsequence(arr)
1887+
>>> str(las)
1888+
'[1, 5, 4, 6, 2, 7]'
1889+
1890+
References
1891+
==========
1892+
1893+
.. [1] https://en.wikipedia.org/wiki/Longest_alternating_subsequence
1894+
1895+
Note
1896+
====
1897+
1898+
The data types of elements in the array should be comparable.
1899+
"""
1900+
raise_if_backend_is_not_python(
1901+
longest_alternating_subsequence, kwargs.get('backend', Backend.PYTHON))
1902+
1903+
increasing = 1
1904+
decreasing = 1
1905+
n = len(array)
1906+
1907+
# Iterate from second element
1908+
for i in range(1, n):
1909+
1910+
# Increasing changes if decreasing changes
1911+
if (array[i] > array[i-1]):
1912+
increasing = decreasing + 1
1913+
1914+
# Decreasing changes if increasing changes
1915+
elif (array[i] < array[i-1]):
1916+
decreasing = increasing + 1
1917+
1918+
# Return the maximum length
1919+
return max(increasing, decreasing)

0 commit comments

Comments
 (0)