I have a task to make an list of list following specific rules. List must represent a tree with a root and branches from this root with specific color. Each branch should be represented as a list of its child elements (one black branch generates 3 white; 1 white branch generates 2 black). For example: root=['black']
, first branches [['white','white','white']]
, next iteration should be [[[black,black],[black,black],[black,black]]]
and so on.
This infinite list should be stored in global variable. Is it possible? My code, which generates such list do it only for for a predetermined number of steps.
root = ['b']
def change(root):
for index, item in enumerate(root):
if isinstance(item, list):
change(item)
elif item == 'b':
root[index] = ['w','w','w']
elif item == 'w':
root[index] = ['b','b']
return root
for i in range(3):
tree=change(root)
print(tree)
How can I generate infinite list if it is possible?