Last active
February 8, 2017 03:05
-
-
Save shmookey/0080fd1c03006709ba820f3106dc22a4 to your computer and use it in GitHub Desktop.
words that rot13 to other words
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 Data.Char (chr, ord) | |
import Data.List (sortOn) | |
rot13 :: String -> String | |
rot13 = map rot where | |
rot x = | |
let | |
n = ord x | |
isUpper = n >= 65 && n <= 90 | |
isLower = n >= 97 && n <= 122 | |
in | |
if isUpper then chr (((n - 65 + 13) `mod` 26) + 65) | |
else if isLower then chr (((n - 97 + 13) `mod` 26) + 97) | |
else x | |
showPair :: (String, String) -> IO () | |
showPair (x,y) = do | |
putStr x | |
putStr " / " | |
putStrLn y | |
main :: IO () | |
main = do | |
dict <- (reverse . sortOn length . lines) <$> readFile "/usr/share/dict/web2" | |
let rdict = map rot13 dict | |
let results = filter (flip elem dict . snd) (zip dict rdict) | |
mapM_ showPair results |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment