Last active
August 29, 2015 14:05
-
-
Save profil/b66f4a727d1a171fc788 to your computer and use it in GitHub Desktop.
r/dailyprogrammer #175
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 System.Environment (getArgs) | |
import System.Random | |
import Data.List (permutations) | |
shuffle :: String -> IO String | |
shuffle xs = do | |
gen <- newStdGen | |
let (randIndex, _) = randomR (0, length ys - 1) gen | |
return $ ys !! randIndex | |
where | |
ys = permutations xs | |
bogo :: String -> String -> Int -> IO String | |
bogo str sorted acc = case str == sorted of | |
True -> return $ show acc ++ " iterations" | |
False -> do | |
newStr <- shuffle str | |
bogo newStr sorted (acc+1) | |
main = do | |
args <- getArgs | |
case args of | |
[str, sorted] -> do | |
answer <- bogo str sorted 0 | |
putStrLn answer | |
_ -> do | |
putStrLn "need two arguments" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment