usr bin python coding utf-8 Some fundamental constants 52 total cards

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Some fundamental constants
N = 52 #total cards
C = 13 #cards in color
M = 4 #colors
K_max = 13 #desired number of cards in a hand
class Hand (list):
def __repr__(self):
res = [('%1d' % r) for r in self]
return '-'.join(res)
def __hash__ (self):
h = 0
for k in self:
h = h * C + k
return h
from copy import copy
old_hands = {Hand([1]+[0]*(M-1)):1.0} #K = 1 => (1,0,0,0) & P=1
for K in xrange(2, K_max+1): #cards in set
new_hands = {}
for h0, p0 in old_hands.iteritems():
for c in range(0,M): #adding colors
h = copy(h0); h[c] += 1;
h.sort (reverse=True)
dp = p0 * (C - h0[c])/(N - K + 1.0)
if not (h in new_hands):
new_hands[h] = 0.0
new_hands[h] += dp
old_hands = new_hands
res = [{'hand':h, 'p': p} for h,p in new_hands.iteritems ()]
def by_probability (v1,v2):
if v1['p'] > v2['p']:
return 1
if v1['p'] < v2['p']:
return -1
return 0
res.sort (cmp=by_probability, reverse=True)
p_total = 0.0
for obj in res:
p_total += obj['p']
print 'P = %e' % obj['p'], 'H =', obj['hand']
print 'Total:', p_total
groups = (
('Равномер',(Hand((4,3,3,3)), Hand((4,4,3,2)), Hand((5,3,3,2)))),
('Боевая 5ка',(Hand((5,4,2,2)), Hand((5,4,3,1)))),
('Трёхцвет',(Hand((4,4,4,1)), Hand((5,4,4,0)))),
('Стандартный двуцвет',(Hand((5,5,2,1)), Hand((5,5,3,0)))),
('Двуцвет Конюховского',(Hand((6,4,2,1)), Hand((6,4,3,0)))),
('Плоские шестёрки',(Hand((6,3,2,2)), Hand((6,3,3,1)))),
('Резкий двуцвет',(Hand((6,5,1,1)), Hand((6,5,2,0)))),
('Блок',(Hand((7,2,2,2)), Hand((7,3,2,1)), Hand((7,3,3,0)))),
('Юло-фиты',(Hand((7,4,1,1)), Hand((7,4,2,0)))),
)
p_standard = 0.0
for g, l in groups:
p = 0.0
for i in l:
p += new_hands[i]
p_standard += p
print '%s %s: %e' % (g, l, p)
print 'Все остальные нестандартные расклады: %e' % (1.0-p_standard)