tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
15
Task/Ordered-Partitions/Haskell/ordered-partitions-3.hs
Normal file
15
Task/Ordered-Partitions/Haskell/ordered-partitions-3.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
-- choose m out of n items, return tuple of chosen and the rest
|
||||
choose aa _ 0 = [([], aa)]
|
||||
choose aa@(a:as) n m
|
||||
| n == m = [(aa, [])]
|
||||
| otherwise = map (\(x,y) -> (a:x, y)) (choose as (n-1) (m-1)) ++
|
||||
map (\(x,y) -> (x, a:y)) (choose as (n-1) m)
|
||||
|
||||
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 = mapM_ print $ partitions [5,5,5]
|
||||
Loading…
Add table
Add a link
Reference in a new issue