|
4 | 4 | This is a pure Python implementation of Dynamic Programming solution to the longest increasing subsequence of a given sequence.
|
5 | 5 |
|
6 | 6 | The problem is :
|
7 |
| -Given an ARRAY, to find the longest and increasing sub ARRAY in that given ARRAY and return it. |
| 7 | +Given an array, to find the longest and increasing sub-array in that given array and return it. |
8 | 8 | Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output
|
9 | 9 | """
|
10 |
| - |
11 |
| - |
12 |
| -def longestSub(ARRAY): # This function is recursive |
13 |
| - |
14 |
| - ARRAY_LENGTH = len(ARRAY) |
| 10 | +from typing import List |
| 11 | + |
| 12 | + |
| 13 | +def longest_subsequence(array: List[int]) -> List[int]: # This function is recursive |
| 14 | + """ |
| 15 | + Some examples |
| 16 | + >>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80]) |
| 17 | + [10, 22, 33, 41, 60, 80] |
| 18 | + >>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9]) |
| 19 | + [1, 2, 3, 9] |
| 20 | + >>> longest_subsequence([9, 8, 7, 6, 5, 7]) |
| 21 | + [8] |
| 22 | + >>> longest_subsequence([1, 1, 1]) |
| 23 | + [1, 1, 1] |
| 24 | + """ |
| 25 | + array_length = len(array) |
15 | 26 | if (
|
16 |
| - ARRAY_LENGTH <= 1 |
| 27 | + array_length <= 1 |
17 | 28 | ): # If the array contains only one element, we return it (it's the stop condition of recursion)
|
18 |
| - return ARRAY |
| 29 | + return array |
19 | 30 | # Else
|
20 |
| - PIVOT = ARRAY[0] |
| 31 | + pivot = array[0] |
21 | 32 | isFound = False
|
22 | 33 | i = 1
|
23 |
| - LONGEST_SUB = [] |
24 |
| - while not isFound and i < ARRAY_LENGTH: |
25 |
| - if ARRAY[i] < PIVOT: |
| 34 | + longest_subseq = [] |
| 35 | + while not isFound and i < array_length: |
| 36 | + if array[i] < pivot: |
26 | 37 | isFound = True
|
27 |
| - TEMPORARY_ARRAY = [element for element in ARRAY[i:] if element >= ARRAY[i]] |
28 |
| - TEMPORARY_ARRAY = longestSub(TEMPORARY_ARRAY) |
29 |
| - if len(TEMPORARY_ARRAY) > len(LONGEST_SUB): |
30 |
| - LONGEST_SUB = TEMPORARY_ARRAY |
| 38 | + temp_array = [element for element in array[i:] if element >= array[i]] |
| 39 | + temp_array = longest_subsequence(temp_array) |
| 40 | + if len(temp_array) > len(longest_subseq): |
| 41 | + longest_subseq = temp_array |
31 | 42 | else:
|
32 | 43 | i += 1
|
33 | 44 |
|
34 |
| - TEMPORARY_ARRAY = [element for element in ARRAY[1:] if element >= PIVOT] |
35 |
| - TEMPORARY_ARRAY = [PIVOT] + longestSub(TEMPORARY_ARRAY) |
36 |
| - if len(TEMPORARY_ARRAY) > len(LONGEST_SUB): |
37 |
| - return TEMPORARY_ARRAY |
| 45 | + temp_array = [element for element in array[1:] if element >= pivot] |
| 46 | + temp_array = [pivot] + longest_subsequence(temp_array) |
| 47 | + if len(temp_array) > len(longest_subseq): |
| 48 | + return temp_array |
38 | 49 | else:
|
39 |
| - return LONGEST_SUB |
40 |
| - |
41 |
| - |
42 |
| -# Some examples |
| 50 | + return longest_subseq |
| 51 | + |
43 | 52 |
|
44 |
| -print(longestSub([4, 8, 7, 5, 1, 12, 2, 3, 9])) |
45 |
| -print(longestSub([9, 8, 7, 6, 5, 7])) |
| 53 | +if __name__ == "__main__": |
| 54 | + import doctest |
| 55 | + doctest.testmod() |
0 commit comments