Skip to content

Instantly share code, notes, and snippets.

@igul222
Created April 2, 2015 02:38
Show Gist options
  • Save igul222/98a487eb6a93ad4ba271 to your computer and use it in GitHub Desktop.
Save igul222/98a487eb6a93ad4ba271 to your computer and use it in GitHub Desktop.
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