Skip to content

feat: add longest alternating subsequence #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added tests for longest alternating subsequence algorithm
  • Loading branch information
FamALouiz committed Mar 10, 2025
commit d594f682fc3a050af66f3fc834d3af481d223d95
31 changes: 30 additions & 1 deletion pydatastructs/linear_data_structures/tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
cocktail_shaker_sort, quick_sort, longest_common_subsequence, is_ordered,
upper_bound, lower_bound, longest_increasing_subsequence, next_permutation,
prev_permutation, bubble_sort, linear_search, binary_search, jump_search,
selection_sort, insertion_sort, intro_sort, Backend)
selection_sort, insertion_sort, intro_sort, longest_alternating_subsequence, Backend)

from pydatastructs.utils.raises_util import raises
import random
Expand Down Expand Up @@ -414,3 +414,32 @@ def test_binary_search():
def test_jump_search():
_test_common_search(jump_search)
_test_common_search(jump_search, backend=Backend.CPP)


def test_longest_alternating_subsequence():
ODA = OneDimensionalArray

arr1 = ODA(int, [-4, 3, -5, 9, 10, 12, 2, -1])
output: OneDimensionalArray = longest_alternating_subsequence(arr1)
expected_result = [3, -5, 9, 2, -1]
assert len(expected_result) == output

arr2 = ODA(int, [10, 22, 9, 33, 49, 50, 31, 60])
output: OneDimensionalArray = longest_alternating_subsequence(arr2)
expected_result = [10, 22, 9, 33, 31, 60]
assert len(expected_result) == output

arr3 = ODA(int, [1, 2, 3, 4, 5, 6, 7, 8, 9])
output: OneDimensionalArray = longest_alternating_subsequence(arr3)
expected_result = [1, 2]
assert len(expected_result) == output

arr4 = ODA(int, [9, 8, 7, 6, 5, 4, 3, 2, 1])
output: OneDimensionalArray = longest_alternating_subsequence(arr4)
expected_result = [9, 8]
assert len(expected_result) == output

arr5 = ODA(int, [1, 5, 4, 3, 2, 1, 6, 7, 8, 9])
output: OneDimensionalArray = longest_alternating_subsequence(arr5)
expected_result = [1, 5, 4, 6]
assert len(expected_result) == output
Loading