We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
So this is my function and a class for a tree data structure (python3). It works for the sample case but none of the other cases. I can't see why this wouldn't work.
classTree:def__init__(self,data):self.branches=[]self.data=datadefsetChildren(self,paths):branches=[]forpathinpaths:ifself.data==path[0]:branch=Tree(path[1])branch.setChildren(paths)self.branches.append(branch)defhasBranches(self):if(self.branches):return(True)else:return(False)defgetDepth(self,depth):depths=[]if(self.hasBranches()):depth=depth+1forbranchinself.branches:depths.append(branch.getDepth(depth))if(depths):return(max(depths))else:return(depth)defgetBestMove(self):depths={}max_key=0if(self.branches):forbranchinself.branches:depths[branch.data]=branch.getDepth(0)max_key=max(depths,key=depths.get)# fptr.write('Branch Depths[' + str(self.data) + ']: ' + str(depths) + '\n')return(max_key)## Complete the bendersPlay function below.#defbendersPlay(n,paths,query,player='Bumi'):## Write your code here.#if(player=='Bumi'):player='Iroh'else:player='Bumi'depths={}bestMoves={}foriinquery:tree=Tree(i)tree.setChildren(paths)tree.getDepth(0)depths[i]=tree.getDepth(0)bestMoves[i]=tree.getBestMove()max_key=max(depths,key=depths.get)max_value=max(depths.values())ifmax_value:query[query.index(max_key)]=bestMoves[max_key]player=bendersPlay(n,paths,query,player)return(player)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Play on benders
You are viewing a single comment's thread. Return to all comments →
So this is my function and a class for a tree data structure (python3). It works for the sample case but none of the other cases. I can't see why this wouldn't work.