incoming = {'E': {'D': {'A': {},
'O': {'Co': {},
'Cu': {}
}
}
}
}
expected = [
('E', 'D', 'A'),
('E', 'D', 'O', 'Co'),
('E', 'D', 'O', 'Cu'),
]
def paths(tree, cur=()):
if not tree: # previous was a leaf, path to us is a solution
yield cur
else: # we're a node
for node, subtree in tree.iteritems():
for path in paths(subtree, cur+(node,)):
yield path
assert list(paths(incoming)) == expected
here I will gather some notes on my python use. I will give my impressions on different libs when I can give them a try to solve the problem at hand. Also I will announce some releases when I think some of my packages might be useful to others.
Search This Blog
Showing posts with label graph. Show all posts
Showing posts with label graph. Show all posts
01 November 2014
recursivity elegance
while searching for an elegant solution, I stumbled upon this by Andrew Clark
Subscribe to:
Posts (Atom)