Skip to content

Instantly share code, notes, and snippets.

@tanob
Created February 20, 2012 16:33
Show Gist options
  • Save tanob/1870017 to your computer and use it in GitHub Desktop.
Save tanob/1870017 to your computer and use it in GitHub Desktop.
-- 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