コード例 #1
0
def prob_two_pairs(N=100000):
    M = 0  # no. of successes
    for exp in xrange(N):
        deck = Deck()
        hand = deck.hand(n=5)
        M += (same_rank(hand, 2) == 2)
    return float(M) / N
コード例 #2
0
def prob_fourofakind(N=100000):
    M = 0  # no. of successes
    for exp in xrange(N):
        deck = Deck()
        hand = deck.hand(n=5)
        M += (same_rank(hand, 4) == 1)
    return float(M) / N
コード例 #3
0
ファイル: card_hands.py プロジェクト: doktor01/python_primer
def prob_two_pairs(N=100000):
    M = 0  # no. of successes
    for exp in xrange(N):
        deck = Deck()
        hand = deck.hand(n=5)
        M += (same_rank(hand, 2) == 2)
    return float(M) / N
コード例 #4
0
ファイル: card_hands.py プロジェクト: doktor01/python_primer
def prob_fourofakind(N=100000):
    M = 0  # no. of successes
    for exp in xrange(N):
        deck = Deck()
        hand = deck.hand(n=5)
        M += (same_rank(hand, 4) == 1)
    return float(M) / N
コード例 #5
0
        return hand

    def deal(self, cards_per_hand, no_of_players):
        """Deal no_of_players hands. Return list of lists."""
        return [self.hand(cards_per_hand) \
                for i in range(no_of_players)]

    def putback(self, card):
        """Put back a card under the rest."""
        self.deck.append(card)

    def __str__(self):
        return str(self.deck)


if __name__ == '__main__':
    deck = Deck()
    print deck
    players = deck.deal(5, 4)
    import pprint
    pprint.pprint(players)
    from cards import same_rank, same_suit
    for hand in players:
        print """\
The hand %s
    has %d pairs, %s 3-of-a-kind and
    %s cards of the same suit.""" % \
        (', '.join(hand), same_rank(hand, 2),
         same_rank(hand, 3),
         '+'.join([str(s) for s in same_suit(hand).values()]))
コード例 #6
0
ファイル: card_hands.py プロジェクト: OldJohn86/Langtangen
from Deck import Deck
from cards import same_rank, same_suit
import random

d = Deck()
N = 10**4
# Probability of two pairs among 5 cards

successes = 0
total = 0
for i in range(N):
    h = d.hand(5)
    if same_rank(h, 2) == 2:
        successes += 1
    total += 1
    for j in range(len(h)):
        d.putback(h[j])
    random.shuffle(d.deck)
print 'Probability of two pairs from among 5 cards',
print '%f' % (float(successes) / total)

# Four or five cards of the same suit among 5 cards
successes = 0
total = 0
for i in range(N):
    h = d.hand(5)
    s = same_suit(h)
    for j in s:
        if s[j] == 4 or s[j] == 5:
            successes += 1
    total += 1
コード例 #7
0
ファイル: Deck.py プロジェクト: nbut23/aprimeronpython
        del self.deck[:n]                        # remove cards
        return hand

    def deal(self, cards_per_hand, no_of_players):
        """Deal no_of_players hands. Return list of lists."""
        return [self.hand(cards_per_hand) \
                for i in range(no_of_players)]

    def putback(self, card):
        """Put back a card under the rest."""
        self.deck.append(card)

    def __str__(self):
        return str(self.deck)

if __name__ == '__main__':
    deck = Deck()
    print deck
    players = deck.deal(5, 4)
    import pprint; pprint.pprint(players)
    from cards import same_rank, same_suit
    for hand in players:
        print """\
The hand %s
    has %d pairs, %s 3-of-a-kind and
    %s cards of the same suit.""" % \
        (', '.join(hand), same_rank(hand, 2),
         same_rank(hand, 3),
         '+'.join([str(s) for s in same_suit(hand).values()]))

コード例 #8
0
        """Deal no_of_players hands. Return list of lists."""
        return [self.hand(cards_per_hand) for i in range(no_of_players)]

    def putback(self, card):
        """Put back a card under the rest."""
        self.deck.append(card)

    def __str__(self):
        return str(self.deck)


if __name__ == "__main__":
    deck = Deck()
    print deck
    players = deck.deal(5, 4)
    import pprint

    pprint.pprint(players)
    from cards import same_rank, same_suit

    for hand in players:
        print """\
The hand %s
    has %d pairs, %s 3-of-a-kind and
    %s cards of the same suit.""" % (
            ", ".join(hand),
            same_rank(hand, 2),
            same_rank(hand, 3),
            "+".join([str(s) for s in same_suit(hand).values()]),
        )