~cpp class PrimaryArithmeticTest(unittest.TestCase): def testCarry(self): self.assertEquals(0,carryCount(123,456))
~cpp def carryCount(one,two): result = 0 return result class PrimaryArithmeticTest(unittest.TestCase): def testCarry(self): self.assertEquals(0,carryCount(123,456))
~cpp def hasCarry(one,two): return one+two >= 10 . . def testCarryOne(self): self.assert_(not hasCarry(3,5)) self.assert_(hasCarry(5,5)) self.assert_(hasCarry(8,5))
~cpp def toList(number): . . . def testToList(self): self.assertEquals([1,2,3], toList(123))
~cpp def toList(numberStr): return [int(each) for each in numberStr] . . . def testToList(self): self.assertEquals([1,2,3], toList('123'))
~cpp def carryCount(one,two): resultCount = 0 oneList,twoList = toList(one),toList(two) for eachOne,eachTwo in zip(oneList,twoList): if hasCarry(eachOne,eachTwo): resultCount += 1 return resultCount class PrimaryArithmeticTest(unittest.TestCase): def testCarry(self): self.assertEquals(0,carryCount('123','456')) self.assertEquals(3,carryCount('123','456')) self.assertEquals(1,carryCount('123','456'))일단 예제로 있던 테스트 3개에 대해서는 별 일 없이 돌아갔다. 이쯤에서 걍 끝낼까 하다가, 너무 허술해보이는 것들이 많아 보였다. 그래서 해당 상황과, 그 상황에 대한 테스트의 예를 추가해나갔다.
~cpp def withNullList(numberStr): result = [0 for each in range(10-len(numberStr))] numbers = [int(each) for each in numberStr] return result + numbers def testToList(self): self.assertEquals([0,0,0,0,0,0,0,1,2,3], withNullList('123')) self.assertEquals([0,0,0,0,0,0,0,5,5,5], withNullList('555'))
~cpp def carryCount(one,two): resultCount = 0 oneList,twoList = withNullList(one),withNullList(two) for idx in range(9,-1,-1): if hasCarry(oneList[idx],twoList[idx]): resultCount += 1 oneList[idx-1] += 1 return resultCount
~cpp def testCarry(self): testSet = [ ['123','456',0], ['555','555',3], ['123','594',1], ['103','597',2], ['1234','123',0], ['1234','236',1], ['999999999','1',9] ] for eachTest in testSet: one,two,expected= eachTest self.assertEquals(expected,carryCount(one,two))마지막 경우도 역시 통과. 아까보다는 좀 더 나아보여서 이즈음에서 그만 둠.
~cpp import unittest LIMIT_NUMBER = 10 def carryCount(one,two): resultCount = 0 oneList,twoList = withNullList(one),withNullList(two) for idx in range(LIMIT_NUMBER-1,-1,-1): if hasCarry(oneList[idx],twoList[idx]): resultCount += 1 oneList[idx-1] += 1 return resultCount def hasCarry(one,two): return one+two >= 10 def nullList(nullCount): return [0 for each in range(LIMIT_NUMBER-nullCount)] def numberList(numberStr): return [int(each) for each in numberStr] def withNullList(numberStr): return nullList(len(numberStr)) + numberList(numberStr) class PrimaryArithmeticTest(unittest.TestCase): def testCarry(self): testSet = [ ['123','456',0], ['555','555',3], ['123','594',1], ['103','597',2], ['1234','123',0], ['1234','236',1], ['999999999','1',9] ] for eachTest in testSet: one,two,expected= eachTest self.assertEquals(expected,carryCount(one,two)) def testCarryOne(self): self.assert_(not hasCarry(3,5)) self.assert_(hasCarry(5,5)) self.assert_(hasCarry(8,5)) def testToList(self): self.assertEquals([0,0,0,0,0,0,0,1,2,3], withNullList('123')) self.assertEquals([0,0,0,0,0,0,0,5,5,5], withNullList('555')) def main(): f=open("input.txt") for eachLine in f: one,two = eachLine.split() if one == '0' and two == '0': break count = carryCount(one,two) if count == 0: count = "No" if count > 1: operationMessage = "operations" else: operationMessage = "operation" print "%s carry %s." % (count, operationMessage) f.close() if __name__=="__main__": unittest.main(argv=('','-v')) #main()