Created
April 12, 2012 16:16
-
-
Save Abizern/2368757 to your computer and use it in GitHub Desktop.
Solution to Google Code Jam puzzle "Reverse Words" in Haskell
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
module Main where | |
{- | |
- Problem Statement: | |
- http://code.google.com/codejam/contest/351101/dashboard#s=p1 | |
- | |
- Usage either compile or use runhaskell / runghc | |
- Pass the input file as the sole command line argument | |
- Redirect output if you want the results to go in a file | |
-} | |
import IO | |
import System.Environment | |
import Data.List | |
reverseWords :: String -> String | |
reverseWords = unwords . reverse . words | |
boilerPlate :: [String] | |
boilerPlate = ["Case #" ++ show n ++ ": " | n <- [1..]] | |
standardOutput :: [String] -> [String] | |
standardOutput = zipWith (++) boilerPlate | |
main = do | |
(f:_) <- getArgs | |
file <- readFile f | |
let cases = tail $ lines file | |
solutions = standardOutput $ map reverseWords cases | |
putStrLn $ unlines $ solutions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment