Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Range-extraction/Haskell/range-extraction-1.hs
Normal file
13
Task/Range-extraction/Haskell/range-extraction-1.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import Data.List (intercalate)
|
||||
|
||||
extractRange :: [Int] -> String
|
||||
extractRange = intercalate "," . f
|
||||
where f :: [Int] -> [String]
|
||||
f (x1 : x2 : x3 : xs) | x1 + 1 == x2 && x2 + 1 == x3
|
||||
= (show x1 ++ '-' : show xn) : f xs'
|
||||
where (xn, xs') = g (x3 + 1) xs
|
||||
g a (n : ns) | a == n = g (a + 1) ns
|
||||
| otherwise = (a - 1, n : ns)
|
||||
g a [] = (a - 1, [])
|
||||
f (x : xs) = show x : f xs
|
||||
f [] = []
|
||||
2
Task/Range-extraction/Haskell/range-extraction-2.hs
Normal file
2
Task/Range-extraction/Haskell/range-extraction-2.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> extractRange $ [0..2] ++ 4 : [6..8] ++ 11 : 12 : [14..25] ++ [27..33] ++ [35..39]
|
||||
"0-2,4,6-8,11,12,14-25,27-33,35-39"
|
||||
68
Task/Range-extraction/Haskell/range-extraction-3.hs
Normal file
68
Task/Range-extraction/Haskell/range-extraction-3.hs
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import Data.List (intercalate)
|
||||
import Data.Function (on)
|
||||
|
||||
----------------------- RANGE FORMAT ---------------------
|
||||
rangeFormat :: [Int] -> String
|
||||
rangeFormat = intercalate "," . fmap rangeString . splitBy ((/=) . succ)
|
||||
|
||||
rangeString xs
|
||||
| 2 < length xs = x ++ '-' : last t
|
||||
| otherwise = intercalate "," ps
|
||||
where
|
||||
ps@(x:t) = show <$> xs
|
||||
|
||||
--------------------- GENERIC FUNCTION -------------------
|
||||
-- Split wherever a supplied predicate matches the
|
||||
-- relationship between two consecutive items.
|
||||
splitBy :: (a -> a -> Bool) -> [a] -> [[a]]
|
||||
splitBy _ [] = []
|
||||
splitBy _ [x] = [[x]]
|
||||
splitBy f xs@(_:t) = uncurry (:) $ foldr go ([], []) (zip xs t)
|
||||
where
|
||||
go (x, prev) (active, acc)
|
||||
| f x prev = ([x], current : acc)
|
||||
| otherwise = (x : current, acc)
|
||||
where
|
||||
current
|
||||
| null active = [prev]
|
||||
| otherwise = active
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main =
|
||||
print $
|
||||
rangeFormat
|
||||
[ 0
|
||||
, 1
|
||||
, 2
|
||||
, 4
|
||||
, 6
|
||||
, 7
|
||||
, 8
|
||||
, 11
|
||||
, 12
|
||||
, 14
|
||||
, 15
|
||||
, 16
|
||||
, 17
|
||||
, 18
|
||||
, 19
|
||||
, 20
|
||||
, 21
|
||||
, 22
|
||||
, 23
|
||||
, 24
|
||||
, 25
|
||||
, 27
|
||||
, 28
|
||||
, 29
|
||||
, 30
|
||||
, 31
|
||||
, 32
|
||||
, 33
|
||||
, 35
|
||||
, 36
|
||||
, 37
|
||||
, 38
|
||||
, 39
|
||||
]
|
||||
29
Task/Range-extraction/Haskell/range-extraction-4.hs
Normal file
29
Task/Range-extraction/Haskell/range-extraction-4.hs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import Data.List (intercalate, groupBy, isPrefixOf)
|
||||
import Data.List.Split (chop)
|
||||
import Data.Bool (bool)
|
||||
|
||||
rangeFormat :: [Int] -> String
|
||||
rangeFormat xs =
|
||||
intercalate "," $
|
||||
(head . ((bool <*> tail) <*> (> 1) . length)) <$>
|
||||
groupBy isPrefixOf (rangeString <$> chop succSpan (zip xs (tail xs)))
|
||||
|
||||
rangeString [] = ""
|
||||
rangeString xxs@(x:xs)
|
||||
| null xs = show (snd x)
|
||||
| otherwise = intercalate "-" (show <$> [fst x, snd (last xs)])
|
||||
|
||||
succSpan [] = ([], [])
|
||||
succSpan (xxs@(x:xs))
|
||||
| null ys = ([x], xs)
|
||||
| otherwise = (ys, zs)
|
||||
where
|
||||
(ys, zs) = span (uncurry ((==) . succ)) xxs
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
putStrLn $
|
||||
rangeFormat [ 0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
|
||||
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
|
||||
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
|
||||
37, 38, 39 ]
|
||||
Loading…
Add table
Add a link
Reference in a new issue