GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Doctest and typing for longest_increasing_subsequence.py (#1526) · jusing-es/Python@a9d5378

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • dynamic_programming
    • longest_increasing_subsequence.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎dynamic_programming/longest_increasing_subsequence.py‎

Copy file name to clipboardExpand all lines: dynamic_programming/longest_increasing_subsequence.py
+36-26Lines changed: 36 additions & 26 deletions
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 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.