Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Production:
- def __init__(self, prod):
- self.prod = prod
- self.sumSet = set()
- def addSum(self, summation):
- self.sumSet.add(summation)
- def removeSum(self, summation):
- self.sumSet.remove(summation)
- def __str__(self):
- return "Prod object: {0} {1}".format( self.prod, self.sumSet)
- class Summation:
- def __init__(self, prod):
- self.sum = prod
- self.prodSet = set()
- def addProd(self, summation):
- self.prodSet.add(summation)
- def removeSum(self, summation):
- self.prodSet.remove(summation)
- def __str__(self):
- return "Sum object: {0} {1}".format( self.sum, self.prodSet)
- class Graph:
- def __init__(self, minNum, maxNum):
- self.sumDict = dict()
- self.prodDict = dict()
- for a in range(minNum, maxNum+1):
- for b in range(a, maxNum+1):
- self.sumDict.setdefault(a+b, Summation(a+b))
- self.prodDict.setdefault(a*b, Production(a*b))
- self.sumDict.get(a+b).addProd(a*b)
- self.prodDict.get(a*b).addSum(a+b)
- def allProdsWithMultipleSums(self, summ):
- prodSet = self.sumDict[summ].prodSet
- for prod in prodSet:
- if len(self.prodDict[prod].sumSet) <= 1:
- return False
- return True
- def removeSum(self, summ):
- prodSet = self.sumDict[summ].prodSet
- self.sumDict.pop(summ, None)
- for prod in prodSet:
- self.prodDict[prod].sumSet.remove(summ)
- if len(self.prodDict[prod].sumSet) == 0:
- self.prodDict.pop(prod, None)
- def removeProd(self, prod):
- sumSet = self.prodDict[prod].sumSet
- self.prodDict.pop(prod, None)
- for summ in sumSet:
- self.sumDict[summ].prodSet.remove(prod)
- if len(self.sumDict[summ].prodSet) == 0:
- self.sumDict.pop(summ, None)
- graph = Graph(minNum=2, maxNum=99)
- sumsToRemove = {summ for summ in graph.sumDict.keys() if not graph.allProdsWithMultipleSums(summ) }
- for summ in sumsToRemove:
- graph.removeSum(summ)
- prodsToRemove = {prod for prod in graph.prodDict.keys() if len(graph.prodDict.get(prod).sumSet) != 1}
- for prod in prodsToRemove:
- graph.removeProd(prod)
- for s in graph.sumDict.values():
- if len(s.prodSet) == 1:
- print("sum: {0}".format(s.sum))
- print("prod: {0}".format(s.prodSet.pop()))
Add Comment
Please, Sign In to add comment