- 괜히 엉성한거 디버그 하기보단 첨부터 다시 짜는게 다 나은거 같다.
~cpp
import unittest
class TugOfWar:
def solutionOfTwoRemain(self,numbers):
if self.sum[0]==0:
self.sum[0]=numbers[0]
self.sum[1]=numbers[1]
self.sum[0]+=numbers[1]
self.sum[1]+=numbers[0]
self.sum.sort()
def solutionOfOddNumber(self,numbers):
if (len(numbers)%2)==1:
numbers.append(0)
numbers.sort()
def calculateEachSum(self,numbers):
self.solutionOfOddNumber(numbers)
self.sum[0]+=numbers[0]+numbers[len(numbers)-1]
self.tmpSum[0]+=numbers[0]+numbers[len(numbers)-2]
self.sum[1]+=numbers[1]+numbers[len(numbers)-2]
self.tmpSum[1]+=numbers[1]+numbers[len(numbers)-1]
self.sum.sort()
self.tmpSum.sort()
def compare(self):
if (self.tmpSum[1]-self.tmpSum[0])<(self.sum[1]-self.sum[0]):
self.sum=self.tmpSum
def removeUsedNumber(self,numbers):
for i in range(2):
if len(numbers)>0:
numbers.pop()
numbers.pop(0)
i+=1
def coupling(self,numbers):
self.sum=[0,0]
self.tmpSum=[0,0]
numbers.sort()
while len(numbers)>0:
if(len(numbers)==2):
self.solutionOfTwoRemain(numbers)
return self.sum
self.solutionOfOddNumber(numbers)
self.calculateEachSum(numbers)
self.compare()
self.removeUsedNumber(numbers)
return self.sum
class testTugOfWar(unittest.TestCase):
def setUp(self):
self.exTugOfWar=TugOfWar()
self.d1=[100,90,200]
self.d2=[45,55,70,60,50,75]
self.d3=[92,56,47,82]
self.d4=[2,3,4,7,8]
self.d5=[50,50,100,200]
def testCoupling(self):
self.assertEqual([190,200],self.exTugOfWar.coupling(self.d1))
self.assertEqual([175,180],self.exTugOfWar.coupling(self.d2))
self.assertEqual([138,139],self.exTugOfWar.coupling(self.d3))
self.assertEqual([12,12],self.exTugOfWar.coupling(self.d4))
self.assertEqual([150,250],self.exTugOfWar.coupling(self.d5))
if __name__=='__main__':
unittest.main()