Created
April 2, 2015 02:38
-
-
Save igul222/98a487eb6a93ad4ba271 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class Node(object): | |
def __init__(self, _children): | |
self.memo = None | |
self.children = _children | |
dest = Node([]) | |
source = Node([ | |
dest, # source -> dest | |
Node([dest]), # source -> intermediate -> dest | |
Node([Node([dest])]), # source -> a -> b -> dest | |
Node([]) # source -> intermediate -> nothing | |
]) | |
def paths_to_dest(node): | |
if node == dest: | |
return 1 | |
else: | |
if node.memo is None: | |
node.memo = sum([paths_to_dest(child) for child in node.children]) | |
return node.memo | |
print paths_to_dest(source) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment