From 357dbd4ada738ed9d4b814eeec99bcc833e94aff Mon Sep 17 00:00:00 2001 From: John Law Date: Thu, 31 Oct 2019 01:06:07 +0800 Subject: [PATCH] Doctest, type hints and bug in LIS_O(nlogn) (#1525) * Update longest_increasing_subsequence_o(nlogn).py * Update longest_increasing_subsequence_o(nlogn).py --- ...longest_increasing_subsequence_o(nlogn).py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py index f7b2c6915..4b06e0d88 100644 --- a/dynamic_programming/longest_increasing_subsequence_o(nlogn).py +++ b/dynamic_programming/longest_increasing_subsequence_o(nlogn).py @@ -4,18 +4,29 @@ # comments: This programme outputs the Longest Strictly Increasing Subsequence in O(NLogN) # Where N is the Number of elements in the list ############################# +from typing import List + def CeilIndex(v, l, r, key): while r - l > 1: - m = (l + r) / 2 + m = (l + r) // 2 if v[m] >= key: r = m else: l = m - return r -def LongestIncreasingSubsequenceLength(v): +def LongestIncreasingSubsequenceLength(v: List[int]) -> int: + """ + >>> LongestIncreasingSubsequenceLength([2, 5, 3, 7, 11, 8, 10, 13, 6]) + 6 + >>> LongestIncreasingSubsequenceLength([]) + 0 + >>> LongestIncreasingSubsequenceLength([0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]) + 6 + >>> LongestIncreasingSubsequenceLength([5, 4, 3, 2, 1]) + 1 + """ if len(v) == 0: return 0 @@ -37,5 +48,5 @@ def LongestIncreasingSubsequenceLength(v): if __name__ == "__main__": - v = [2, 5, 3, 7, 11, 8, 10, 13, 6] - print(LongestIncreasingSubsequenceLength(v)) + import doctest + doctest.testmod()