Skip to content

Instantly share code, notes, and snippets.

@derekmcloughlin
Last active August 29, 2015 14:09
Show Gist options
  • Save derekmcloughlin/91b331742a1064498ebb to your computer and use it in GitHub Desktop.
Save derekmcloughlin/91b331742a1064498ebb to your computer and use it in GitHub Desktop.
import Data.List
import Data.Ord
import Test.QuickCheck
import Test.HUnit
getSubSeries :: [Int] -> Int -> [Int]
getSubSeries [] _ = []
getSubSeries series threshold = head [x | x <- sortBy (flip $ comparing length) $ subLists series,
sum x <= threshold]
where
subLists xs = ( ([] :) . concatMap (tail . inits) . tails) xs
testSeries :: [Int]
testSeries = [100, 300, 100, 50, 50, 50, 50, 50, 500, 200, 100]
-- QuickCheck general property: any result must be a sub-list of the original list
-- Load in GHCI:
-- quickCheck prop_getSubSeries_1
prop_getSubSeries_1 :: [Int] -> Bool
prop_getSubSeries_1 xs = getSubSeries xs 500 `isInfixOf` xs
-- Test Case
test1 :: Test
test1 = TestCase (assertEqual "test1" (getSubSeries testSeries 500) [100, 50, 50, 50, 50, 50])
tests :: Test
tests = TestList [TestLabel "test1" test1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment