Last active
August 29, 2015 14:15
-
-
Save maarek/ecbe1e176c292b6912be to your computer and use it in GitHub Desktop.
Length of longest subsequence of (+1)-increasing consecutive integers
This file contains hidden or 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
; ex. (= (__ [1 0 1 2 3 0 4 5]) [0 1 2 3]) | |
; #1 given list | |
(def lci (fn [list] | |
; #2 create partitioned list ((1 0) (0 1) (1 2) (2 3) (3 0) (0 4) (4 5)) | |
; #3 perform last first on partitioned list | |
(->> (partition 2 1 list) | |
; #4 goes through each group and determines if index 2 is less than index 1 and groups those as it iterates | |
; (((1 0)) ((0 1) (1 2) (2 3)) ((3 0)) ((0 4)) ((4 5))) | |
(partition-by #(- (second %) (first %))) | |
; #5 iterate each group from the previous to determine is index 2 is less than 1 and if true return | |
; #6 (((0 1) (1 2) (2 3)) ((4 5))) | |
(filter #(= 1 (- (second (first %)) (ffirst %)))) | |
; #7 check each group if on which has greater count and return that one ((0 1) (1 2) (2 3)) | |
(reduce #(if (< (count %1) (count %2)) %2 %1) []) | |
; #8 flatten all values back into one list (0 1 1 2 2 3) | |
flatten | |
; #9 remove copies that were created by the grouping | |
distinct | |
; #10 count | |
count | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment