Skip to content

Instantly share code, notes, and snippets.

@yihuang
Created March 31, 2012 08:03
Show Gist options
  • Save yihuang/2260787 to your computer and use it in GitHub Desktop.
Save yihuang/2260787 to your computer and use it in GitHub Desktop.
find longest continuous sub number sequence.
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