Created
April 15, 2017 16:47
-
-
Save nodew/e3950afd5866b34d10e50ad3e72660f3 to your computer and use it in GitHub Desktop.
to find increasing subsequence, for example, [1 0 1 2 3 0 4 5] => [0 1 2 3]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun find-subsequence-a (lst) | |
(let ((result '(1))) | |
(do ((l lst (cdr l))) | |
((null (cdr l)) result) | |
(let ((current (car l)) | |
(next (cadr l))) | |
(if (> next current) | |
(setq result (cons (1+ (car result)) result)) | |
(setq result (cons 1 result))))))) | |
(defun find-subsequence-1 (lst) | |
(let* ((dp (find-subsequence-a lst)) | |
(max-length (reduce #'max dp)) | |
(index (1+ (position max-length (reverse dp))))) | |
(subseq lst (- index max-length) index))) | |
(defun find-subsequence-b (lst) | |
(reduce #'(lambda (acc y) | |
(let ((x (caar acc))) | |
(if (> y x) | |
`((,y ,@(car acc)) ,@(cdr acc)) | |
`((,y) ,@acc)) | |
) | |
) | |
(cdr lst) | |
:initial-value `((,(car lst))))) | |
(defun find-subsequence-2 (lst) | |
(reverse (reduce #'(lambda (sub-a sub-b) | |
(if (> (length sub-a) (length sub-b)) | |
sub-a | |
sub-b)) | |
(find-subsequence-b lst)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment