Skip to content

Instantly share code, notes, and snippets.

@the6p4c
Created April 22, 2018 04:56
Show Gist options
  • Save the6p4c/3f268de442e2598ec2365b07d10e1608 to your computer and use it in GitHub Desktop.
Save the6p4c/3f268de442e2598ec2365b07d10e1608 to your computer and use it in GitHub Desktop.
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