Haskell solution:
import Data.Foldable (foldl') data IndexedNumber = IndexedNumber { index :: Int , number :: Int } deriving Show nconsecutive :: [Int] -> [IndexedNumber] nconsecutive xs = reverse $ foldl' compute [] indexedPairs where indexedPairs = zip3 [1..] xs (tail xs) compute result (i, x, x') | x' - x == 1 = result | otherwise = (IndexedNumber i x') : result
Test results:
> nconsecutive [6,7,8,9,11,12] [IndexedNumber {index = 4, number = 11}] > nconsecutive [100,101,102,112,113,114,129] [IndexedNumber {index = 3, number = 112},IndexedNumber {index = 6, number = 129}]
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Haskell solution:
Test results: