Question: I need help on parsing a input(a tree nested list) such as: [[3,12,8],[2,4,6],[14,5,2]] in python. Basically right now the code reads in from a .txt

I need help on parsing a input(a tree nested list) such as: [[3,12,8],[2,4,6],[14,5,2]] in python.

Basically right now the code reads in from a .txt file, but I want a user input instead, how can I achieve that from the following code.

Here what I have as parser code as of now:

Posted Image code only, since indention problems :(

I need help on parsing a input(a tree nested list) such as:

[[3,12,8],[2,4,6],[14,5,2]] in python. Basically right now the code reads in from a

As you can see from the "parse_data_as_list" function I only managed to read from file and before it was a string input, now its just tree type nested list, but since I am quite new to python, I am having trouble testing my input through user input.

def parse data as list (fname) with open (fname, "r") as f: data as string- f.read () print data as string data_list - literal_eval (data_as_string) return data list class GameNode: def init 3 elf . Name = name 3e1f.value = value self.parent-parent # a node reference self. children- [] # a list of nodes (self, name, value=0, parent=None): # a char # an int 9 2 def addChild (self, childNode): self.children.append (childNode) 6 class GameTree: def init (self): self. root = None def build tree (self, data list) self. root GameNode (data list. pop (0) ) for elem in data list: )3 self.parse_subtree (elem, self.root) def parse subtree (self, data list, parent): # base case if type (data_list) is tuple: # make connect!on3 leafnode GameNode (datalist [0] ) leaf_node.parent-parent parent . addChild (leafnode) # if we're at a leaf, set the value if len(data list)- 9 - - - 2 2: leaf node.value data list [1] return # recursive cas tree node GameNode (data list. pop(0)) # make connections tree_node parent-parent

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!