Guest User

Untitled

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