Skip to content

Instantly share code, notes, and snippets.

@hryk
Created September 17, 2012 14:37
Show Gist options
  • Select an option

  • Save hryk/3737766 to your computer and use it in GitHub Desktop.

Select an option

Save hryk/3737766 to your computer and use it in GitHub Desktop.
tea break
# coding: utf-8
from copy import copy
class SimpleBars(object):
def __init__(self,arg):
""" Initializing a sequence as list """
self.current = [c for c in arg]
def apply_rules(self):
""" Applying rules to current sequence """
# Rule1:
# 'T' -> 'j'
next_seq = copy(self.current)
for i, v in enumerate(self.current):
if v == 'T': next_seq[i] = 'j'
# Rule2:
# 'i' -> 'iTi', (i+i|i+j|j+j|j+i) -> ' '
next_seq2 = copy(next_seq)
for i, v in enumerate(next_seq):
if v != 'i': continue
next_seq2[i] = 'T'
targets = [i-1,
(0 if len(next_seq2) <= i+1 else i+1)]
for t in targets:
if next_seq2[t] == 'i' or next_seq2[t] == 'j':
next_seq2[t] = ' '
else:
next_seq2[t] = 'i'
# Replace 'j' to 'i'
self.current = [c for c in ''.join(next_seq2).replace('j', 'i')]
def next(self):
""" Generate next sequences from current sequences """
self.apply_rules()
return ''.join(self.current)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment