Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import Debug.Trace
  2.  
  3. data TestCase = Test [Int] Int Bool deriving (Show)
  4.  
  5. tests :: [TestCase]
  6. tests = [
  7. Test [23, 5, 4, 7, 2, 11] 20 True,
  8. Test [1, 3, 5, 23, 2] 8 True,
  9. Test [1, 3, 5, 23, 2] 7 False,
  10. Test [1, 3, 5, 23, 2] 23 True,
  11. Test [1, 3, 5, 23] 23 True,
  12. Test [0, 1, 3, 5, 23] 23 True,
  13. Test [1] 7 False,
  14. Test [1] 1 True,
  15. Test [22] 1 False
  16. ]
  17.  
  18. main :: IO()
  19. main = sequence_ . map (\test -> putStrLn . (++) (show test ++ " -> ") . runTestCase $ test) $ tests
  20.  
  21.  
  22. runTestCase :: TestCase -> String
  23. runTestCase (Test arr num expected) = if testPassed then "PASSED" else "FAILED"
  24. where testPassed = (subsequenceFound arr num) == expected
  25.  
  26.  
  27. subsequenceFound :: [Int] -> Int -> Bool
  28. subsequenceFound arr num = subsequenceFound' [] arr 0 num
  29.  
  30.  
  31. subsequenceFound' :: [Int] -> [Int] -> Int -> Int -> Bool
  32. subsequenceFound' buffer remaining currentSum expectedSum
  33. | currentSum == expectedSum = True
  34. | currentSum > expectedSum = subsequenceFound' bt remaining (currentSum - bh) expectedSum
  35. | remaining == [] = False
  36. | otherwise = subsequenceFound' (buffer ++ [rh]) rt (currentSum + rh) expectedSum
  37. where rh = head remaining
  38. rt = tail remaining
  39. bh = head buffer
  40. bt = tail buffer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement