fork download
  1. def lds(sequence, numbers, max_sequence):
  2. """
  3. In the following demo, I have chosen for clarity not to use a deque,
  4. which supports efficient appending and removing of values to either
  5. the front or back of the list. Instead, I am just using regular
  6. lists.
  7.  
  8. The important thing to note is that this function is never called
  9. with the values for the sequence and numbers arguments repeated
  10. implying that there are no overlapping subproblems,
  11. which is the requirements for a dynamic programming approach.
  12. With true dynamic programming you have memoization to remember a prior
  13. subproblem result for when that subproblem arises again. But that
  14. never happens here.
  15.  
  16. Instead, as you iterate the input numbers you arrive at one of 3
  17. conditions: (1) The current input number can not be appended nor prepended and you
  18. have found the longest descending sequence (lds) for the descisions made up
  19. to this point or (2) The number is appended or prepended as appropriate and you
  20. continue iterating or (3) The number is not added to the sequence and you continue
  21. iterating. When the current number can be added to the sequence, you perform options
  22. 2 and 3 and see which one producers a longer lds.
  23. """
  24.  
  25. print(f'sequence, arguments -> {sequence}, {numbers}') # Print out the arguments
  26.  
  27. if len(sequence) == 0:
  28. n = numbers[0]
  29. numbers = numbers[1:] # New list without the first element
  30. sequence = [n]
  31. max_sequence = sequence
  32. return lds(sequence, numbers, max_sequence)
  33.  
  34. if not numbers:
  35. l = len(sequence)
  36. if len(sequence) > len(max_sequence):
  37. max_sequence = sequence
  38. return max_sequence
  39.  
  40. n = numbers[0]
  41. # Can we prepend or append?
  42. if n <= sequence[0] and n >= sequence[-1]:
  43. # No!
  44. l = len(sequence)
  45. if l > len(max_sequence):
  46. max_sequence = sequence
  47. return max_sequence
  48.  
  49. numbers = numbers[1:] # New list without the first number
  50. # Compute maximum length without appending/prepending:
  51. max_sequence_1 = lds(sequence, numbers, max_sequence)
  52. l1 = len(max_sequence_1)
  53. # Compute maximum length with appending/prepending:
  54. if n > sequence[0]:
  55. sequence = [n, *sequence] # Prepend
  56. else:
  57. sequence = [*sequence, n] # Append
  58. max_sequence_2 = lds(sequence, numbers, max_sequence)
  59. l2 = len(max_sequence_2)
  60.  
  61. return max_sequence_1 if l1 >= l2 else max_sequence_2
  62.  
  63. max_sequence = lds([], [6, 7, 3, 5, 4], [])
  64. print('lds: ', max_sequence)
  65.  
Success #stdin #stdout 0.03s 9580KB
stdin
Standard input is empty
stdout
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]