22

# 命名扑克手

``````High card
One pair
Two pair
Three of a kind
Straight
Flush
Full house
Four of a kind
Straight flush
Royal Flush
``````

## 输入项

### 卡示例

``````5H - five of hearts
``````

### 输入示例=>所需输出

``````3H 5D JS 3C 7C => One pair
JH 4C 2C JD 2H => Two pair
7H 3S 7S 7D 7C => Four of a kind
8C 3H 8S 8H 3S => Full house
``````

2

dmckee 2012年

daniero 2012年

3

## GolfScript（209 208 207 206 200 199 197 196个字符）

``````3/zip:^0={10,''*"TJQKA"+?}/]:?15,{?\{=}+,,}%2,-\$6,14.),++@\$/):|;[!!2*^1=.&,(!+5+]or{/}*'Full house
Two pair
One pair
ThreeKFourKHigh card
Flush
Straight''K'/' of a kind
'*n/~|1\$"Royal"if" "+2\$+](=
``````

@ w0lf，这是一个相当标准的技巧。霍华德在解决方案中也使用了它。

8

# 蟒- 312 301 298

``````R,K,F,S,g=' 23456789TJQKA2345A',' of a Kind','Flush','Straight ',sorted
s,r=''.join(g(raw_input(),key=R.find)).split()
n,m=g(map(r.count,set(r)))[-2:]
print[[F,[0,'High Card','TOwnoe'[n&1::2]+' Pair',['Full House','Three'+K][n&1],'Four'+K][m]],[[S,'Royal '][r[0]=='T']+F,S]][r in R][len(set(s))>1]``````

1
...击败我。

Nolen Royalty

daniero 2012年

1

Nolen Royalty

daniero 2012年

5

## Ruby 1.9（427359348338296 292）

``````o,p=%w(flush straight)
f=/1{5}|1{4}0+1\$/
s=[0]*13
puts Hash[*\$*.map{|c|s['23456789TJQKA'.index c[0]]+=1;c[1]}.uniq[1]?[f,p,?4,'four'+a=' of a kind',/3.*2|2.*3/,'full house',?3,'three'+a,/2.*2/,'two pair',?2,'one pair',0,'high card']:[/1{5}\$/,'royal '+o,f,p+' '+o,0,o]].find{|r,y|s.join[r]}[1]``````

``````>ruby poker-hand-golf.rb 3H 5D JS 3C 7C
one pair
``````

4

C，454个字符

``````#define L for(a=1;a<6;a++)for(b=0;b<13;b++)
#define U u[b+6]
#define R(x,y) if(x)puts(#y);else
b,f,r,h=0,s=0,u[20]={0};main(int a,char**v){L U+=v[a][0]=="23456789TJQKA"[b];f=v[1][1];L{if(v[a][1]!=f)f=0;u[a]+=a==U;if(b>7)h+=U;if(a*13+b<64||!U)r=0;else if(++r==5)s=1;}R(f&&h==25,Royal flush)R(f&&s,Straight flush)R(u[4],Four of a kind)R(u[3]&&u[2],Full house)R(f,Flush)R(s,Straight)R(u[3],Three of a kind)R(u[2]==2,Two pair)R(u[2],One pair)R(h,High card);}
``````

``````#define L for(a=1;a<6;a++)for(b=0;b<13;b++)
#define R(x,y) if(x)puts(#y);else
#define U u[b+6]
b,f,r,h=0,s=0,u[20]={0};
main(int a,char**v){
// card usage - u[6..]
L U+=v[a][0]=="23456789TJQKA"[b];
// NOTE: lets expand the inner body of the loop in the answer so this looks more sane:
// flush
f=v[1][1];L if(v[a][1]!=f)f=0;
// count of usages - u[0..5]
L u[a]+=a==U;
// high cards x5
L if(b>7)h+=U;
// straights
L if(a*13+b<64||!U)r=0;else if(++r==5)s=1;
// display
R(f&&h==25,Royal flush)
R(f&&s,Straight flush)
R(u[4],Four of a kind)
R(u[3]&&u[2],Full house)
R(f,Flush)
R(s,Straight)
R(u[3],Three of a kind)
R(u[2]==2,Two pair)
R(u[2],One pair)
R(h,High card);
}
``````

1. 通过组合和重用循环节省了12个字符。
2. 通过内联字符串常量节省了9个字符。
3. 通过在宏中使用字符串化节省了19个字符，令人讨厌。

3

## 数学 365

``````If[
a = Characters;
r = Range;
d = Sort[a@StringSplit@# /. x[a@"23456789TJQKA" -> 2~r~14]];
{t, u} = Sort[Last /@ Tally@#] & /@ x@d;
c = First /@ d;
f = u == {5};
S = "Straight";
c == r[b = d[[1, 1]], b + 4],
If[f,
If[c == 10~r~14, "Royal Flush", S <> " flush"], S],
If[f, "Flush",
Switch[t,
{_, 4},    "Four of a kind",
{2, 3},    "Full house",
{__, 3},   "Three of a kind",
{_, 2, 2}, "Two pair",
{__, 2},   "One pair",
_,         "High card"]
]
] &
``````

``````If[a=Characters;x=Thread;r=Range;d=Sort[a@StringSplit@#/.x[a@"23456789TJQKA"->2~r~14]];{t,u}=Sort[Last/@Tally@#]&/@x@d;c=First/@d;f=u=={5};S="Straight";c==r[b=d[[1,1]],b+4],If[f,If[c==10~r~14,"Royal Flush",S<>" flush"],S],If[f,"Flush",Switch[t,{_,4},"Four of a kind",{2,3},"Full house",{__,3},"Three of a kind",{_,2,2},"Two pair",{__,2},"One pair",_,"High card"]]]&
``````

DavidC

daniero

@Daniero谢谢。我会改名字。

3

# K，294 295

``````d:{F:"Flush";S:"Straight ";P:" Pair";K:" of a kind";\$[(f:1=#?,/-1#'c)&("AJKQT")~a@<a:,/j:1#'c:" "\:x;"Royal ",F;f&s:(4#1)~1_-':a@<a:,/(("A23456789TJQKA")!1+!14)@j;S,F;4=p:|/#:'=j;"Four",K;(2;3)~u:a@<a:,/#:'=j;"Full House";f;F;s;S;3=p;"Three",K;(1;2;2)~u;"Two",P;(1;1;1;2)~u;"One",P;"High Card"]}
``````

``````k)d'("TS JS QS KS AS";"3S 4S 5S 7S 6S";"JC JH KS JD JS";"JC JH 2S JD 2C";"2C 9C TC QC 6C";"8C 5D 9H 6C 7D";"8C 8D 9H 8S 7D";"8C 8D 9H 2S 9D";"8C 8D 4H 2S 9D";"3C 8D 4H 2S 9D")
"Royal Flush"
"Straight Flush"
"Four of a kind"
"Full House"
"Flush"
"Straight "
"Three of a kind"
"Two Pair"
"One Pair"
"High Card"
``````

3

### Python的334，326个 322字符

``````p,f,l,t,o=" pair"," of a kind"," Flush","Straight","A23456789TJQK"
v,u=zip(*raw_input().split())
s=''.join(sorted(v,key=o.find))
print{5:"High card",7:"One"+p,9:"Two"+p,11:"Three"+f,13:"Full house",17:"Four"+f,23:t,24:l[1:],25:t,42:t+l,44:"Royal"+l}[(sum(map(v.count,v)),24)[len(set(u))<2]+((0,20)[s=="ATJQK"],18)[s in o]]
``````

2

### GolfScript，258 250个字符

``````3/zip~;.&,(!\{"23456789TJQKA"?}%\$.(\{.@- 8%}%\;"\1"-!\.1/.&{1\$\-,}%1.\$?)"Four"" of a kind":k+{.,2="Full house"{.2\?)"Three"k+{.3-,({.3\?)"One pair"{;"Straight":?;2\$2\$&{(8="Royal"?if" flush"+}{;?{"Flush""High card"if}if}if}if}"Two pair"if}if}if}if])\;
``````

``````> 8C 3H 8S 8H 3S
Full house

> 8C 7H 6S TH 9S
Straight

> AH 3H 4S 2H 6S
High card
``````

Cristian Lupascu 2012年

@ w0lf谢谢。我将您的建议添加到了代码中。

@ w0lf Hmmm，我得考虑一下...

2

# 数学- 500 494 465个字符

``````v[x_] := Block[{d, t, c, f, s},
d = Sort@ToExpression[Characters[ImportString[x, "Table"][[1]]] /. {"T" -> 10, "J" -> 11, "Q" -> 12, "K" -> 13, "A" -> 14}];t = Sort /@ Map[Length, Split /@ Sort /@ Transpose@d, {2}];c = d[[All, 1]];f = (t[[2]] == {5});s = c == Range[b = d[[1, 1]], b + 4];
If[s,
If[f, If[c == 10~Range~14, "royal flush", "straight flush"],"straight"],
If[ f, "flush",
Switch[t[[1]],
{1, 4}, "four of a kind",
{2, 3}, "full house",
{1, 1, 3}, "three of a kind",
{1, 2, 2}, "two pair",
{1, 1, 1, 2}, "one pair",
{1, 1, 1, 1, 1}, "high card"]]]]
``````

f：齐平

c：卡片（无西装）

s：直

t：{卡，套房}

d：

daniero 2012年

DavidC 2012年

@Daniero您提出的问题已解决。
DavidC 2012年

Wizard先生2012年

@Wizard先生成为我的客人。我会观看和学习。
DavidC