Skip to content

Instantly share code, notes, and snippets.

@willnet
Created August 21, 2012 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willnet/3414923 to your computer and use it in GitHub Desktop.
Save willnet/3414923 to your computer and use it in GitHub Desktop.
http://qiita.com/items/cbc3af152ee3f50a822f の問題をhaskellで解いてみた
import Data.List
import Test.HUnit
poker :: String -> String
poker = check . split
split :: String -> [(String, String)]
split ('D':'1':'0':xs) = [("D", "10")] ++ split xs
split ('C':'1':'0':xs) = [("C", "10")] ++ split xs
split ('S':'1':'0':xs) = [("S", "10")] ++ split xs
split ('H':'1':'0':xs) = [("H", "10")] ++ split xs
split ('D':numberString:xs) = ("D", [numberString]) : split xs
split ('C':numberString:xs) = ("C", [numberString]) : split xs
split ('S':numberString:xs) = ("S", [numberString]) : split xs
split ('H':numberString:xs) = ("H", [numberString]) : split xs
split [] = []
countSameRank :: [String] -> String -> Int
countSameRank [] x = 0
countSameRank (y:ys) x
| x == y = 1 + (countSameRank ys x)
| otherwise = countSameRank ys x
check :: [(String, String)] -> String
check cards
| maxCount == 4 = "4K"
| maxCount == 3 = if rankCounts !! 1 == 2
then "FH"
else "3K"
| maxCount == 2 = if rankCounts !! 1 == 2
then "2P"
else "1P"
| otherwise = "--"
where
ranks = map snd cards
seeds = nub ranks
rankCounts = reverse . sort $ map (countSameRank ranks) seeds
maxCount = maximum rankCounts
tests = test ["FH" ~=? poker "D3C3C10D10S3"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment