The input is array of string ["(1,2)","(10,2)"]
import ast
def TreeConstructor(strArr):
# code goes here
if len(strArr) == 0:
return "true"
nodes = {}
parent_exist = set()
for ele in strArr:
child, parent = ast.literal_eval(ele)
child_count = nodes.get(parent, 0)
child_count += 1
if(child_count > 2):
return "false"
nodes[parent] = child_count
parent_exist.add(child)
no_parent_count = 0
for node in nodes:
if node not in parent_exist:
no_parent_count += 1
return "true" if no_parent_count == 1 else "false"
I'm trying to understand the porpose of this part in code
child, parent = ast.literal_eval(ele)
child_count = nodes.get(parent, 0)
child_count += 1
if(child_count > 2):
return "false"
nodes[parent] = child_count
parent_exist.add(child)
and why when i use eval like this : child, parent = eval(ele) i had an error : indentationerror: expected an indented block