Generating all 5 card poker hands

前端 未结 11 687
再見小時候
再見小時候 2020-12-23 09:53

This problem sounds simple at first glance, but turns out to be a lot more complicated than it seems. It\'s got me stumped for the moment.

There are 52c5 = 2,598,960

11条回答
  •  被撕碎了的回忆
    2020-12-23 10:18

    Chances are you really want to generate the number of distinct hands, in the sense of non-equivalent. In that case, according to the wikipedia article there are 7462 possible hands. Here is a python snippet that will enumerate them all.

    The logic is simple: there is one hand for each 5-set of ranks; in addition, if all the ranks are distinct, another, different kind of hand can be formed by making all the suits match.

    count = 0 
    
    for i in range(0,13):
        for j in range (i,13):
            for k in range(j,13):
                for l in range(k,13):
                    for m in range(l,13):
                        d = len(set([i,j,k,l,m])) # number of distinct ranks
                        if d == 1: continue    # reject nonsensical 5-of-a-kind
                        count += 1
                        # if all the ranks are distinct then 
                        # count another hand with all suits equal
                        if d == 5: count += 1
    
    print count   # 7462
    

提交回复
热议问题