单词扑克,谁赢?


14

输入将是两个五个字母的单词。实际上,它们不一定是字典单词,每个单词只需要五个字母(全部小写或全部大写)即可。输入的单词中只会出现AZ,并且长度始终为5个字符。

您的程序是对他们都打分,就像他们是扑克手一样,并输出较高的手牌。当然,西装不会在这里适用,仅适用于排名,因此不会出现同花顺。

典型的扑克排名系统为:“ 1对”,“ 2对”,“ 3类”,“直发”,“满屋”,“ 4类”,“ 5类”,当然这只手(或本例中的单词)可能一文不值。

如果是平局,则认为字母A较接近,因此一对As胜过一对B。在某些情况下,两只手可能是相同的,但顺序不同(或不相同),在这种情况下,输出的要么是手,要么是手的替代版本。

该外部页面包含有关如何识别获胜者的信息,特别是解决特定排名中的平局的情况,以防您不熟悉如何得分。

如果是直线,则字母必须在字母表中相邻,并且不能环绕。因此,“ defgh”在任何顺序上都是直的,“ xyzab”则不是。

如何单手得分的示例:

word   | scored as
---------------------
ccccc  | 5 of a kind     <-- highest ranking
woooo  | 4 of a kind
opopo  | full house
vurst  | straight
vovvu  | 3 of a kind
ppoww  | 2 pairs
upper  | 1 pair
kjsdf  | high card only (in this case D) <-- lowest ranking

因此,该程序实际上将产生如下结果:

input        |  output
-----------------------
voviu,kjsdf  |  voviu     because a pair beats nothing 
opoqo,upper  |  opoqo     because 3 of a kind beats a pair
woooo,ggegg  |  ggegg     because 4 Gs beats 4 Os
queue,hopup  |  queue     because 2 pairs beats 1 pair
lodpl,ddkop  |  ddkop     because pair DD beats pair LL
huhyg,hijht  |  huhyg     both have pair HH, but G beats I
ddffh,ccyyz  |  ccyyz     both have 2 pairs, but CC(yyz) beats DD(ffh)
okaok,nkunk  |  nkunk     KK ties with KK, but NN beats OO
abcdf,bcdef  |  bcdef     because it is a straight
qtery,retyq  |  qtery     identical! so doesnt matter
abedc,vyxwz  |  abedc     because it is a "higher" straight
hhhij,hijkl  |  hijkl     because straight beats 3 of a kind
aaabb,zzzzz  |  zzzzz     because nothing beats 5 of a kind

输入和输出中字母的顺序无关紧要,因此输出中的字母顺序可以不同于输入,但是需要提供相同的字母清单。

输出必须恰好包含五个字母-不能多也不能少。

通用的代码高尔夫规则适用。最短的代码获胜。

Answers:


4

的JavaScript(224个 218 213字节)

s=>t=>(v=s=>(o={},l=n=0,z=3.5,[...s].sort().map(c=>(n+=o[c]=-~o[c],z*=!l|l+1==(l=c.charCodeAt()))),[n+z,Object.keys(o).sort((a,b)=>o[b]-o[a]||(o[b]>o[a]?1:-1))]),w=v(s),x=v(t),w[0]>x[0]||w[0]==x[0]&&w[1]<x[1]?s:t)

取消高尔夫:

s=>t=>(
  v=s=>(
    o={},
    l=n=0,
    z=3.5,
    [...s].sort().map(c=>(
      n+=o[c]=-~o[c],
      z*=!l|l+1==(l=c.charCodeAt())
    )),
    [n+z,Object.keys(o).sort((a,b)=>o[b]-o[a]||(o[b]>o[a]?1:-1))]
  ),
  w=v(s),x=v(t),
  w[0]>x[0] || w[0]==x[0] && w[1]<x[1] ? s : t
)

map()运行时,n + z确定手的排名:

在此处输入图片说明

(您可以理解为什么我初始化z为3.5。)

如果是平局,Object.keys(o).sort()则用于确定较高排名的手。

片段:

f=

s=>t=>(v=s=>(o={},l=n=0,z=3.5,[...s].sort().map(c=>(n+=o[c]=-~o[c],z*=!l|l+1==(l=c.charCodeAt()))),[n+z,Object.keys(o).sort((a,b)=>o[b]-o[a]||(o[b]>o[a]?1:-1))]),w=v(s),x=v(t),w[0]>x[0]||w[0]==x[0]&&w[1]<x[1]?s:t)

console.log(/voviu/.test(f('voviu')('kjsdf')))       //because a pair beats nothing 
console.log(/opoqo/.test(f('opoqo')('upper')))       //because 3 of a kind beats a pair
console.log(/ggegg/.test(f('woooo')('ggegg')))       //because 4 Gs beats 4 Os
console.log(/queue/.test(f('queue')('hopup')))       //because 2 pairs beats 1 pair
console.log(/ddkop/.test(f('lodpl')('ddkop')))       //because pair DD beats pair LL
console.log(/huhyg/.test(f('huhyg')('hijht')))       //both have pair HH, but G beats I
console.log(/ccyyz/.test(f('ddffh')('ccyyz')))       //both have 2 pairs, but CC(yyz) beats DD(ffh)
console.log(/nkunk/.test(f('okaok')('nkunk')))       //KK ties with KK, but NN beats OO
console.log(/bcdef/.test(f('abcdf')('bcdef')))       //because it is a straight
console.log(/qtery|retyq/.test(f('qtery')('retyq'))) //identical! so doesnt matter
console.log(/abedc/.test(f('abedc')('vyxwz')))       //because it is a "higher" straight
console.log(/hijkl/.test(f('hhhij')('hijkl')))       //because straight beats 3 of a kind
console.log(/zzzzz/.test(f('aaabb')('zzzzz')))       //because nothing beats 5 of a kind


3

果冻 28 27 29  27 个字节

+2&-2修复错误,然后重新打高尔夫球。

FI=1ȦḤW
OµNĠLÞṚịµL€+Ç,N
ÇÞṪ

单调链接,其中包含“手”列表并返回(其中一个)获胜者。

适用于全大写或全小写输入。
(...但不要混在一起,为此在最后一行之前加上ŒlŒu)。

在线尝试!或查看测试套件

怎么样?

FI=1ȦḤW - Link 1, straight offset: grouped and reverse sorted hand ordinals
        -                     e.g. [[-101],[-100],[-99],[-98],[-97]]
F       - flatten                  [-101,-100,-99,-98,-97]
 I      - increments               [1,1,1,1]
  =1    - equal 1? (vectorises)    [1,1,1,1]
    Ȧ   - any and all?             1
     Ḥ  - double                   2
      W - wrap in a list           [2]
        -   The purpose of this is so that when "a" from Link 2 represents a straight we
        -   get [2], whereas for any other hand we get [0]. Adding the [2] to [1,1,1,1,1]
        -   (the lengths of a straight's groups) yields [3,1,1,1,1], placing it between
        -   three of a kind, [3,1,1], and a full house, [3,2], as required.

OµNĠLÞṚịµL€+Ç,N - Link 2, hand rank key function: list of characters       e.g. "huhyg"
O               - cast to ordinals                                [104,117,104,121,103]
 µ              - monadic chain separation, call that o
  N             - negate (to give them a reverse-sort order) [-104,-117,-104,-121,-103]
   Ġ            - group indices by value                            [[4],[2],[1,3],[5]]
     Þ          - sort by key function:
    L           -   length                                          [[4],[2],[5],[1,3]]
      Ṛ         - reverse                                           [[1,3],[5],[2],[4]]
       ị        - index into o                       [[-104,-104],[-103],[-117],[-121]]
        µ       - monadic chain separation (call that a)
         L€     - length of €ach                                              [2,1,1,1]
            Ç   - call last link (2) as a monad -> [isStraight? * 2]                [0]
           +    - addition (vectorises)                                       [2,1,1,1]
              N - negate o                                [[104,104],[103],[117],[121]]
             ,  - pair                        [[2,1,1,1],[[104,104],[103],[117],[121]]]
                -   now sorting by this will first be comparing the hand class, and if
                -   and only if they match comparing the card values in the required order.

ÇÞḢ - Main link: list of lists of characters (list of hands)
 Þ  - sort by key function:
Ç   -   last link (2) as a monad
  Ṫ - tail (best or an equal-best hand)

与我在JS 0.o中所做的相比,简直太短了
斯蒂芬

3
@StephenS欢迎来到PPCG,在这里您用某种非高尔夫语言来做某事,然后有人用Jelly,05AB1E,Pyth,CJam等做一些比您的语言名称短的事:I:P
HyperNeutrino,

1
@StephenS-JS应该与JS竞争。不要让高尔夫语言阻止您以其他语言提交经过深思熟虑的解决方案!
乔纳森·艾伦

@JonathanAllan阻止了我投入过多的精力去思考和抽象一个可以用〜25个字符解决的问题,这是我正在研究的小提琴 -我编写了所有样板文件,但没有编写任何实际代码
Stephen

这很棒,但是我最近添加了一个无法计算的测试用例,特别是[“ hhhij”,“ hijkl”]。我认为这是因为您将直线排列为[3,1,1,1,1]的方式吗?
八达通

3

的JavaScript(250个 247 232字节)

S=d=>(x={},l=99,h=s=0,[...d].map(v=>x[v]=-~x[v]),Object.keys(x).map(v=>(c=91-v.charCodeAt(),t=x[v],s+=1e4**t,c<x[t]?0:x[t]=c,g=(h=c>h?c:h)-(l=c<l?c:l))),[5,4,3,2,1].map(v=>s+=0|x[v]**v),s+(s<5e7&&g<5?1e13:0)),C=a=>b=>(S(a)>S(b)?a:b)

JSFiddle中的取消代码和测试用例:https ://jsfiddle.net/CookieJon/8yq8ow1b/

感谢@RickHitchcock,节省了一些字节。@StephenS和@Arnauld


这是我试图制作的,但不知道如何制作。
斯蒂芬,

我也没有,直到我开始!:-)
Bumpy

s=0,h=0=> s=h=0我相信
斯蒂芬(Stephen)

1
经过大量拉毛后现已修复。在手牌相同且第一和第二大组中的最低字符相同的情况下确定决胜局是杀手(仅33字节左右!):-(
笨拙的

x[v]=x[v]?++x[v]:1可以变成x[v]=(x[v]|0)+1,节省3个字节。
里克·希区柯克

2

Python 2.7版,242个 223字节

from collections import*
s=sorted
f=lambda x,y:s(map(lambda h:(lambda (r,n):((3,1.5)if len(r)==5 and ord(r[0])+4==ord(r[4])else n,[-ord(d) for d in r],h))(zip(*s(Counter(h).items(),key=lambda z:(-z[1],z[0])))),(x,y)))[1][2]

基本概念与javascript示例相似(按手的力量排序,平直除外);然后按等级排序;但利用collections.Counter不幸的是,.most_common它并没有达到理想的行为;因此必须添加自定义排序键。

编辑:打高尔夫球以减少19个字节的更多代码。

非高尔夫代码

from collections import Counter

def convertHand(h):
    # first get item counts, appropriately ordered; e.g. cbabc -> (('b',2), ('c',2),('a',1))
    sortedPairs = sorted(Counter(h).items(),key=lambda x:(-x[1],x[0]))

    # 'unzip' the tuples to get (('b','c','a'), (2,2,1))
    ranks, numberFound = zip(*sortedPairs) 

    if len(ranks)==5:
        # no pairs; is it a straight? well, since they are in increasing order...
        if ord(ranks[0])+4 == ord(ranks[4]):
            # replace numberFound with something that will sort above 3 of a kind but below full house
            numberFound = (3,1.5)

    # invert the values of the ranks, so they are in decreasing, rather then increasing order
    ranks = [-ord(r) for r in ranks]

    # arrange tuples so we can sort by numberFound, and then ranks; and keep a reference to the hand
    return (numberFound, ranks, h)

# put it all together...
def f(x,y):
    hands = [convertHand(h) for h in (x,y)]
    rankedHands = sorted(hands)
    return rankedHands[1][2]

1

Mathematica,635个字节

H[x_]:=Block[{t},T=Sort@ToCharacterCode[x];L=Last/@Tally@T;t=0;S=Count;If[S[L,2]==1,t=1];If[S[L,2]==2,t=2];If[S[L,3]==1,t=3];If[S[Differences@T,1]==4,t=4];If[S[L,2]==1&&S[L,3]==1,t=5];If[S[L,4]==1,t=6];If[S[L,5]==1,t=7];t];F[K_,v_]:=First@Flatten@Cases[Tally@K,{_,v}];B=ToCharacterCode;(Z=Sort@B@#1;Y=Sort@B@#2;a=H[#1];b=H[#2];If[a>b,P@#1,If[a<b,P@#2]]If[a==b&&a==0,If[Z[[1]]<Y[[1]],P@#1,P@#2]];If[a==b&&(a==1||a==2),If[F[Z,2]<F[Y,2],P@#1,If[F[Z,2]==F[Y,2],If[F[Z,1]<F[Y,1],P@#1,P@#2],P@#2]]];If[a==b&&(a==3||a==5),If[F[Z,3]<F[Y,3],P@#1,P@#2]];If[a==b&&a==6,If[F[Z,4]<F[Y,4],P@#1,P@#2]];If[a==b&&(a==7||a==4),If[Tr@Z<Tr@Y,P@#1,P@#2]])&



输入形式

[“ abcde”,“ kkekk”]


有没有一种在线测试方法?
Octopus

2
sandbox.open.wolframcloud.com/app/objects 用ctrl + v粘贴在代码末尾添加输入,并使用shift + enter运行
J42161217 '17
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.