164

Python でポーカーのハンドを高速に評価することを検討しています。プロセスをスピードアップする 1 つの方法は、すべてのカードの面とスーツを素数として表し、それらを掛け合わせてハンドを表すことだと思いました。具体的には:

class PokerCard:
    faces = '23456789TJQKA'
    suits = 'cdhs'
    facePrimes = [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 53, 59, 61]
    suitPrimes = [2, 3, 5, 7]

    def HashVal(self):
      return PokerCard.facePrimes[self.cardFace] * PokerCard.suitPrimes[self.cardSuit]

これにより、各ハンドに数値が与えられ、モジュロを介してハンドにキングがいくつあるか、またはハートがいくつあるかがわかります。たとえば、5 つ以上のクラブを含むハンドは、2^5 で均等に分割されます。キングが 4 枚あるハンドは、59^4 で等分されます。

問題は、AcAdAhAsKdKhKs のような 7 枚の手札のハッシュ値が約 62.7 千兆であり、内部的に表現するには 32 ビットよりもかなり多くかかることです。算術演算を実行できるような大きな数値を Python に格納する方法はありますか?

4

6 に答える 6

99

python は自然に任意の大きな整数をサポートします:

例:

>>> 10**1000
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

たとえば、巨大な整数値 fib(4000000) を取得することもできます。

しかし、それでも(今のところ)任意の大きなフロートをサポートしていませ!!

大きな大きな浮動小数点数が 1 つ必要な場合は、10 進モジュールを確認してください。これらのフォーラムでの使用例があります: OverflowError: (34, 'Result too large')

別の参照: http://docs.python.org/2/library/decimal.html

スピードアップが必要な場合は、 gmpy モジュールを使用することもできます (これはおそらくあなたの関心事です) :

別の参照: https://code.google.com/p/gmpy/

于 2014-01-07T19:50:08.040 に答える
38

You could do this for the fun of it, but other than that it's not a good idea. It would not speed up anything I can think of.

  • Getting the cards in a hand will be an integer factoring operation which is much more expensive than just accessing an array.

  • Adding cards would be multiplication, and removing cards division, both of large multi-word numbers, which are more expensive operations than adding or removing elements from lists.

  • The actual numeric value of a hand will tell you nothing. You will need to factor the primes and follow the Poker rules to compare two hands. h1 < h2 for such hands means nothing.

于 2009-02-11T21:07:15.947 に答える