I am trying to use Python to print the nested lists in such a way that we can have multiple copies of them.
My question is: How to properly print this recursively in DFS with x[0] as the parent, and x[1:] as the children?
For example, I have this input
['1', ['2', ['3', ['4', ['5', ['6', ['7']], ['8', ['9']]]]]]]
And it can be expanded to visualize in this structure:
['1',
['2',
['3',
['4',
['5',
['6',
['7']
],
['8',
['9']
]
]
]
]
]
]
I am trying to have both printed recursively and expected to have a result that looks like this, with the entire path printed out.
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
1 -> 2 -> 3 -> 4 -> 5 -> 8 -> 9
But actually end up getting this
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
5 -> 8 -> 9
Here is my code snippet
def __print(chains: List):
"""
Assuming the input list where index 0 is the source, and index 1 is its
children paths.
"""
if (len(chains) == 0):
return
elif (len(chains) == 1):
# reaching the end
print("{0}".format(chains[0]))
return
else:
# everything after index 0 is considered children
children = chains[1:]
# has children
for child in children:
print("{0} -> ".format(chains[0]), end='')
__print(child)