Created
February 20, 2012 16:33
-
-
Save tanob/1870017 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Make: ghc --make groups.hs | |
-- Run: echo -e "[4,3,3,2]\n[5,3,2,1,1,3,5]\n[4,5]" | ./groups | |
import System.IO | |
import Data.List | |
isSameSolution f (s1,s2) = f == (s2,s1) | |
discardDuplicated = nubBy isSameSolution | |
uniqueSubsequences = filter ((/=0).length) . nub . subsequences | |
solutionsFor list = discardDuplicated solutions | |
where solutions = [(x,y) | x <- subsequencesOfList, y <- listWithout x, sum x == sum y] | |
subsequencesOfList = uniqueSubsequences list | |
listWithout x = [list \\ x] | |
output [] = "no solution\n" | |
output solution = unlines $ map show solution | |
main = do | |
theEnd <- isEOF | |
if theEnd | |
then return () | |
else do line <- getLine | |
putStr $ output $ solutionsFor (read line :: [Int]) | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment