μ²μ μ κ·Ό ¶
λ¬Έμ μ체 μ½μΌλ©΄μ κ·Έλ₯ λ»ν΄λ³΄μ΄κΈ΄ νλ€. μ΄μ μ λμ§νΈ 곡ν μμ
λ κ°μ°κΈ°μ λν΄μ λ°°μ΄λ°λ μμλ κ΄κ³λ‘. κ·Έλ₯ λ¨Έλ¦Ώμμ λλ΅μ ν μΌλ€μ΄ κ·Έλ €μ‘λ€.
κ·Έλμ 첫 ν
μ€νΈλ₯Ό λ°λ‘ μμ±νμλ€.
~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()










