Skip to content

Instantly share code, notes, and snippets.

@martinstoyanov
Created October 24, 2020 00:16
Show Gist options
  • Save martinstoyanov/11ade23280ad7bec12fc04191539bd59 to your computer and use it in GitHub Desktop.
Save martinstoyanov/11ade23280ad7bec12fc04191539bd59 to your computer and use it in GitHub Desktop.
from random import randint
# generates combinations in k-tuples/triples etc. if k=2 (1,2).
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
def combinations(iterable, r):
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = list(range(r))
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in list(range(i+1, r)):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
def sim(deck, n, k):
table = []
for round in range(n):
newCard = deck.pop(randint(0,len(deck)-1)) if len(deck) > 1 else deck.pop(0)
if round < k:
table.append(newCard)
else:
discardInd = randint(0, k)
if discardInd < k:
table.pop(discardInd)
table.append(newCard)
return table
def main():
deck = [1,2,3,4,5]
n = 5
k = 3
allCardCombinations = list(set(list(combinations(deck, k))))
res = {}
for comb in allCardCombinations:
res[str(comb)] = 0
for i in range(100000):
table = sim(deck.copy(), n, k)
table = sorted(table)
key = str(tuple(table))
res[key] += 1
print(res)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment