Skip to content

Instantly share code, notes, and snippets.

@mattsan
Created June 25, 2012 11:46
Show Gist options
  • Save mattsan/2988136 to your computer and use it in GitHub Desktop.
Save mattsan/2988136 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <algorithm>
void removeSuit(std::string::value_type& c)
{
switch(c)
{
case 'S':
case 'H':
case 'D':
case 'C':
c = ' ';
}
}
int second(const std::map<std::string, int>::value_type& value)
{
return value.second + '0';
}
std::string poker(const std::string& cards)
{
std::string s = cards;
std::for_each(s.begin(), s.end(), removeSuit);
std::istringstream ss(s);
std::string n;
std::map<std::string, int> m;
while(ss >> n)
{
++m[n];
}
std::string t(m.size(), ' ');
std::transform(m.begin(), m.end(), t.begin(), second);
std::sort(t.begin(), t.end());
if(t == "1112") { return "1P"; }
if(t == "113") { return "3K"; }
if(t == "122") { return "2P"; }
if(t == "14") { return "4K"; }
if(t == "23") { return "FH"; }
return "--";
}
// This program needs Google Test 1.6.0 ( see http://code.google.com/p/googletest/ )
#include <gtest/gtest.h>
#include <string>
std::string poker(const std::string& cards);
TEST(pokerTest, test)
{
ASSERT_EQ(poker("S8S9HJS10D8"), "1P");
ASSERT_EQ(poker("S8D8HJS10CQ"), "1P");
ASSERT_EQ(poker("S5C8HJD8CQ"), "1P");
ASSERT_EQ(poker("S2C8H8DJCQ"), "1P");
ASSERT_EQ(poker("S2C8H3D8CQ"), "1P");
ASSERT_EQ(poker("S2C8H2DJCQ"), "1P");
ASSERT_EQ(poker("S2C8H3D2CQ"), "1P");
ASSERT_EQ(poker("S2C10H8D8CQ"), "1P");
ASSERT_EQ(poker("S2C5H8DJC8"), "1P");
ASSERT_EQ(poker("S2C8H4DJCJ"), "1P");
ASSERT_EQ(poker("S8C8HJSJD2"), "2P");
ASSERT_EQ(poker("S8D8HJS10CJ"), "2P");
ASSERT_EQ(poker("S5CJHJD5CQ"), "2P");
ASSERT_EQ(poker("S2C8H8DJC2"), "2P");
ASSERT_EQ(poker("S2C2H3DQCQ"), "2P");
ASSERT_EQ(poker("S2C8H8DQCQ"), "2P");
ASSERT_EQ(poker("SQC8H3D3CQ"), "2P");
ASSERT_EQ(poker("S2C10H2D10CQ"), "2P");
ASSERT_EQ(poker("S2C5H2DJH5"), "2P");
ASSERT_EQ(poker("S2C8H2DJCJ"), "2P");
ASSERT_EQ(poker("S2CJH3D2CJ"), "2P");
ASSERT_EQ(poker("S8C8H8SJD2"), "3K");
ASSERT_EQ(poker("S8D8HJH8C2"), "3K");
ASSERT_EQ(poker("S5C5HJD7D5"), "3K");
ASSERT_EQ(poker("S2C8H2D2C9"), "3K");
ASSERT_EQ(poker("S2CJH2DQC2"), "3K");
ASSERT_EQ(poker("S2C8HQD2C2"), "3K");
ASSERT_EQ(poker("SQC8H8D8C2"), "3K");
ASSERT_EQ(poker("S2C10H10D4D10"), "3K");
ASSERT_EQ(poker("S2C5HKD5H5"), "3K");
ASSERT_EQ(poker("S2C8H3D3C3"), "3K");
ASSERT_EQ(poker("S8C8H8D8D2"), "4K");
ASSERT_EQ(poker("S8D8H8HJC8"), "4K");
ASSERT_EQ(poker("S5C5HJH5D5"), "4K");
ASSERT_EQ(poker("S2C8H2D2C2"), "4K");
ASSERT_EQ(poker("SKS2H2D2C2"), "4K");
ASSERT_EQ(poker("S8C8HJDJCJ"), "FH");
ASSERT_EQ(poker("S8DJH8HJCJ"), "FH");
ASSERT_EQ(poker("S5C2H2H5D2"), "FH");
ASSERT_EQ(poker("S2C8H8D8C2"), "FH");
ASSERT_EQ(poker("SKS2H2DKCK"), "FH");
ASSERT_EQ(poker("SKS2HKD2CK"), "FH");
ASSERT_EQ(poker("SKS2HKDKC2"), "FH");
ASSERT_EQ(poker("SKDKH2D2CK"), "FH");
ASSERT_EQ(poker("C8S8H2D8C2"), "FH");
ASSERT_EQ(poker("SKCKHKD2C2"), "FH");
ASSERT_EQ(poker("S1C2H3D4C5"), "--");
ASSERT_EQ(poker("S8S7S6S5S4"), "--");
}
import List
parse [] rs = sort $ map length $ group $ sort rs
parse (_:'1':'0':ss) rs = parse ss ('p':rs)
parse (_:d:ss) rs = parse ss (d:rs)
poker cs = case parse cs [] of
[1,1,1,1,1] -> "--"
[1,1,1,2] -> "1P"
[1,2,2] -> "2P"
[1,1,3] -> "3K"
[2,3] -> "FH"
[1,4] -> "4K"
main = do
putStrLn $ poker "D3C3C10D10S3"
putStrLn $ poker "S8D10HJS10CJ"
import List(group, sort)
import Data.Map(fromList, (!))
parse = sort.(map length).group.sort.words.(foldr (\x xs -> (if elem x "SHDC" then ' ' else x):xs) [])
poker cs = fromList [([1,1,1,1,1], "--"), ([1,1,1,2], "1P"), ([1,2,2], "2P"), ([1,1,3], "3K"), ([2,3], "FH"), ([1,4], "4K")] ! (parse cs)
main = do
putStrLn $ poker "D3C3C10D10S3"
putStrLn $ poker "S8D10HJS10CJ"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment