Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def generate_stacks(n):
if n == 1:
yield (True,)
yield (False,)
else:
for stack in generate_stacks(n - 1):
yield stack + (True,)
yield stack + (False,)
def stack_flip(stack, n):
end = [not x for x in stack[0:n]]
end.reverse()
return stack[n:len(stack)] + tuple(end)
def stack_to_string(stack):
def bool_to_str(x):
return 'T' if x else 'H'
return ''.join([bool_to_str(x) for x in stack])
def main():
stack_size = 3
results = {}
for stack in generate_stacks(stack_size):
for n in range(1, stack_size):
inner_results = results.get(stack, [])
inner_results.append((n, stack_flip(stack, n)))
results[stack] = inner_results
dot_file = ''
for stack, inner_results in results.items():
for n, inner_stack in inner_results:
dot_file += '{} -> {} [label=\"{}\"];\n'.format(
stack_to_string(stack), stack_to_string(inner_stack),
n
)
with open('graph.dot', 'w') as f:
f.write('digraph G {{\n{}}}'.format(dot_file))
if __name__ == '__main__':
main()
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.