Last active
July 23, 2020 22:22
-
-
Save mapio/70153b1750b3019d6764d5573135e7a0 to your computer and use it in GitHub Desktop.
Playing with my son Federico
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import deque | |
from subprocess import Popen, PIPE | |
from sys import argv | |
first, last = map(int, argv[1:3]) | |
name = argv[3] | |
ops = [ | |
lambda x: x + 1, | |
lambda x: x * 2 | |
] | |
frontier = deque([first]) | |
seen = set() | |
graph = 'graph G {\nnode [shape=point]' | |
while frontier: | |
v = frontier.popleft() | |
if v in seen: continue | |
for op in ops: | |
u = op(v) | |
if u > last: continue | |
graph += '\t{} -- {}'.format(v, u) | |
frontier.append(u) | |
seen.add(v) | |
graph += '}' | |
sfdp = Popen(['sfdp', '-Tpng'], stdin = PIPE, stdout = PIPE) | |
with open('{}.png'.format(name), 'wb') as of: | |
of.write(sfdp.communicate(input = graph)[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I guess you mean 179x173 pixels, the image size depends on the
sfdp
tool, part of Graphviz. You can check the manual to see if you can specify a larger size, this Stack Overflow answer could be a good starting point.