Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
2
Task/Range-expansion/Haskell/range-expansion-1.hs
Normal file
2
Task/Range-expansion/Haskell/range-expansion-1.hs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
> expandRange "-6,-3--1,3-5,7-11,14,15,17-20"
|
||||
[-6,-3,-2,-1,3,4,5,7,8,9,10,11,14,15,17,18,19,20]
|
||||
10
Task/Range-expansion/Haskell/range-expansion-2.hs
Normal file
10
Task/Range-expansion/Haskell/range-expansion-2.hs
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
expandRange :: String -> [Int]
|
||||
expandRange = concatMap f . split ','
|
||||
where f str@(c : cs) | '-' `elem` cs = [read (c : a) .. read b]
|
||||
| otherwise = [read str]
|
||||
where (a, _ : b) = break (== '-') cs
|
||||
|
||||
split :: Eq a => a -> [a] -> [[a]]
|
||||
split delim [] = []
|
||||
split delim l = a : split delim (dropWhile (== delim) b)
|
||||
where (a, b) = break (== delim) l
|
||||
27
Task/Range-expansion/Haskell/range-expansion-3.hs
Normal file
27
Task/Range-expansion/Haskell/range-expansion-3.hs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{-# LANGUAGE FlexibleContexts #-}
|
||||
|
||||
import Text.Parsec
|
||||
|
||||
expandRange :: String -> Maybe [Int]
|
||||
expandRange =
|
||||
either
|
||||
(const Nothing)
|
||||
Just
|
||||
. parse rangeParser ""
|
||||
|
||||
rangeParser ::
|
||||
(Enum a, Read a, Stream s m Char) =>
|
||||
ParsecT s u m [a]
|
||||
rangeParser = concat <$> (item `sepBy` char ',')
|
||||
where
|
||||
item = do
|
||||
n1 <- num
|
||||
n2 <- option n1 (char '-' *> num)
|
||||
return [n1 .. n2]
|
||||
num =
|
||||
read `dot` (<>) <$> option "" (string "-")
|
||||
<*> many1 digit
|
||||
dot = (.) . (.)
|
||||
|
||||
main :: IO ()
|
||||
main = print $ expandRange "-6,-3--1,3-5,7-11,14,15,17-20"
|
||||
Loading…
Add table
Add a link
Reference in a new issue