def lds(sequence, numbers, max_sequence): """ In the following demo, I have chosen for clarity not to use a deque, which supports efficient appending and removing of values to either the front or back of the list. Instead, I am just using regular lists. The important thing to note is that this function is never called with the values for the sequence and numbers arguments repeated implying that there are no overlapping subproblems, which is the requirements for a dynamic programming approach. With true dynamic programming you have memoization to remember a prior subproblem result for when that subproblem arises again. But that never happens here. Instead, as you iterate the input numbers you arrive at one of 3 conditions: (1) The current input number can not be appended nor prepended and you have found the longest descending sequence (lds) for the descisions made up to this point or (2) The number is appended or prepended as appropriate and you continue iterating or (3) The number is not added to the sequence and you continue iterating. When the current number can be added to the sequence, you perform options 2 and 3 and see which one producers a longer lds. """ print(f'sequence, arguments -> {sequence}, {numbers}') # Print out the arguments if len(sequence) == 0: n = numbers[0] numbers = numbers[1:] # New list without the first element sequence = [n] max_sequence = sequence return lds(sequence, numbers, max_sequence) if not numbers: l = len(sequence) if len(sequence) > len(max_sequence): max_sequence = sequence return max_sequence n = numbers[0] # Can we prepend or append? if n <= sequence[0] and n >= sequence[-1]: # No! l = len(sequence) if l > len(max_sequence): max_sequence = sequence return max_sequence numbers = numbers[1:] # New list without the first number # Compute maximum length without appending/prepending: max_sequence_1 = lds(sequence, numbers, max_sequence) l1 = len(max_sequence_1) # Compute maximum length with appending/prepending: if n > sequence[0]: sequence = [n, *sequence] # Prepend else: sequence = [*sequence, n] # Append max_sequence_2 = lds(sequence, numbers, max_sequence) l2 = len(max_sequence_2) return max_sequence_1 if l1 >= l2 else max_sequence_2 max_sequence = lds([], [6, 7, 3, 5, 4], []) print('lds: ', max_sequence)
Standard input is empty
sequence, arguments -> [], [6, 7, 3, 5, 4] sequence, arguments -> [6], [7, 3, 5, 4] sequence, arguments -> [6], [3, 5, 4] sequence, arguments -> [6], [5, 4] sequence, arguments -> [6], [4] sequence, arguments -> [6], [] sequence, arguments -> [6, 4], [] sequence, arguments -> [6, 5], [4] sequence, arguments -> [6, 5], [] sequence, arguments -> [6, 5, 4], [] sequence, arguments -> [6, 3], [5, 4] sequence, arguments -> [7, 6], [3, 5, 4] sequence, arguments -> [7, 6], [5, 4] sequence, arguments -> [7, 6], [4] sequence, arguments -> [7, 6], [] sequence, arguments -> [7, 6, 4], [] sequence, arguments -> [7, 6, 5], [4] sequence, arguments -> [7, 6, 5], [] sequence, arguments -> [7, 6, 5, 4], [] sequence, arguments -> [7, 6, 3], [5, 4] lds: [7, 6, 5, 4]