Skip to content

Instantly share code, notes, and snippets.

@kkwteh
Last active August 9, 2019 21:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkwteh/b81d8e599ec46a2d64b096f953a11e63 to your computer and use it in GitHub Desktop.
Save kkwteh/b81d8e599ec46a2d64b096f953a11e63 to your computer and use it in GitHub Desktop.
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