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.
I've made a change to my Tree class so that I store parents as a list. But I'm still failing all the same case. I think I'm running into issues with the recurrence of the bendersPlay function, all the parent data is lost when this happens and the code goes into an infinite loop. Here's what I've got so far anyway.
classTree:def__init__(self,data,parents=[]):self.branches=[]self.data=dataself.parents=parentsdefsetChildren(self,paths):branches=[]forpathinpaths:ifself.data==path[0]andpath[1]notinself.parents:branch=Tree(path[1],self.parents+[self.data])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)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()total=sum(depths.values())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 →
I've made a change to my Tree class so that I store parents as a list. But I'm still failing all the same case. I think I'm running into issues with the recurrence of the bendersPlay function, all the parent data is lost when this happens and the code goes into an infinite loop. Here's what I've got so far anyway.