python - List of all possible ways from root to leaf (without binary tree) -


i'm trying list possible moves root of leafs in tree. have setup can list ['a', ['b', ['c'], ['k']], ['d', ['e', ['f'], ['g']], ['h', ['i'], ['j']]]]

here image of list represents:

list representation

i'm wondering how can transform above list into: "abk", "abc", "adef", "adeg", "adhi", "adhj"

i've tried recursion through list can't quite figure out. btw reason i'm trying use list because thats real way think of , doesn't seem of strech list different pathways?

here's suggestion!

def walktree(lst):     # leaf?  here's our base case.     if len(lst) == 1:         return lst      # if not, it's node; make sure list formatted correctly.     assert(len(lst) == 3)      first = lst[0]      # here's recursion happens.      left = walktree(lst[1])     right = walktree(lst[2])      # finally, combination step.      return [first + x x in left + right] 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -