Advertisement
Guest User

Untitled

a guest
Nov 8th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. #A Zero-Sum Game Of Threes Challenge
  2. #Graeme Cliffe
  3.  
  4. children=[-2,-1,1,2]
  5.  
  6. def dfsChildren(parentRunTot,parentSum):#spin children off of parent node
  7. #print "Checking children"
  8. for childVal in children:#Attempt all of the children branches
  9. childSum=parentSum+childVal
  10. childRunTot=parentRunTot+childVal
  11. print "Childval is",childVal
  12. print "ChildTot is",childRunTot
  13. print "ChildSum is",childSum
  14.  
  15. additionList=dfs(childRunTot,childSum)
  16. if additionList:#If this child returns True, then append and return up
  17. print "Returning",additionList
  18. additionList.append(childVal)
  19. return additionList
  20. print "No children of this branch work"
  21. return None#No case matched
  22.  
  23. #Use a DFS. If we reach more than 3*initial number a solution is impossible
  24. def dfs(parentRunTot,parentSum):
  25. print "=====Entering dfs====="
  26. print "Current total is",parentRunTot
  27. print "Current sum is",parentSum
  28. if parentRunTot==1:
  29. if parentSum==0:
  30. #If we have hit the base case
  31. print "Base case"
  32. baseList=list()
  33. baseList.append(0)
  34. return baseList
  35. print "End of Branch"
  36. return None#End of branch
  37. if parentRunTot>3*initNum:
  38. #If we reach too high, end this child
  39. print "Too high"
  40. return None
  41. if parentRunTot<1:
  42. #If we move below 1, end this child
  43. print "Below 1"
  44. return None
  45. if parentRunTot%3==0:
  46. #If we can divide by 3, we do
  47. print "Dividing by 3"
  48. parentRunTot/=3
  49. return dfs(parentRunTot,parentSum)
  50. #If we aren't at the base case, but no problems, keep checking children
  51. else:
  52. print "Normal children"
  53. return dfsChildren(parentRunTot,parentSum)
  54.  
  55. initNum=int(raw_input())
  56. print dfs(initNum,0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement