Last active
August 29, 2015 14:09
-
-
Save derekmcloughlin/91b331742a1064498ebb to your computer and use it in GitHub Desktop.
This file contains 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
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