命名吹牛之手


11

背景

吹牛是一种纸牌游戏,其概念类似于扑克,但比扑克简单。吹牛由三张牌组成,从高到低排名如下:

  • 三种-所有三张牌的等级相同。命名为“三王”等

  • 运行冲洗,也称为直接冲洗。相同花色和连续等级的所有三张牌。该手牌由三张牌以升序命名,后跟“弹跳”字样,以区别于简单的奔跑/直行,例如“弹跳时十个插孔的女王”。请注意,王牌是高位还是低位,但不是两者都高-“王牌王牌二”不是一轮。

  • 直奔。如上所述,但不需要搭配西装。简单地命名为例如“十个杰克女王”。

  • 同花大顺-所有三张牌都相同,以最高级别命名,例如“ As同花顺”。

  • 配对-两张同等级的牌,另一张同等级的牌。命名为“三元组”等

  • 以最高等级命名的任何其他组合,例如“王牌高”。

挑战

给定三张纸牌,输出他们输出的吹牛手的名称。

卡片将以三个2个字符的字符串输入,或者以单个6个字符的字符串连接(无论您的实现方式是哪一个),其中每对中的第一个是等级(2 ... 9,T,J,Q, K,A),第二个表示西装(H,C,D,S)。

适用标准高尔夫球规则-编写程序或函数以接受此输入并输出手的名称,如上所述。

您可以假定输入将是有效的(在上述范围内,等级和花色,没有重复的卡片),并且在任何情况下都可以使用,但顺序不是特定的。

输出必须全部大写,全部小写或大写,例如标题大小写或句子大小写。数字等级应拼写清楚,例如“十”而不是10。

样本输入和输出:

2H3C2D => "pair of twos"

TD8C9C => "eight-nine-ten"

4SKS9S => "king flush"

4D4H4S => "three fours"

5H3H2C => "five high"

2D3DAD => "ace-two-three on the bounce"

6D6C6H => "three sixes"

这是我第一次尝试在此站点上进行挑战,请提出一些改进建议,但要谨慎:)



4
欢迎来到PPCG!到目前为止,我只是略过了挑战,但是对于第一个挑战来说看起来不错。就是说,要编写好的挑战是困难的,未来,我建议您先在沙盒中发布想法,在此之前,您可以获取反馈并改进规范的详细信息,然后再冒险投票,关闭投票和回答,而这些更改可能会因以后的更改而无效。挑战。
Martin Ender

@MartinEnder谢谢!下次我一定会看一下沙盒。
IanF1

我们可以以元组数组的形式获取输入吗?另外,我们可以将“ king flush”之类的输出缩短为“ fk”吗?
马修·鲁

1
请添加"6D6C6S"一个测试用例,因为六个是奇数个复数
并不是查尔斯(Charles)

Answers:


2

红宝石,384,320

接受两个字符的字符串数组。

将点值转换为十六进制值,并根据有多少个不同的点值来识别手。

->*d{u=d.map{|x|*u=x[1]}==u*3
g=d.map{|x|(x[0].tr'TJQKA','ABCDE').hex}.sort
g=1,2,3if[2,3,14]==g
_,l,h=a=g.map{|x|%w{king queen jack ten nine eight seven six five four three two ace}[-x%13]}
[*g[0]..2+g[0]]==g ?a*?-+(u ?' on the bounce':''):u ?h+' flush':[h+' high','pair of '+l+=l[?x]?'es':?s,'three '+l][-g.uniq.size]}

注释:

->*d{
    # u is "Is this a flush?"" (see if you have more than one suit)
    u=d.map{|x|u=x[1]}==[u]*3

    # g is the sorted card values in integer (convert to base 16)
    g=d.map{|x|x[0].tr('TJQKA','ABCDE').hex}.sort

    # use Ace == 1 if we have a low straight
    g=[1,2,3]if[2,3,14]==g

    # a is the names of all the cards
    a=g.map{|x|%w{ace two three four five six seven eight nine ten jack queen king ace}[x-1]}

    # l is for "plural" - just choose the middle card because we
    #                     only care about plurals for 2s or 3s
    l=a[1].sub(?x,'xe')+?s

    # if [g[0],g[0]+1,g[0]+2] == g, we have a run
    # possibly "on the bounce"
    ([*g[0]..g[0]+2]==g) ? (a * ?-) + (u ? ' on the bounce' : '') :

    # if we have a flush, we can't have three-of-a-kind, so try that first
    u ? a[2]+' flush' :

    # otherwise, dedupe your hand. if there's: 
    # 3 values, x high; 2 values, pair; 1 value, three
    [a[2]+' high','pair of '+l,'three '+l][-g.uniq.size]
}

3

Python 2中788,715,559,556,554,546,568, 522个字节

*现在通过了“六个” *感谢本·弗兰克尔节省了46个字节!


import re
d,m,n=dict(zip('JQKA',range(10,15))),'pair of %ss','%s-%s-%s'
C=lambda s:int(d.get(s[0],s[0]))
z,x,c=sorted(re.findall('..',raw_input()),key=C)
q,w,e=C(z),C(x),C(c)
A=[0,0,'two','three','four','five','six','seven','eight','nine','ten','jack','queen','king','ace']
I,O,U=A[e],A[w],A[q]
a,k='%s high'%I,e-w+q
if k==13:a=n%(I,U,O)
if k==w:a=n%(U,O,I)
if q==w or e==w or e==q:a=m%O
if k==e==w:a='three %ss'%I
if'x'in a:a=a[:-1]+'es'
if z[-1]==x[-1]==c[-1]:
 if'-'in a:a+=' on the bounce'
 else:a='%s flush'%I
print a

在线尝试!

感谢您的第一酷挑战!


1
一些空白高尔夫建议:TIO
数学迷

谢谢!我知道空白会增加很多字节,但是我认为它需要4个空格。编辑!@math_junkie
斯蒂芬(

@ user7686415或者您也可以使用实际的选项卡。
mbomb007 '17

1
@NotthatCharles修复了它!
斯蒂芬

1
@斯蒂芬,当然。D.get(a, b)表示访问键a中dict D中的值,如果找不到键,则使用默认值b。它与写作相同D[a] if a in D else b,也与写作相同D[a] if a in D.keys() else b
本·弗兰克尔

2

PHP,413个 405 398 409 408 406 398字节

不幸的是,PHP不支持在字符串内部引用嵌套数组。
那会节省另外6 5个字节。

for(;$a=$argn[$i++];)$i&1?$v[strpos(_3456789TJQKA,$a)]++:$c[$a]++;$k=array_keys($v);sort($k);$n=[two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace];echo($m=max($v))<2?($k[!$d=count($c)]+2-($h=$k[2])?$k[1]>1|$h<12?"$n[$h] ".[flush,high][$d++/2]:"ace-two-three":$n[$k[0]]."-".$n[$k[1]]."-$n[$h]").[" on the bounce"][$d^1]:($m<3?"pair of ":"three ").$n[$v=array_flip($v)[$m]].e[$v^4].s;

在线运行echo <hand> | php -nR '<code>或对其进行测试

分解

for(;$a=$argn[$i++];)$i&1?      # loop through input
    $v[strpos(_3456789TJQKA,$a)]++  # count values on even positions [0,2,4]
    :$c[$a]++;                      # count colors on odd positions [1,3,5]
$k=array_keys($v);sort($k);     # $k=ascending values
$n=[two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace];
echo($m=max($v))<2              # three different values:
?($k[!$d=count($c)]+2-($h=$k[2])    # test normal straight ($d=color count, $h=high card)
    ?$k[1]>1|$h<12                      # test special straight
        ?"$n[$h] ".[flush,high][$d++/2]     # flush if one color, high card if not
                                            #   ($d++ to avoid " on the bounce")
        :"ace-two-three"                    # special straight
    :$n[$k[0]]."-".$n[$k[1]]."-$n[$h]"  # normal straight
).[" on the bounce"][$d^1]          # if straight: straight flush if one color
:($m<3?"pair of ":"three ")     # pair or triplet
    .$n[$v=array_flip($v)[$m]]      # card name
    .e[$v^4].s                      # plural suffix
;

需要PHP> = 5.6(用于e[...]


1
这可能会失败“六个”
并不是说查尔斯

1
@NotthatCharles:这花了我11个字节...但是我打了回去。:)
Titus

1

Python 2-583字节

我太新了,无法评论帖子,所以我只发布了我的python solusion版本。

解决了对和六分之三的“ es”问题。感谢Not that Charles

d={'A':['ace',14],'2':['two',2],'3':['three',3],'4':['four',4],'5':['five',5],'6':['six',6],'7':['seven',7],'8':['eight',8],'9':['nine',9],'T':['ten',10],'J':['jack',11],'Q':['queen',12],'K':['king',13]}
r=input()
j=1
i=lambda x:d[x][j]
v=sorted(r[::2],key=i)
z,y,x=v
s=r[1::2]
e='es'if i(y)==6else's'
j=0
a=i(x)
if z==y or y==x:r="pair of %s"%i(y)+e
if s[0]*3==s:r="%s flush"%a
t="%s-%s"%(i(z),i(y))
j=1
u=" on the bounce"if r[-1]=='h'else ""
if i(z)+i(x)==2*i(y):r=t+"-%s"%a+u
if ''.join(v)=="23A":r="%s-"%a+t+u
if [z]*3==v:r="three %s"%d[z][0]+e
if len(r)==6:r="%s high"%a
print r

带有一些注释的可读性更高

# first of all we don't need to keep suits
d={'A':['ace',14],'2':['two',2],'3':['three',3],'4':['four',4],'5':['five',5],'6':['six',6],'7':['seven',7],'8':['eight',8],'9':['nine',9],'T':['ten',10],'J':['jack',11],'Q':['queen',12],'K':['king',13]}
r=input()                           # input placed in r, to safely check r[-1] later in code
j=1                                 # j toggles reading from dictionary: 0-string, 1-value
i=lambda x:d[x][j]                  # lambda used to access dictionary
v=sorted(r[::2],key=i)              # take values from input and sort
z,y,x=v                             # variables to compact code
s=r[1::2]                           # take suits from input
e='es'if i(y)==6else's'             # choose ending 'es' for six and 's' for others (for pair and three)
j=0                                 # toggle reading from dictionary to string
a=i(x)                              # get string of top most value
if z==y or y==x:                    # check only two pairs as values are sorted
    r="pair of %s"%i(y)+e
if s[0]*3==s:                       # compact check if all string characters are equal to detect flush
    r="%s flush"%a
t="%s-%s"%(i(z),i(y))               # part of straight output - first two values
j=1                                 # toggle reading from dictionary to values
u=" on the bounce"\                 # addon to output in case of possible straight flush
if r[-1]=='h'else ""                # detected by checking last character in r
                                    # which would be 'h' if flush was detected
if i(z)+i(x)==2*i(y):               # check straight - three sorted numbers a,b,c would be in line if a+c == 2*b
    r=t+"-%s"%a+u                   
if ''.join(v)=="23A":               # check special case with straight, started from Ace
    r="%s-"%a+t+u  
j=0                                 # toggle reading from dictionary to string
if [z]*3==v:                        # check three equal values (almost the same as flush check)
    r="three %s"%d[z][0]+e
if len(r)==6:                       # if r was never modified, then it's just one high card
    r="%s high"%a
print r                             # output r

也可能在最后一行更改j=0; if [z]*3==v:r="three %ss"%i(z)为,if [z]*3==v:r="three %ss"%d[z][0]但是仅保存1个字节
Dead Possum

1
这可能会失败“六个”
并不是查尔斯(Charles)

1
@NotthatCharles是的,谢谢您的注意。我已添加修复程序
Dead Possum
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.