[ create a new paste ] login | about

Link: http://codepad.org/HS8z6MZd    [ raw code | output | fork | 1 comment ]

mohit_at_codepad - Haskell, pasted on Feb 27:
-- Written by Mohit Jain
module Main where

-- Import List to enable sort
import List

-- Remove zero from input
-- Convert 10 into 1 so that all card face value is 1 character long
-- Card 1 is represented by A
removeZero x = filter ( (/=) '0' ) x

-- Remove the type of card
-- As we don't care whether it is heart/space/club/diamond
removeType []       = []
removeType (_:x:xs) = x : removeType xs

-- To check poker hand
-- AaBbCcDdEe
-- If any of a/b/c/d/e/f is 10, change it to 1 : removeZero
-- Ignore A/B/C/D/E/F : removeType
-- Sort remaining card values so that similar cards are together (any order) : sort
-- Group similar cards to single list : group
-- Change lists to length: map length
-- Sort the length of similar hands (High to low) : sort + reverse
-- Possible list is (4,1), (3,2),(3,1,1),(2,2,1),(2,1,1,1),(1,1,1,1,1)
-- Convert above lists to output string: toPokerString
poker= analyzeHand.group.sort.removeType.removeZero
analyzeHand = toPokerStr.(sortBy $ flip compare).(map length)
toPokerStr [4,1]  = "4K"
toPokerStr [3,2]  = "FH"
toPokerStr (3:_)  = "3K"
toPokerStr [2,2,1]= "2P"
toPokerStr (2:_)  = "1P"
toPokerStr _      = "--"

main = putStrLn $ unlines $ map poker inputList
inputList = [
    "H6D6D4C6S6", "S7D7C7H8H7", "SADAHACAS8", "C4H4D6D4S4", "DAC9HASACA",
    "H2C2S2D2H9", "C3H8D8C8S8", "S10C9D10H9H10", "CAHAS10H10D10", "D5H5S5H3C3",
    "D3C3C10D10S3", "D7H7SADAHA", "D7CAC7SAHA", "CKHKC7D7DK", "D3H3DKS3C7",
    "S8H2C8H7H8", "S3SAD2H3C3", "CADAC9C7SA", "SAD7S7C3C7", "H10S4D3S3C3",
    "HJDKC7SKHK", "D3H3S5H10C5", "S3H8C8D7D3", "H5S3C5H4D4", "S8D10HJS10CJ",
    "C9D8H4D4D9", "C10CAH5S10C5", "H9S10H10DASA", "H10CJH6C7D7", "C3C10DKS4CK",
    "SJH6S7C8D6", "HKS5C10S8H5", "HKS5H10H3SK", "HAHJC2DACK", "D2S2C9H4C8",
    "DQC4SQD5CK", "D4H3HJSKS10", "S3C5D9S6DA", "CJD5H9H6D2", "H6C10D9S3H7",
    "H9DJHQH7C8", "C7D3CJD2S9"]

-- eof


Output:
4K
4K
4K
4K
4K
4K
4K
FH
FH
FH
FH
FH
FH
FH
3K
3K
3K
3K
3K
3K
3K
2P
2P
2P
2P
2P
2P
2P
1P
1P
1P
1P
1P
1P
1P
1P
--
--
--
--
--
--



Create a new paste based on this one


Comments:
posted by mohit_at_codepad on Mar 20
Problem statement:
http://moritaro.sakura.ne.jp/atq/simpoka/
(Page
contains some typographical mistake in example)

Review Comments:
1. removeType can be implemented using filter with lambda function:
removeType = filter (\x -> notElem x "HSDC")
2. In removeZero; argument is superfluous. It should be:
removeZero = filter ( (/=) '0' )
reply