Skip to content

Instantly share code, notes, and snippets.

@the6p4c
Created April 22, 2018 04:56
Show Gist options
  • Select an option

  • Save the6p4c/3f268de442e2598ec2365b07d10e1608 to your computer and use it in GitHub Desktop.

Select an option

Save the6p4c/3f268de442e2598ec2365b07d10e1608 to your computer and use it in GitHub Desktop.

Revisions

  1. the6p4c created this gist Apr 22, 2018.
    44 changes: 44 additions & 0 deletions coin_graph.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    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()