My Weblog

Blog about programming and math

SPOJ Mirrored Pairs

Mirrored pairs is easy problem. You take a map and map the mirror words to each other. I tried it Haskell and accepted. Although this code does not look like more Haskell way so my apology to Haskell programmers ;).

import qualified Data.Map as M  
import Data.Char

m :: M.Map Char Char
m =  M.fromList [ ('b' , 'd' ) , ( 'd' , 'b' ) , ( 'p' , 'q' ) , ( 'q', 'p') ]

recurse :: IO ()
recurse = do 
	    line <- getLine
	    case and.map isSpace $ line of 
		True -> return ()
		_    -> do 
			  let (x:y:_) = line
			  case or [ M.lookup x m == Just y , M.lookup y m == Just x ] of 
					True -> putStrLn "Mirrored pair"
			  		_    -> putStrLn "Ordinary pair"
			  recurse
			  return ()
					

main :: IO ()
main = do 
	putStrLn "Ready"
	recurse
Advertisements

May 16, 2011 - Posted by | Programming

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: