Skip to content

Commit a9d5378

Browse files
authored
Doctest and typing for longest_increasing_subsequence.py (#1526)
* Update longest_increasing_subsequence.py * Update longest_increasing_subsequence.py * Format longest_increasing_subsequence.py to PEP8 * Update longest_increasing_subsequence.py
1 parent 5452e94 commit a9d5378

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

dynamic_programming/longest_increasing_subsequence.py

+36-26
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,52 @@
44
This is a pure Python implementation of Dynamic Programming solution to the longest increasing subsequence of a given sequence.
55
66
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.
88
Example: [10, 22, 9, 33, 21, 50, 41, 60, 80] as input will return [10, 22, 33, 41, 60, 80] as output
99
"""
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)
1526
if (
16-
ARRAY_LENGTH <= 1
27+
array_length <= 1
1728
): # If the array contains only one element, we return it (it's the stop condition of recursion)
18-
return ARRAY
29+
return array
1930
# Else
20-
PIVOT = ARRAY[0]
31+
pivot = array[0]
2132
isFound = False
2233
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:
2637
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
3142
else:
3243
i += 1
3344

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
3849
else:
39-
return LONGEST_SUB
40-
41-
42-
# Some examples
50+
return longest_subseq
51+
4352

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

Comments
 (0)