The input will be two five letter words. They don't actually have to be dictionary words, just any five letters each, all lowercase or all uppercase, your choice. Only A-Z will appear in the input words and they will always be 5 characters in length.

Your program is to score them both as though they were poker hands and output the higher hand. Of course suits won't apply here, only rankings so there's no flushes.

The typical poker ranking system goes: '1 pair', '2 pairs', '3 of a kind', 'straight', 'full house', '4 of a kind', '5 of a kind', and of course there is the possibility the hand (or word in this case) could be worth nothing.

In the case of ties, letters closer to A are considered higher, so a pair of As beats a pair of Bs. In some cases both hands might be identical, but in a different order (or not), in that case output either hand or a resorted version of it.

This external page contains information about how to identify the winner and especially addresses ties within the specific rankings, in case you aren't familiar with how to score poker hands.

In the case of straights: the letters must be adjacent in the alphabet and are not allowed to wrap around. So 'defgh' in any order is a straight, 'xyzab' is not.

Examples of how to score a single hand:

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

So the program will actually produce results like this:

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

The order of the letters in both the input and output are irrelevant, so the order in your output can be different than the input, but the same inventory of letters needs to be present.

The output must contain exactly five letters -- no more, no less.

The usual codegolf rules apply. Shortest code wins.


5 Answers 5


JavaScript (224 218 213 bytes)



  w[0]>x[0] || w[0]==x[0] && w[1]<x[1] ? s : t

After map() runs, n + z determines the ranking of a hand:

enter image description here

(You can understand why I initialized z to 3.5.)

In case of a tie, Object.keys(o).sort() is used to determine the higher ranking hand.




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


Jelly,  28 27 29  27 bytes

+2 & then -2 fix a bug, then re-golf.


A monadic link taking a list of "hands" and returning (one of) the winners.

Works for all-uppercase OR all-lowercase input.
(...but not mixed, for that prepend the final line with Œl or Œu).

Try it online! or see the test suite.


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)
  • \$\begingroup\$ So freaking short compared to what I'm making in JS 0.o \$\endgroup\$
    – Stephen
    May 25, 2017 at 22:45
  • 3
    \$\begingroup\$ @StephenS Welcome to PPCG, where you make something in some non-golfing language and then someone makes something in Jelly, 05AB1E, Pyth, CJam, etc that's shorter than your language name :I :P \$\endgroup\$
    – hyper-neutrino
    May 25, 2017 at 22:48
  • 1
    \$\begingroup\$ @StephenS - JS should compete with JS. Do not let golfing languages deter you from submitting well thought out solutions in other languages! \$\endgroup\$ May 26, 2017 at 3:08
  • \$\begingroup\$ @JonathanAllan it deters me from putting too much effort into overthinking and abstracting a problem that can be solved in ~25 characters, here's the fiddle I was working on - I wrote all the boilerplate and none of the actual code \$\endgroup\$
    – Stephen
    May 26, 2017 at 4:26
  • \$\begingroup\$ This is awesome, but I recently added a test case that this doesn't compute, specifically ["hhhij","hijkl"]. I think it's because of the way you rank a straight as [3,1,1,1,1]? \$\endgroup\$
    – Octopus
    May 26, 2017 at 20:11

JavaScript (250 247 232 bytes)


Ungolfed code & test cases in JSFiddle: https://jsfiddle.net/CookieJon/8yq8ow1b/

Saved some bytes with thanks to @RickHitchcock. @StephenS & @Arnauld

  • \$\begingroup\$ This is what I was trying to make but had no clue how to make. \$\endgroup\$
    – Stephen
    May 26, 2017 at 4:31
  • \$\begingroup\$ Nor did I until after I started! :-) \$\endgroup\$
    – Bumpy
    May 26, 2017 at 4:33
  • \$\begingroup\$ s=0,h=0 => s=h=0 I believe \$\endgroup\$
    – Stephen
    May 26, 2017 at 4:34
  • 1
    \$\begingroup\$ Fixed now after much hair-pulling. Determining the tie-breaker in cases where the hand is the same AND the lowest characters in the 1st & 2nd largest groups were the same was the killer (33 bytes or so JUST for that!?) :-( \$\endgroup\$
    – Bumpy
    May 26, 2017 at 13:29
  • \$\begingroup\$ x[v]=x[v]?++x[v]:1 can become x[v]=(x[v]|0)+1, saving 3 bytes. \$\endgroup\$ May 26, 2017 at 14:20

Python 2.7, 242 223 bytes

from collections import*
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]

Similar in basic concept to the javascript examples (sort by hand strength with an exception for straights; then by rank); but taking advantage of collections.Counter Unfortunately, .most_common doesn't have quite the desired behavior; so had to add a custom sort key.

Edit: a bit more code golfing to trim down 19 bytes.

Un-golfed code

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]

Mathematica, 635 bytes


Input form

["abcde", "kkekk"]

  • \$\begingroup\$ Is there a way to test this online? \$\endgroup\$
    – Octopus
    May 26, 2017 at 3:31
  • 2
    \$\begingroup\$ sandbox.open.wolframcloud.com/app/objects paste with ctrl+v add the input at the end of the code and run with shift+enter \$\endgroup\$
    – ZaMoC
    May 26, 2017 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.