Guest User

Untitled

a guest
Mar 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. class Production:
  2. def __init__(self, prod):
  3. self.prod = prod
  4. self.sumSet = set()
  5.  
  6. def addSum(self, summation):
  7. self.sumSet.add(summation)
  8.  
  9. def removeSum(self, summation):
  10. self.sumSet.remove(summation)
  11.  
  12. def __str__(self):
  13. return "Prod object: {0} {1}".format( self.prod, self.sumSet)
  14.  
  15. class Summation:
  16. def __init__(self, prod):
  17. self.sum = prod
  18. self.prodSet = set()
  19.  
  20. def addProd(self, summation):
  21. self.prodSet.add(summation)
  22.  
  23. def removeSum(self, summation):
  24. self.prodSet.remove(summation)
  25.  
  26. def __str__(self):
  27. return "Sum object: {0} {1}".format( self.sum, self.prodSet)
  28.  
  29. class Graph:
  30. def __init__(self, minNum, maxNum):
  31. self.sumDict = dict()
  32. self.prodDict = dict()
  33. for a in range(minNum, maxNum+1):
  34. for b in range(a, maxNum+1):
  35. self.sumDict.setdefault(a+b, Summation(a+b))
  36. self.prodDict.setdefault(a*b, Production(a*b))
  37. self.sumDict.get(a+b).addProd(a*b)
  38. self.prodDict.get(a*b).addSum(a+b)
  39.  
  40. def allProdsWithMultipleSums(self, summ):
  41. prodSet = self.sumDict[summ].prodSet
  42. for prod in prodSet:
  43. if len(self.prodDict[prod].sumSet) <= 1:
  44. return False
  45. return True
  46.  
  47. def removeSum(self, summ):
  48. prodSet = self.sumDict[summ].prodSet
  49. self.sumDict.pop(summ, None)
  50. for prod in prodSet:
  51. self.prodDict[prod].sumSet.remove(summ)
  52. if len(self.prodDict[prod].sumSet) == 0:
  53. self.prodDict.pop(prod, None)
  54.  
  55. def removeProd(self, prod):
  56. sumSet = self.prodDict[prod].sumSet
  57. self.prodDict.pop(prod, None)
  58. for summ in sumSet:
  59. self.sumDict[summ].prodSet.remove(prod)
  60. if len(self.sumDict[summ].prodSet) == 0:
  61. self.sumDict.pop(summ, None)
  62.  
  63. graph = Graph(minNum=2, maxNum=99)
  64.  
  65. sumsToRemove = {summ for summ in graph.sumDict.keys() if not graph.allProdsWithMultipleSums(summ) }
  66. for summ in sumsToRemove:
  67. graph.removeSum(summ)
  68.  
  69. prodsToRemove = {prod for prod in graph.prodDict.keys() if len(graph.prodDict.get(prod).sumSet) != 1}
  70. for prod in prodsToRemove:
  71. graph.removeProd(prod)
  72.  
  73. for s in graph.sumDict.values():
  74. if len(s.prodSet) == 1:
  75. print("sum: {0}".format(s.sum))
  76. print("prod: {0}".format(s.prodSet.pop()))
Add Comment
Please, Sign In to add comment