Skip to content

Instantly share code, notes, and snippets.

@hielscher hielscher/cards.py
Created Jun 1, 2019

Embed
What would you like to do?
#!/usr/bin/python
from itertools import combinations
class NumberGame:
def Run(self, small_cards):
self.small_cards = small_cards
self.large_cards = [25, 50, 75, 100]
self.sols = {}
self.counts = {}
self.sol_counts = {}
self.sol_counts_per = {}
for card in self.small_cards:
self.sols[(card,)] = {card}
for card in self.large_cards:
self.sols[(card,)] = {card}
total_combs = 0
for num_large in xrange(5):
seen = set()
num_combs = 0
self.counts[num_large] = 0
self.sol_counts_per[num_large] = {}
l = combinations(self.large_cards, num_large)
s = combinations(self.small_cards, 6-num_large)
print "Num large", sum([1 for x in l])
print "Num small", sum([1 for x in s])
small_combs = combinations(self.small_cards, 6-num_large)
for smalls in small_combs:
large_combs = combinations(self.large_cards, num_large)
for larges in large_combs:
cardsl = list(larges)
cardsl.extend(list(smalls))
cards = tuple(sorted(cardsl))
if cards not in self.sols:
self.ComputeCombs(cards)
self.sols[cards] = {x for x in self.sols[cards] if x >= 100 and x <= 999}
if cards not in seen:
num_combs += 1
total_combs += 1
seen.add(cards)
self.counts[num_large] += len(self.sols[cards])
for sol in self.sols[cards]:
if sol not in self.sol_counts:
self.sol_counts[sol] = 1
else:
self.sol_counts[sol] += 1
if sol not in self.sol_counts_per[num_large]:
self.sol_counts_per[num_large][sol] = 1
else:
self.sol_counts_per[num_large][sol] += 1
print "Num combs for num_large", num_large, num_combs
self.counts[num_large] /= (num_combs * 900.0)
self.sol_counts_per[num_large] = {
k:(v/(1.0*num_combs)) for k,v in self.sol_counts_per[num_large].iteritems()}
self.sol_counts = {k:(v/(1.0*total_combs)) for k,v in self.sol_counts.iteritems()}
self.sols = {k:v for k,v in self.sols.iteritems() if len(k) == 6}
def ComputeCombs(self, cards):
if cards in self.sols:
return
self.sols[cards] = set()
if len(cards) == 1:
self.sols[cards].add(cards[0])
elif len(cards) == 2:
self.sols[cards] = self.combs2(cards[0], cards[1])
else:
for idx1 in xrange(len(cards)):
for idx2 in xrange(idx1+1, len(cards)):
cards_slice = cards[:idx1]+cards[idx1+1:idx2]+cards[idx2+1:]
paircombs = self.combs2(cards[idx1], cards[idx2])
for p in paircombs:
post_comb = tuple(sorted((p,)+cards_slice))
if post_comb not in self.sols:
self.ComputeCombs(post_comb)
self.sols[cards].update(self.sols[post_comb])
def combs2(self, c1, c2):
t = {c1,c2}
t.add(c1+c2)
if c1-c2 >= 1:
t.add(c1-c2)
elif c2-c1 >= 1:
t.add(c2-c1)
t.add(c1*c2)
if c1 % c2 == 0 and c1/c2 >= 1:
t.add(c1/c2)
elif c2 % c1 == 0 and c2/c1 >= 1:
t.add(c2/c1)
return t
def get_sols(self):
return self.sols
def get_counts(self):
return self.counts
def get_sol_counts(self):
return self.sol_counts
def get_sol_counts_per(self):
return self.sol_counts_per
def PrintMaxMinNumber(self):
cs = self.sol_counts
maxs = [(k,v) for k,v in cs.iteritems() if v == max(cs.values())][0]
mins = [(k,v) for k,v in cs.iteritems() if v == min(cs.values())][0]
print "max,min num:", maxs, mins
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.