Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import random
from collections import defaultdict, namedtuple
def penultimate_five_gram(hist):
return ''.join(hist[-6:-1])
def last_five_gram(hist):
return ''.join(hist[-5:])
def chunks(l, n):
"""Yield successive n-sized chunks from l."""
for i in range(0, len(l), n):
yield l[i:i + n]
ctrs = defaultdict(lambda: [0,0])
hist = []
for i in range(100):
if len(hist) < 6 or ctrs[last_five_gram(hist)][0] == ctrs[last_five_gram(hist)][1]:
next_letter = random.choice(['L','R'])
else:
if ctrs[last_five_gram(hist)][0] < ctrs[last_five_gram(hist)][1]:
next_letter = 'L'
else:
next_letter = 'R'
hist.append(next_letter)
if len(hist) >= 6:
if hist[-1] == 'L':
ctrs[''.join(penultimate_five_gram(hist))][0] += 1
else:
ctrs[''.join(penultimate_five_gram(hist))][1] += 1
print(list(chunks(''.join(hist), 5)))
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.