Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
13
Task/Ordered-partitions/Haskell/ordered-partitions-1.hs
Normal file
13
Task/Ordered-partitions/Haskell/ordered-partitions-1.hs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
import Data.List ((\\))
|
||||
|
||||
comb :: Int -> [a] -> [[a]]
|
||||
comb 0 _ = [[]]
|
||||
comb _ [] = []
|
||||
comb k (x:xs) = map (x:) (comb (k-1) xs) ++ comb k xs
|
||||
|
||||
partitions :: [Int] -> [[[Int]]]
|
||||
partitions xs = p [1..sum xs] xs
|
||||
where p _ [] = [[]]
|
||||
p xs (k:ks) = [ cs:rs | cs <- comb k xs, rs <- p (xs \\ cs) ks ]
|
||||
|
||||
main = print $ partitions [2,0,2]
|
||||
12
Task/Ordered-partitions/Haskell/ordered-partitions-2.hs
Normal file
12
Task/Ordered-partitions/Haskell/ordered-partitions-2.hs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
comb :: Int -> [a] -> [([a],[a])]
|
||||
comb 0 xs = [([],xs)]
|
||||
comb _ [] = []
|
||||
comb k (x:xs) = [ (x:cs,zs) | (cs,zs) <- comb (k-1) xs ] ++
|
||||
[ (cs,x:zs) | (cs,zs) <- comb k xs ]
|
||||
|
||||
partitions :: [Int] -> [[[Int]]]
|
||||
partitions xs = p [1..sum xs] xs
|
||||
where p _ [] = [[]]
|
||||
p xs (k:ks) = [ cs:rs | (cs,zs) <- comb k xs, rs <- p zs ks ]
|
||||
|
||||
main = print $ partitions [2,0,2]
|
||||
26
Task/Ordered-partitions/Haskell/ordered-partitions-3.hs
Normal file
26
Task/Ordered-partitions/Haskell/ordered-partitions-3.hs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import Data.Bifunctor (first, second)
|
||||
|
||||
-- choose m out of n items, return tuple of chosen and the rest
|
||||
|
||||
choose :: [Int] -> Int -> Int -> [([Int], [Int])]
|
||||
choose [] _ _ = [([], [])]
|
||||
choose aa _ 0 = [([], aa)]
|
||||
choose aa@(a : as) n m
|
||||
| n == m = [(aa, [])]
|
||||
| otherwise =
|
||||
(first (a :) <$> choose as (n - 1) (m - 1))
|
||||
<> (second (a :) <$> choose as (n - 1) m)
|
||||
|
||||
partitions :: [Int] -> [[[Int]]]
|
||||
partitions x = combos [1 .. n] n x
|
||||
where
|
||||
n = sum x
|
||||
combos _ _ [] = [[]]
|
||||
combos s n (x : xs) =
|
||||
[ l : r
|
||||
| (l, rest) <- choose s n x,
|
||||
r <- combos rest (n - x) xs
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ print $ partitions [5, 5, 5]
|
||||
Loading…
Add table
Add a link
Reference in a new issue