I need help on parsing a input(a tree nested list) such as: [[3,12,8],[2,4,6],[1
ID: 3604008 • Letter: I
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 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 :(
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-parentExplanation / Answer
Add parse_input_data_as_list() function into you main file just below parse_data_as_list() and make changes to you main() function with respect to the one given below.
Note: Keep indentation in mind
def parse_input_data_as_list(data_as_string):
data_list = literal_eval(data_as_string)
return data_list
def main():
data_as_string = sys.argv[1] #eg:while running code: python fileName.py [[3,12,8],[2,4,6],[14,5,2]]
data_list = parse_input_data_as_list(data_as_string)
data_tree = GameTree()
data_tree.build_tree(data_list)
minimax = MiniMax(data_tree)
best_move = minimax.minimax(minimax.root)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.