Created
November 1, 2016 21:51
-
-
Save vaibhavsagar/27205cf92a91ca26c893e1846d4a87de to your computer and use it in GitHub Desktop.
Week 12 code dojo with @lamarqua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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