Advertisement
osipyonok

Huiaskel 2-7

Jan 22nd, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad
  2. import qualified Data.ByteString.Char8 as B
  3. import Data.Maybe
  4. import Data.List
  5. import Control.Arrow
  6.  
  7. readInt :: B.ByteString -> Int
  8. readInt = fst . fromJust . B.readInt
  9.  
  10. main :: IO ()
  11. main = do
  12.   [len] <- liftM (map readInt . B.words) B.getLine
  13.   lst   <- liftM (map readInt . take len . B.words) B.getLine :: IO [Int]
  14.   [n]   <- liftM (map readInt . B.words) B.getLine
  15.   putStrLn "Input: "
  16.   print lst
  17.   print n
  18.   putStrLn "Result: "
  19.   print (distribute lst len n n)
  20.  
  21. erase idx xs = lft ++ rgt
  22.   where (lft, (_:rgt)) = splitAt idx xs
  23.  
  24.  
  25. distribute _ _ _  0    = []
  26. distribute [] _ _ todo = [[]] ++  distribute [] 0 0 (todo - 1)
  27. distribute (h:t) len groups todo = [sublst] ++ distribute t (len - 1) groups (todo - 1)
  28.     where sublst = select_sublist ([h] ++ t) len 0 groups
  29.  
  30.  
  31. select_sublist []    n i m = []
  32. select_sublist (h:t) n i m | mod i m == 0 = [h] ++ select_sublist t n (i + 1) m
  33.                            | otherwise    = (select_sublist t n (i + 1) m)
  34.  
  35. -- 10
  36. -- 1 2 3 4 5 6 7 8 9 10
  37. -- 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement