Guest User

Untitled

a guest
May 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. import qualified Data.List as List (permutations)
  2.  
  3. burst :: [Int] -> (Int, [Int])
  4. burst = burstNaive
  5.  
  6. burstNaive :: [Int] -> (Int, [Int])
  7. burstNaive = foldl (a@(aVal,_) e@(eVal,_) -> if eVal > aVal then e else a) (0, []) . allOrders
  8.  
  9. allOrders :: [Int] -> [(Int, [Int])]
  10. allOrders [] = []
  11. allOrders l@(x:xs) = map (p -> (flip withOrder l p, p))$ List.permutations [0..length l - 1]
  12.  
  13. type IndexOrder = [Int]
  14. withOrder :: IndexOrder -> [Int] -> Int
  15. withOrder [] _ = 0
  16. withOrder (i:is) xs = left * (xs !! i) * right + withOrder adjust xss
  17. where
  18. xss = remAt i
  19. adjust = map (index -> if index > i then index - 1 else index) is
  20. left = if i == 0 then 1 else xs !! (i-1)
  21. right = if i == (length xs - 1) then 1 else xs !! (i+1)
  22. remAt index = let (f,s) = splitAt index xs in f ++ drop 1 s
  23.  
  24. Prelude> :l Balloon.hs
  25. [1 of 1] Compiling Balloon ( Balloon.hs, interpreted )
  26. Ok, one module loaded.
  27. *Balloon> burst [3,1,5,8]
  28. (167,[1,2,0,3])
  29. *Balloon>
Add Comment
Please, Sign In to add comment