Generating all 5 card poker hands

前端 未结 11 643
再見小時候
再見小時候 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条回答
  •  梦毁少年i
    2020-12-23 10:12

    Your problem sounded interesting, so i simple tried to implements it by just looping over all possible hands in a sorted way. I've not looked at your code in details, but it seems my implementation is quite different from yours. Guess what count of hands my script found: 160537

    • My hands are always sorted, e.g. 2 3 4 4 D
    • If there are 2 equal cards, the color is also sorted (colors are just called 0,1,2,3)
    • the first card has always color 0, the second color 0 or 1
    • A card can only have the color of an previous card or the next bigger number, e.g. if card 1+2 have color 0, card three can only have the colors 0 or 1

    Are you sure, the number on wikipedia is correct?

    count = 0
    for a1 in range(13):
        c1 = 0
        for a2 in range(a1, 13):
            for c2 in range(2):
                if a1==a2 and c1==c2:
                    continue
                nc3 = 2 if c1==c2 else 3
                for a3 in range(a2, 13):
                    for c3 in range(nc3):
                        if (a1==a3 and c1>=c3) or (a2==a3 and c2>=c3):
                            continue
                        nc4 = nc3+1 if c3==nc3-1 else nc3
                        for a4 in range(a3, 13):
                            for c4 in range(nc4):
                                if (a1==a4 and c1>=c4) or (a2==a4 and c2>=c4) or (a3==a4 and c3>=c4):
                                    continue
                                nc5 = nc4+1 if (c4==nc4-1 and nc4!=4) else nc4
                                for a5 in range(a4, 13):
                                    for c5 in range(nc5):
                                        if (a1==a5 and c1>=c5) or (a2>=a5 and c2>=c5) or (a3==a5 and c3>=c5) or (a4==a5 and c4>=c5):
                                            continue
                                        #print([(a1,c1),(a2,c2),(a3,c3),(a4,c4),(a5,c5)])
                                        count += 1
    print("result: ",count)
    

提交回复
热议问题