Skip to content

Instantly share code, notes, and snippets.

@vaibhavsagar
Created November 1, 2016 21:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhavsagar/27205cf92a91ca26c893e1846d4a87de to your computer and use it in GitHub Desktop.
Save vaibhavsagar/27205cf92a91ca26c893e1846d4a87de to your computer and use it in GitHub Desktop.
Week 12 code dojo with @lamarqua
import Control.Applicative
import Control.Monad
import System.IO
import Data.List (foldl')
import Data.Maybe
main :: IO ()
main = do
t <- fmap read getLine :: IO Int
lns <- mapM (const getLine) [1..t]
mapM_ putStrLn (map valid lns)
matches o c = (o:c:[]) `elem` ["{}", "[]", "()"]
step Nothing c = Nothing
step (Just stack) c = case (c `elem` "({[", stack) of
(True, _) -> Just (c:stack)
(False, []) -> Nothing
(False, x:xs) -> if matches x c then Just xs else Nothing
valid = maybe "NO" (\o -> if o=="" then "YES" else "NO") . foldl' step (Just [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment