Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Created October 31, 2011 01:44
Show Gist options
  • Save pervognsen/1326716 to your computer and use it in GitHub Desktop.
Save pervognsen/1326716 to your computer and use it in GitHub Desktop.
-- Solution to http://beust.com/weblog/2011/10/30/a-new-coding-challenge/
import Data.List(isInfixOf)
partitions n 1 = [[n]]
partitions n k | n > 0 = [m:ms | m <- [1..n `div` 2], ms <- partitions (n-m) (k-1)]
partitions n k | otherwise = []
weighings [] = [0]
weighings (w:ws) = [m + n | m <- weighings ws, n <- [-w, 0, w]]
-- solve 40 4 returns [[1,3,9,27]]
solve n k = [p | p <- partitions n k, [1..n] `isInfixOf` (weighings p)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment