U E D R , A S I H C RSS

Primary Arithmetic/1002

처음 μ ‘κ·Ό

문제 자체 μ½μœΌλ©΄μ„œ κ·Έλƒλ»”해보이긴 ν–ˆλ‹€. 이전에 λ””μ§€ν„Έ 곡학 μˆ˜μ—…λ•Œ 가산기에 λŒ€ν•΄μ„œ λ°°μš΄λ°”λ„ μžˆμ—ˆλ˜ κ΄€κ³„λ‘œ. κ·Έλƒλ¨Έλ¦Ώμ†μ— λŒ€λž΅μ˜ ν•  일듀이 κ·Έλ €μ‘Œλ‹€.

κ·Έλž˜μ„œ 첫 ν…ŒμŠ€νŠΈλ₯Ό λ°”λ‘œ μž‘μ„±ν•˜μ˜€λ‹€.

~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μ°¨

이 μƒνƒœμ—μ„œ '음.. 이정도 κ΅¬ν˜„μ΄λ©΄ μ–΄λ””κΉŒμ§€ κΈ°λŠ₯이 μ»λ²„λ κΉŒ?' λΌλŠ” 의문이 생겼닀. 일단 λ§Œλ“  μ½”λ“œλ“€μ„ μ—°κ²°ν•΄λ³΄μ•˜λ‹€.
~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 λ₯Ό λ‹€μ‹œ λ°œμƒμ‹œν‚¬ λ•Œ)

carry 에 λŒ€ν•΄μ„œλŠ” 별 생각을 μ•ˆν–ˆλ‹€. ν˜„μž¬μ˜ κ΅¬μ‘°λ‘œλŠ” carry μ²˜λ¦¬κ°€ 그리 이쁘게 λ‚˜μ˜¬ 것 κ°™μ§€κ°€ μ•Šμ•˜λ‹€. μ½”λ“œλ₯Ό μ€ λ” μž‘μ„±ν• κΉŒ ν•˜λ‹€κ°€ 일단은 green bar μ—μ„œ λ‚΄λΆ€ 자료 ꡬ쑰만 λ°”κΎΈκΈ°λ‘œ ν–ˆλ‹€.
일단, 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()

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:24:01
Processing time 0.0133 sec