#!/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)