mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 09:10:16 +00:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
62 lines
1.8 KiB
Python
62 lines
1.8 KiB
Python
"""
|
|
Author : Mehdi ALAOUI
|
|
|
|
This is a pure Python implementation of Dynamic Programming solution to the longest
|
|
increasing subsequence of a given sequence.
|
|
|
|
The problem is :
|
|
Given an array, to find the longest and increasing sub-array in that given array and
|
|
return it.
|
|
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return
|
|
[10, 22, 33, 41, 60, 80] as output
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
|
|
def longest_subsequence(array: list[int]) -> list[int]: # This function is recursive
|
|
"""
|
|
Some examples
|
|
>>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80])
|
|
[10, 22, 33, 41, 60, 80]
|
|
>>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9])
|
|
[1, 2, 3, 9]
|
|
>>> longest_subsequence([9, 8, 7, 6, 5, 7])
|
|
[8]
|
|
>>> longest_subsequence([1, 1, 1])
|
|
[1, 1, 1]
|
|
>>> longest_subsequence([])
|
|
[]
|
|
"""
|
|
array_length = len(array)
|
|
# If the array contains only one element, we return it (it's the stop condition of
|
|
# recursion)
|
|
if array_length <= 1:
|
|
return array
|
|
# Else
|
|
pivot = array[0]
|
|
is_found = False
|
|
i = 1
|
|
longest_subseq: list[int] = []
|
|
while not is_found and i < array_length:
|
|
if array[i] < pivot:
|
|
is_found = True
|
|
temp_array = [element for element in array[i:] if element >= array[i]]
|
|
temp_array = longest_subsequence(temp_array)
|
|
if len(temp_array) > len(longest_subseq):
|
|
longest_subseq = temp_array
|
|
else:
|
|
i += 1
|
|
|
|
temp_array = [element for element in array[1:] if element >= pivot]
|
|
temp_array = [pivot] + longest_subsequence(temp_array)
|
|
if len(temp_array) > len(longest_subseq):
|
|
return temp_array
|
|
else:
|
|
return longest_subseq
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|