|
30 | 30 | 'jump_search', |
31 | 31 | 'selection_sort', |
32 | 32 | 'insertion_sort', |
33 | | - 'intro_sort' |
| 33 | + 'intro_sort', |
| 34 | + 'longest_alternating_subsequence', |
34 | 35 | ] |
35 | 36 |
|
36 | 37 | def _merge(array, sl, el, sr, er, end, comp): |
@@ -1850,3 +1851,69 @@ def partition(array, lower, upper): |
1850 | 1851 | intro_sort(array, start=p+1, end=upper, maxdepth=maxdepth-1, ins_threshold=ins_threshold) |
1851 | 1852 |
|
1852 | 1853 | 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