Created
March 31, 2012 08:03
-
-
Save yihuang/2260787 to your computer and use it in GitHub Desktop.
find longest continuous sub number sequence.
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
import Data.List | |
import Data.Function | |
isSeq :: [Int] -> Bool | |
isSeq l = | |
let l' = sort l | |
in all (==(-1)) $ zipWith (-) l' (tail l') | |
longest :: [Int] -> [Int] | |
longest = maximumBy (compare `on` length) | |
. map (head . filter isSeq . reverse . inits) | |
. tails | |
main = do | |
print $ longest [5,1,3,2] | |
print $ longest [1,3,5,2,4] | |
print $ longest [1,3,5,2] | |
{- | |
> ./Main | |
[1,3,2] | |
[1,3,5,2,4] | |
[2] | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment