Re:Most candidates cannot solve this interview problem
by hyperrealgopher on Sunday February 07, 2021 @03:15AM
(#26053510)
Great solution that avoids using any functions that do the heavy lifting for you! Thanks for showing!
> input = "aaaabbbcca"
> r :: String -> (Char,Int) -> [(String, Int)]
> r [] (c,n) = [([c],n)]
> r (x:xs) (c,n)
> | c == x = r xs (c,n+1)
> | otherwise = (([c],n): r xs (x,1))
> main = putStrLn (show (r (tail input) (head input, 1) ))
4 minutes 42 seconds.
And no, I don't write code like this normally, but I guess this is me when I'm under time pressure... anyway, I could refactor it nicely afterwards in the 25 minutes allocated.