Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.