Advertisement
banovski

Quick, reliable pseudo-shuffler (enhanced)

Feb 29th, 2024 (edited)
1,700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 0.92 KB | Source Code | 0 0
  1. import Data.Function
  2. import Data.List
  3.  
  4. orderLists :: [a] -> [a] -> ([a], [a])
  5. orderLists xs ys
  6.   | on compare length xs ys == LT = (xs, ys)
  7.   | otherwise = (ys, xs)
  8.  
  9. listsLengthQuotient :: ([a], [a]) -> ([a], [a], Int)
  10. listsLengthQuotient (xs, ys) =
  11.   (xs, ys, (round $ on (/) (fromIntegral . length) ys xs) :: Int)
  12.  
  13. chunksOf :: Int -> [a] -> [[a]]
  14. chunksOf n [] = []
  15. chunksOf x xs = take x xs : chunksOf x (drop x xs)
  16.  
  17. groupListItems :: ([a], [a], Int) -> ([[a]], [[a]])
  18. groupListItems (xs, ys, q) = (chunksOf 1 xs, chunksOf q ys)
  19.  
  20. merge :: ([a], [a]) -> [a]
  21. merge (xs, []) = xs
  22. merge ([], ys) = ys
  23. merge (x:xs, y:ys) = y : x : merge (xs, ys)
  24.  
  25. mixLists :: [a] -> [a] -> [a]
  26. mixLists xs ys =
  27.   concat $ merge $ groupListItems $ listsLengthQuotient $ orderLists xs ys
  28.  
  29. main :: IO ()
  30. main = do
  31.   input <- getContents
  32.   mapM_ putStrLn $
  33.     foldl1 mixLists $ sortBy (on compare length) (group . sort $ lines input)
  34.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement