From 3efc7baa2178b77dbdebe89825402711d74632b7 Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 30 Oct 2019 23:33:26 +0800 Subject: [PATCH 1/4] Update longest_increasing_subsequence.py --- .../longest_increasing_subsequence.py | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index 3cb57806e06f..222627d653bf 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -7,10 +7,19 @@ 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 """ - - -def longestSub(ARRAY): # This function is recursive - +from typing import List + + +def longestSub(ARRAY: List[int]) -> List[int]: # This function is recursive + """ + Some examples + >>> longestSub([10, 22, 9, 33, 21, 50, 41, 60, 80]) + [10, 22, 33, 41, 60, 80] + >>> longestSub([4, 8, 7, 5, 1, 12, 2, 3, 9]) + [1, 2, 3, 9] + >>> longestSub([9, 8, 7, 6, 5, 7]) + [8] + """ ARRAY_LENGTH = len(ARRAY) if ( ARRAY_LENGTH <= 1 @@ -37,9 +46,8 @@ def longestSub(ARRAY): # This function is recursive return TEMPORARY_ARRAY else: return LONGEST_SUB + - -# Some examples - -print(longestSub([4, 8, 7, 5, 1, 12, 2, 3, 9])) -print(longestSub([9, 8, 7, 6, 5, 7])) +if __name__ == "__main__": + import doctest + doctest.testmod() From 172776eb7fc4cc20b001eb08fa2ae0e3be6b713a Mon Sep 17 00:00:00 2001 From: John Law Date: Wed, 30 Oct 2019 23:35:13 +0800 Subject: [PATCH 2/4] Update longest_increasing_subsequence.py --- dynamic_programming/longest_increasing_subsequence.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index 222627d653bf..88b433c988db 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -19,6 +19,8 @@ def longestSub(ARRAY: List[int]) -> List[int]: # This function is recursive [1, 2, 3, 9] >>> longestSub([9, 8, 7, 6, 5, 7]) [8] + >>> longestSub([1, 1, 1]) + [1, 1, 1] """ ARRAY_LENGTH = len(ARRAY) if ( From 81f7d9af81c53a73ce437cbfbcda9bd4f9cbef63 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 5 Nov 2019 00:20:33 +0800 Subject: [PATCH 3/4] Format longest_increasing_subsequence.py to PEP8 --- .../longest_increasing_subsequence.py | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index 88b433c988db..4c1e610c68a6 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -4,50 +4,50 @@ 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. +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 typing import List -def longestSub(ARRAY: List[int]) -> List[int]: # This function is recursive +def longest_subsequence(Array: List[int]) -> List[int]: # This function is recursive """ Some examples - >>> longestSub([10, 22, 9, 33, 21, 50, 41, 60, 80]) + >>> longest_subsequence([10, 22, 9, 33, 21, 50, 41, 60, 80]) [10, 22, 33, 41, 60, 80] - >>> longestSub([4, 8, 7, 5, 1, 12, 2, 3, 9]) + >>> longest_subsequence([4, 8, 7, 5, 1, 12, 2, 3, 9]) [1, 2, 3, 9] - >>> longestSub([9, 8, 7, 6, 5, 7]) + >>> longest_subsequence([9, 8, 7, 6, 5, 7]) [8] - >>> longestSub([1, 1, 1]) + >>> longest_subsequence([1, 1, 1]) [1, 1, 1] """ - ARRAY_LENGTH = len(ARRAY) + ArrayLength = len(Array) if ( - ARRAY_LENGTH <= 1 + ArrayLength <= 1 ): # If the array contains only one element, we return it (it's the stop condition of recursion) - return ARRAY + return Array # Else - PIVOT = ARRAY[0] + Pivot = Array[0] isFound = False i = 1 - LONGEST_SUB = [] - while not isFound and i < ARRAY_LENGTH: - if ARRAY[i] < PIVOT: + LongestSubseq = [] + while not isFound and i < ArrayLength: + if Array[i] < Pivot: isFound = True - TEMPORARY_ARRAY = [element for element in ARRAY[i:] if element >= ARRAY[i]] - TEMPORARY_ARRAY = longestSub(TEMPORARY_ARRAY) - if len(TEMPORARY_ARRAY) > len(LONGEST_SUB): - LONGEST_SUB = TEMPORARY_ARRAY + TempArray = [element for element in Array[i:] if element >= Array[i]] + TempArray = longest_subsequence(TempArray) + if len(TempArray) > len(LongestSubseq): + LongestSubseq = TempArray else: i += 1 - TEMPORARY_ARRAY = [element for element in ARRAY[1:] if element >= PIVOT] - TEMPORARY_ARRAY = [PIVOT] + longestSub(TEMPORARY_ARRAY) - if len(TEMPORARY_ARRAY) > len(LONGEST_SUB): - return TEMPORARY_ARRAY + TempArray = [element for element in Array[1:] if element >= Pivot] + TempArray = [Pivot] + longest_subsequence(TempArray) + if len(TempArray) > len(LongestSubseq): + return TempArray else: - return LONGEST_SUB + return LongestSubseq if __name__ == "__main__": From 073655f2062df61b537eb2441f12b686ad509f50 Mon Sep 17 00:00:00 2001 From: John Law Date: Tue, 5 Nov 2019 00:35:33 +0800 Subject: [PATCH 4/4] Update longest_increasing_subsequence.py --- .../longest_increasing_subsequence.py | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence.py b/dynamic_programming/longest_increasing_subsequence.py index 4c1e610c68a6..6d12f1c7caf0 100644 --- a/dynamic_programming/longest_increasing_subsequence.py +++ b/dynamic_programming/longest_increasing_subsequence.py @@ -4,13 +4,13 @@ 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. +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 typing import List -def longest_subsequence(Array: List[int]) -> List[int]: # This function is recursive +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]) @@ -22,32 +22,32 @@ def longest_subsequence(Array: List[int]) -> List[int]: # This function is recu >>> longest_subsequence([1, 1, 1]) [1, 1, 1] """ - ArrayLength = len(Array) + array_length = len(array) if ( - ArrayLength <= 1 + array_length <= 1 ): # If the array contains only one element, we return it (it's the stop condition of recursion) - return Array + return array # Else - Pivot = Array[0] + pivot = array[0] isFound = False i = 1 - LongestSubseq = [] - while not isFound and i < ArrayLength: - if Array[i] < Pivot: + longest_subseq = [] + while not isFound and i < array_length: + if array[i] < pivot: isFound = True - TempArray = [element for element in Array[i:] if element >= Array[i]] - TempArray = longest_subsequence(TempArray) - if len(TempArray) > len(LongestSubseq): - LongestSubseq = TempArray + 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 - TempArray = [element for element in Array[1:] if element >= Pivot] - TempArray = [Pivot] + longest_subsequence(TempArray) - if len(TempArray) > len(LongestSubseq): - return TempArray + 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 LongestSubseq + return longest_subseq if __name__ == "__main__":