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.0214 sec