Advertisement
Guest User

Coding challenge CoderBreakfast Avions

a guest
Feb 22nd, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- http://thecodersbreakfast.net/index.php?post/2013/02/18/Coding-challenge-maman-les-petits-avions
  2.  
  3. import Data.Char (ord)
  4.  
  5. syracuse :: [Int] -> [Int]
  6. syracuse n = case n of
  7.                 1 : _ -> reverse n
  8.                 x : _ -> if mod x 2 == 0 then syracuse((div x 2) : n) else syracuse((x*3+1) : n)
  9.  
  10. continous :: (Int -> Bool) -> [Int] -> [Int] -> [Int]
  11. continous f n r = case n of
  12.             [] -> r
  13.             x : y -> if f x then continous f (dropWhile f n) (length (takeWhile f n) : r) else continous f y r
  14.  
  15. getData :: Int -> [Int]
  16. getData n = let syr = syracuse [n] in
  17.             [length syr, maximum (continous (>=n) syr []), maximum syr]
  18.  
  19. challenge :: String -> [Int]
  20. challenge n = getData (sum (map (ord) n))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement