์ฒ์ ์ ๊ทผ ¶
๋ฌธ์ ์์ฒด ์ฝ์ผ๋ฉด์ ๊ทธ๋ฅ ๋ปํด๋ณด์ด๊ธด ํ๋ค. ์ด์ ์ ๋์งํธ ๊ณตํ ์์
๋ ๊ฐ์ฐ๊ธฐ์ ๋ํด์ ๋ฐฐ์ด๋ฐ๋ ์์๋ ๊ด๊ณ๋ก. ๊ทธ๋ฅ ๋จธ๋ฆฟ์์ ๋๋ต์ ํ ์ผ๋ค์ด ๊ทธ๋ ค์ก๋ค.
๊ทธ๋์ ์ฒซ ํ
์คํธ๋ฅผ ๋ฐ๋ก ์์ฑํ์๋ค.
~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))
๊ทธ๋ฆฌ๊ณ ํ ์ผ์ ์ข ์๊ฐํด๋ณด์๋ค.
1์ฐจ ์๊ณ ๋ฆฌ์ฆ ์๊ฐ ¶
๋ฌธ์ ๋ฅผ ์ด๋ฆฌ์ ๋ฆฌ ๋๋ ๋ณด๋, ์๋ฆฌ์ ํ๋์ ๋ํด์ carry ๊ฐ ๋ฐ์ํ๋์ง๋ฅผ ์ธ๋ ๊ฒ์ด ์ฌ์๋ณด์๋ค. ๊ทธ๋ฆฌ๊ณ ํด๋น ์คํธ๋ง์ ์ผ์ข
์ list ๋ก ๋๋๋ ๊ฒ๋ ์์ผ๋ฉด ์ข์ ๊ฒ ๊ฐ์๋ค. ๊ทธ๋ฆฌ๊ณ carry ์ ๋ํด์ ์ถํ ์์๋ฆฌ์ ๋ํด์ฃผ๋ ์์
๋ฑ์ ํด์ผ ํ ๊ฒ ๊ฐ์๋ค. ์ผ๋จ์ ์ด์ ๋ ๋ ์ฌ๋ฆฌ๊ธฐ๋ก ํ๊ณ , ์์ ๋ ์ผ๋ง ํ์๋ค.
~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))
์.. ์ด ๋ถ๋ถ์ ์์ฑํ๋ ์ค, ์๊ฐํด๋ณด๋ ์
๋ ฅ ๋ฐ์ดํฐ๊ฐ ์คํธ๋ง์ด๋ฉด ๋ ๊ฐ๋จํ ๊ฒ ๊ฐ์๋ค. integer ๋จ์๋ก ๋ํ๊ธฐ ๋ณด๋ค๋ ์๋ฆฌ์ ๋จ์๋ก ์๊ฐํ ๊ฒ์ด๋ ์๊ฐ์ด ๋ค์๋ค. ๊ทธ๋์ ํ
์คํธ ์ฝ๋๋ฅผ ๋ค์ ๋ฐ๊พธ์๋ค. ๊ทธ๋ฌ๊ณ ๋ณด๋, ๊ทธ๋ฅ ๊ตฌํํ ๋ฐฉ๋ฒ์ด ๋ ์ค๋ฅธ๋ค.
~cpp
def toList(numberStr):
return [int(each) for each in numberStr]
.
.
.
def testToList(self):
self.assertEquals([1,2,3], toList('123'))
2์ฐจ ¶
์ด ์ํ์์ '์.. ์ด์ ๋ ๊ตฌํ์ด๋ฉด ์ด๋๊น์ง ๊ธฐ๋ฅ์ด ์ปค๋ฒ๋ ๊น?' ๋ผ๋ ์๋ฌธ์ด ์๊ฒผ๋ค. ์ผ๋จ ๋ง๋ ์ฝ๋๋ค์ ์ฐ๊ฒฐํด๋ณด์๋ค.
์ผ๋จ, testToList ๋ถํฐ. ๋ฌธ์ ์คํ์์ '10์๋ฆฌ ๋ฏธ๋ง ์'๋ผ๋ ์ ์ ๊ทธ๋ฅ ์ด์ฉํด๋ ๋ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์๋ค.
~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๊ฐ์ ๋ํด์๋ ๋ณ ์ผ ์์ด ๋์๊ฐ๋ค. ์ด์ฏค์์ ๊ฑ ๋๋ผ๊น ํ๋ค๊ฐ, ๋๋ฌด ํ์ ํด๋ณด์ด๋ ๊ฒ๋ค์ด ๋ง์ ๋ณด์๋ค. ๊ทธ๋์ ํด๋น ์ํฉ๊ณผ, ๊ทธ ์ํฉ์ ๋ํ ํ
์คํธ์ ์๋ฅผ ์ถ๊ฐํด๋๊ฐ๋ค.- ์๋ฆฟ์๊ฐ ๋์ด๊ฐ๋ ๊ฒฝ์ฐ - 1234 + 123
- 103 + 597 (์ฆ, 0+9 ๊ฐ 1+9 ๊ฐ ๋๋ฉด์ carry ๋ฅผ ๋ค์ ๋ฐ์์ํฌ ๋)
์ผ๋จ, testToList ๋ถํฐ. ๋ฌธ์ ์คํ์์ '10์๋ฆฌ ๋ฏธ๋ง ์'๋ผ๋ ์ ์ ๊ทธ๋ฅ ์ด์ฉํด๋ ๋ ๊ฒ ๊ฐ๋ค๋ ์๊ฐ์ด ๋ค์๋ค.
~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
์ด์ฏค ๋๋, ํ
์คํธ ์ฝ๋๋ค์ ์ ๋ถ ํต๊ณผ. main ์ฝ๋ ์์ฑํ๊ณ ์ฝ๊ฐ ๋ฆฌํฉํ ๋ง.
ํน์๋ ํด์ ๋ง์ง๋ง ํ ์คํธ ์ถ๊ฐํจ.
ํน์๋ ํด์ ๋ง์ง๋ง ํ ์คํธ ์ถ๊ฐํจ.
~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()










