U E D R , A S I H C RSS

Spiral Array/Leonardong

ํ”ผ๋ผ๋ฏธ๋“œ ์˜ค๋ฅด๊ธฐ

2h 20m 28s
์•„์ด๋””์–ด๋Š” JuNe ์„ ๋ฐฐ๊ฐ€ ๋งํ–ˆ๋˜ ๊ฒƒ์ด๋‹ค.(์ €๋ฒˆ ์ž๋ฐ” ์ปจํผ๋Ÿฐ์Šค์—์„œ ์˜€๋˜๊ฐ€..) ํ•œ ๋ฒˆ ๋‚˜์„ ํ˜•์œผ๋กœ ์ง„ํ–‰๋˜๋Š” ๊ฒƒ์„ ๊ฐ™์€ ์ธต์œผ๋กœ ๋ณธ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ์€ 1์ธต, ๋‹ค์Œ ์•ˆ์ชฝ์€ 2์ธต ์ด๋Ÿฐ ์‹์œผ๋กœ ์•ˆ์ชฝ์œผ๋กœ ๊ฐˆ์ˆ˜๋ก ๋†’์ด๊ฐ€ ๋†’์•„์ง„๋‹ค. ํ•œ ์‚ฌ๋žŒ์ด ํ”ผ๋ผ๋ฏธ๋“œ๋ฅผ ํ•œ ๋ฐ”ํ€ด ๋Œ๊ณ  ๋‹ค์Œ ์ธต์œผ๋กœ ์˜ฌ๋ผ๊ฐ€๋ฉด์„œ ์ž์‹ ์ด ๋“ค๋ €๋˜ ๊ณณ์ด ๋ช‡ ๋ฒˆ์งธ์ธ์ง€, ์ขŒํ‘œ๋Š” ๋ฌด์—‡์ธ์ง€ ๊ธฐ์–ตํ•œ๋‹ค. ํ•œ ์ธต์„ ๋‹ค ๋Œ๋ฉด ์‹œ์ž‘ํ–ˆ๋˜ ์ž๋ฆฌ๋กœ ๋Œ์•„์˜ค๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๋ณตํ•ด์„œ ๊ธฐ์–ตํ•œ ์ขŒํ‘œ๋Š” ์ง€์šฐ๊ณ  ๋‹ค์Œ ์ธต์œผ๋กœ ์ด๋™ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ ์—ฌ์ง€๊ป ๊ทธ๋Ÿฌํ•œ ์ ‘๊ทผ๋ฒ•์„ ์•Œ๊ณ ์„œ๋„ TDD๋กœ ํ’€์ง€๋ฅผ ๋ชปํ–ˆ์—ˆ๋‹ค. ๋งค๋ฒˆ ๋‚˜์„ ํ˜• "ํ–‰๋ ฌ"์— ์–ด๋–ป๊ฒŒ ์ˆซ์ž๋ฅผ ์ƒˆ๊ธธ์ง€๋งŒ ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‹ค ๋ณด๋‹ˆ 2์ฐจ์› ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค๋ฅผ ์กฐ์ž‘ํ•˜๋Š” ์ˆ˜์ค€์—์„œ ์ƒ๊ฐ์ด ๋ฒ—์–ด๋‚˜์งˆ ๋ชปํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ ์‚ฌ์‹ค์€ ์›€์ง์ž„(์ด์ „์˜ ์ธ๋ฑ์Šค ์กฐ์ž‘), ์›€์ง์ธ ์ ๋“ค, ํ–‰๋ ฌ์„ ๋”ฐ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์•„! ์ด๋ ‡๊ฒŒ ํ…Œ์ŠคํŠธ ํ•˜๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜!

TDD๋กœ ํ’€์—ˆ๋‹ค๋Š” ์ ์ด ๊ธฐ์˜๋‹ค. ์ฒ˜์Œ๋ถ€ํ„ฐ ๋„ˆ๋ฌด ๋ฉ”์„œ๋“œ๋ฅผ ์–ด๋””์— ์†ํ•˜๊ฒŒ ํ•  ์ง€ ๊ณ ๋ฏผํ•˜์ง€ ์•Š๊ณ  ์‹œ์ž‘ํ•œ ๊ฒƒ์ด ์œ ์šฉํ–ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋กœ ์˜ˆ์ „ ๊ฐ™์œผ๋ฉด ์ƒ๊ฐํ•˜์ง€ ๋ชปํ–ˆ์„ Directionํด๋ž˜์Šค์™€ ๊ทธ ํ•˜์œ„ ํด๋ž˜์Šค๊ฐ€ ํƒ„์ƒํ–ˆ๋‹ค. ๋˜ํ•œ ํ–‰๋ ฌ์€ ์ตœ์ข… ๊ฒฐ๊ณผ๋ฌผ์„ ์ €์žฅํ•˜๊ณ  ๋ณด์—ฌ์ฃผ๋Š” ์ผ์ข…์˜ ๋ทฐ์ฒ˜๋Ÿผ ์“ฐ์˜€๋‹ค.

ํ˜„์žฌ๋Š” ํ–‰๋ ฌ ๊ตฌ์„ฑ์ด ๋น„ํšจ์œจ์ ์ด๋‹ค. ์›€์ง์˜€๋˜ ๊ธฐ๋ก์„ ๊ฐ€์ง€๊ณ  ํ–‰๋ ฌ์„ ๊ตฌ์„ฑํ•˜๊ธฐ๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค.์ด๊ฒƒ์„ ์ˆ˜์ •ํ•  ๋•Œ ์ข€๋” ํšจ์œจ์ ์œผ๋กœ ์ž‘๋™ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์•ผ๊ฒ ๋‹ค. Moverํด๋ž˜์Šค, Arrayํด๋ž˜์Šค์˜ ์ข…๋ฃŒ๊ฒ€์‚ฌ, ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ™•์‹คํžˆ Refactoring์ด ํ•„์š”ํ•˜๋‹ค.


~cpp
import unittest

ROW = 0
COL = 1

class Direction:
    def move(self, coordinate, board):
        if ( board.isWall( self.next(coordinate) ) ):    
            return coordinate
        return self.next(coordinate)

class Down(Direction):
    def next(self, coordinate):
        return (coordinate[ROW] + 1, coordinate[COL])
    
class Up( Direction ):
    def next(self, coordinate):
        return (coordinate[ROW] - 1, coordinate[COL])

class Right(Direction):
    def next(self, coordinate):
        return (coordinate[ROW], coordinate[COL] + 1 )

class Left(Direction):
    def next(self, coordinate):
        return (coordinate[ROW], coordinate[COL] - 1 )


class Board:
    def __init__(self):
        self.size = 0
    def setBoundary( self, start, end ):
        self.start = start
        self.end = end
    def isWall( self, coordinate ):
        if ( coordinate[ROW] < self.start ):
            return True
        elif ( coordinate[ROW] >= self.end ):
            return True
        elif ( coordinate[COL] < self.start ):
            return True
        elif ( coordinate[COL] >= self.end ):
            return True
        return False
    def decreaseBoundary(self, amount):
        self.setBoundary( self.start+amount, self.end-amount )
    

class Mover:
    def __init__(self):
        self.coordinate = (0,0)
        self.moveCount = 0
        self.history = []
    def position(self):
        return self.coordinate
    def goStraight(self, direction, board):
        while ( not self.coordinate == direction.move(self.coordinate, board) ):
            self.history.append( Point( self.coordinate, self.moveCount+1 ) )
            self.moveCount += 1
            self.coordinate = direction.move( self.coordinate, board )
        self.history.append( Point( self.coordinate, self.moveCount+1 ) )
    def mark(self, point, value):
        point.value = value
    def _setMoveCount(self, count):
        self.moveCount = count
    def _setPosition(self, coordinate):
        self.coordinate = coordinate
    def getHistory(self):
        return self.history
    def turnRound(self, board):
        self.goStraight( Right(), board )
        self.goStraight( Down(), board )
        self.goStraight( Left(), board )
        self.goStraight( Up(), board )
        board.decreaseBoundary(1)
        self.eraseLastMovement()
        self._setPosition( self.nextFloor() )
    def nextFloor(self):
        return (self.coordinate[ROW]+1, self.coordinate[COL]+1 )
    def eraseLastMovement(self):
        self._setMoveCount( self.history.pop().value - 1 )
        
class Point:
    def __init__(self, coordinate, value):
        self.coordinate = coordinate
        self.value = value

class Array:
    def __init__(self, size):
        self.matrix = []
        for i in range(size):
            self.matrix.append( [Point((-1,-1),-1)] * size )
    def isAllFilled(self):
        for row in self.matrix:
            for point in row:
                if ( point.value < 0 ):
                    return False
        return True
    def _fillAll(self):
        for row in self.matrix:
            for point in row:
                point.value = 1
    def get(self, row, col):
        return self.matrix[row][col]
    def construct(self, pointList):
        for p in pointList:
            self.matrix[p.coordinate[ROW]][p.coordinate[COL]] = p
    def printValues(self):
        for row in self.matrix:
            for point in row:
                print point.value,
                print "\t",
            print

class SpiralArrayTest(unittest.TestCase):
    def setUp(self):
        self.board = Board()
        self.size = 10
        self.board.setBoundary(start=0, end=self.size)
        self.mover = Mover()
        self.array = Array( self.size )
    def testMoveDown(self):
        self.assertEquals( (1,0), Down().move( (0,0), self.board ) )
        self.assertEquals( (self.size-1,0), Down().move( (self.size-1,0), self.board ) )
    def testMoveUp(self):
        self.assertEquals( (0,0), Up().move( (0,0), self.board ) )
        self.assertEquals( (self.size-1,0), Up().move( (self.size,0), self.board ) )
    def testIsWall(self):
        self.assertEquals( False, self.board.isWall( (0,0) ) )
        self.assertEquals( True, self.board.isWall( (-1,0) ) )
        self.assertEquals( True, self.board.isWall( (0,-1) ) )
        self.assertEquals( True, self.board.isWall( (0,self.size) ) )
        self.assertEquals( True, self.board.isWall( (self.size,0) ) )
    def testGoStraightRight(self):
        self.mover.goStraight(Right(), self.board)
        self.assertEquals( (0,self.size-1), self.mover.position() )        
    def testMark(self):
        point = Point((0,0),0)
        self.mover._setMoveCount(1)
        self.mover.mark( point, self.mover.moveCount )
        self.assertEquals( 1, point.value )
    def testStorePointCoordinate(self):
        self.mover.goStraight( Down(), self.board )
        points = self.mover.getHistory()
        self.assertEquals( (self.size-1,0), points.pop().coordinate )
        self.assertEquals( (self.size-2,0), points.pop().coordinate )
    def testStoreMoveCount(self):
        self.mover._setPosition((self.size-1, self.size-1))
        self.mover.goStraight( Left(), self.board )
        points = self.mover.getHistory()
        self.assertEquals( self.size, points.pop().value )
        self.assertEquals( self.size-1, points.pop().value )
    def testTurnRound(self):
        self.mover._setPosition( (0,0) )
        self.mover.turnRound(self.board)
        self.assertEquals( (1,0), self.mover.getHistory().pop().coordinate )
    def testIsFinished(self):
        self.assertEquals( False, self.array.isAllFilled() )
        self.array._fillAll()
        self.assertEquals( True, self.array.isAllFilled() )        
    def testConstructArray(self):
        self.mover._setPosition( (0,0) )
        self.mover.turnRound(self.board)
        self.array.construct( self.mover.getHistory() )
        self.assertEquals( (0,0), self.array.get(0,0).coordinate )
        self.assertEquals( (self.size-1, self.size-1),
                           self.array.get(self.size-1, self.size-1).coordinate )
if __name__ == "__main__":
##    unittest.main()
    inputSize = input()
    board = Board()
    board.setBoundary(0, inputSize )
    array = Array(inputSize)
    mover = Mover()
    mover._setPosition((0,0))
    while not ( array.isAllFilled() ):
        mover.turnRound(board)
        array.construct(mover.getHistory())
    array.printValues()

ํ”ผ๋ผ๋ฏธ๋“œ ์˜ค๋ฅด๊ธฐ Refacotring

1h 58m 26s

์ง€๋‚œ ๋ฒˆ ๋ฆฌํŒฉํ† ๋ง ๋Œ€์ƒ์ด์—ˆ๋˜ Moverํด๋ž˜์Šค, Arrayํด๋ž˜์Šค์˜ ์ข…๋ฃŒ๊ฒ€์‚ฌ, ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋ฅผ ๋ฆฌํŒฉํ† ๋ง ํ–ˆ๋‹ค. ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์™€ Arrayํด๋ž˜์Šค๋Š” ์‰ฝ๊ฒŒ ๋ฆฌํŒฉํ† ๋ง ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ Moverํด๋ž˜์Šค๋ฅผ ์†๋Œ€๋Š”๋ฐ ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค.

goStraight ์ „ ๋ฒ„์ „์€ Directionํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด์„œ ์ด๋™ํ•œ(๋ฒฝ์„ ๋งŒ๋‚˜๋ฉด ์ด๋™ํ•˜์ง€ ์•Š์Œ) ์œ„์น˜๋ฅผ ์–ป์–ด๋‚ด๊ณ , ์ด๋™ํ•œ ๊ธฐ๋ก์„ ์ €์žฅํ•˜๋Š” ํ˜•์‹์ด์—ˆ๋‹ค. ๋ฒฝ์— ๋Œ€ํ•œ ๊ฒ€์‚ฌ๊ฐ€ ๊ฒน์น˜๋Š” ๊ฒƒ ๊ฐ™์•„ mover์—์„œ ๋ฐ”๋กœ ๋ฒฝ์„ ๊ฒ€์‚ฌํ•˜๊ณ  ๋ฒฝ์— ๋“ค์–ด์„œ๋ฉด ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋ณด๋‹ˆ ๋”ฐ๋กœ ์นด์šดํŠธ ํ•  ํ•„์š”๊ฐ€ ์—†์–ด moveCount๋ณ€์ˆ˜๋ฅผ ์—†์•ด๋‹ค. mover๊ฐ€ ์ข…๋ฃŒ ์กฐ๊ฑด๋„ ๊ฒ€์‚ฌํ•˜๋Š”๋ฐ board ๋„“์ด๋งŒํผ ์ด๋™ํ–ˆ์œผ๋ฉด ๋๋‚˜๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ๋ฒฝ์— ๋“ค์–ด์„œ์•ผ ์ข…๋ฃŒํ•˜๋‹ค ๋ณด๋‹ˆ๊นŒ mover๋ฅผ ๋ฒฝ์— ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ์œ„์น˜๋กœ ๋˜๋Œ๋ ค๋†“์•„์•ผ ํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ direction์— ๋ชจ๋‘ previous ๋ฉ”์„œ๋“œ๊ฐ€ ์ƒ๊ฒผ๋‹ค. ํ•œ๋ฐ ๋‹ค์Œ ๋ฒˆ goStraight๋ฅผ ํ•  ๋•Œ๋Š” ์ด๋ฏธ ์ด๋™ํ–ˆ๋˜ ๊ธฐ๋ก์ด ๋‚จ์•„์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋งค๋ฒˆ goStraight๋ฅผ ํ•  ๋•Œ๋งˆ๋‹ค ๋งˆ์ง€๋ง‰ ์ด๋™ ๊ธฐ๋ก์„ ์‚ญ์ œํ–ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ค๋ณด๋‹ˆ boardํฌ๊ธฐ๊ฐ€ 1์ผ ๊ฒฝ์šฐ๋Š” ์ด๋™ํ•œ ๊ธฐ๋ก์ด ๋ชจ๋‘ ์ง€์›Œ์ ธ๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์ด ์•„๋‹Œ๊ฐ€. ์กฐ์žกํ•˜์ง€๋งŒ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค.

๊ฒฝ๊ณ„์กฐ๊ฑด์ด ์ฐธ ๋ฏธ๋ฌ˜ํ•˜๋‹ค๋Š” ๊ฒƒ์„ ๋А๊ผˆ๋‹ค. ์‹œ์ž‘๊ณผ ๋์„ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ๊ฐ€? ํ•œ์ฐธ์„ ํ—ค๋งค๋‹ค๋ณด๋‹ˆ ๋” ๋‚˜์€ ๋ฐฉ๋ฒ•์ด ์žˆ๋Š”๋ฐ๋„ ์ฐพ์งˆ ๋ชปํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

์ฝ”๋“œ ์•ˆ์—์„œ ํ—ค๋งค๊ธฐ ๋ณด๋‹ค๋Š” ์ •ํ™•ํžˆ ์ƒ๊ฐ์„ ์ •๋ฆฌํ•ด์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค. ์ด๊ฒƒ ํ•ด๋ณด๊ณ  ์ €๊ฒƒ ํ•ด๋ณด๋Š” ์‚ฌ์ด์— ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ๋งŽ์ด ํ˜๋Ÿฌ๊ฐ”๋‹ค. ๊ฒฐ๊ตญ์— ๋‹ต์€ ๋‚˜์™”์ง€๋งŒ, ์ด๋ณด๋‹ค ๋นจ๋ฆฌ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.


~cpp

import unittest

ROW = 0
COL = 1

class Direction:
    pass
class Down(Direction):
    def next(self, coordinate):
        return (coordinate[ROW] + 1, coordinate[COL])
    def previous(self, coordinate):
        return Up().next( coordinate )
    
class Up( Direction ):
    def next(self, coordinate):
        return (coordinate[ROW] - 1, coordinate[COL])
    def previous(self, coordinate):
        return Down().next( coordinate )

class Right(Direction):
    def next(self, coordinate):
        return (coordinate[ROW], coordinate[COL] + 1 )
    def previous(self, coordinate):
        return Left().next( coordinate )

class Left(Direction):
    def next(self, coordinate):
        return (coordinate[ROW], coordinate[COL] - 1 )
    def previous(self, coordinate):
        return Right().next( coordinate )


class Board:
    def __init__(self, initialSize):
        self.size = initialSize
        self.setBoundary( 0, self.size )
    def setBoundary( self, start, end ):
        self.start = start
        self.end = end
    def isWall( self, coordinate ):
        if ( coordinate[ROW] < self.start ):
            return True
        elif ( coordinate[ROW] >= self.end ):
            return True
        elif ( coordinate[COL] < self.start ):
            return True
        elif ( coordinate[COL] >= self.end ):
            return True
        return False
    def decreaseBoundary(self, amount):
        self.setBoundary( self.start+amount, self.end-amount )
    

class Mover:
    def __init__(self):
        self.coordinate = (0,0)
        self.history = []
    def position(self):
        return self.coordinate
    def goStraight(self, direction, board):
        while ( not board.isWall( self.coordinate ) ):
            self.history.append( Point( self.coordinate, self.moveCount() ) )
            self.coordinate = direction.next( self.coordinate )
        self.coordinate = direction.previous( self.coordinate )
    def mark(self, point, value):
        point.value = value
    def moveCount(self):
        return len( self.history ) + 1
    def setPosition(self, coordinate):
        self.coordinate = coordinate
    def getHistory(self):
        return self.history
    def turnRound(self, board):
        for direction in [Right(), Down(), Left()]:
            self.goStraight( direction, board )
            self.eraseLastMovement()
        self.goStraight( Up(), board )
        if ( board.end - board.start != 1 ):
            self.eraseLastMovement()
        self.setPosition( self.nextFloor() )
    def nextFloor(self):
        return (self.coordinate[ROW]+1, self.coordinate[COL]+1 )
    def eraseLastMovement(self):
        self.history.pop()
    def isFinished( self, board ):
        return board.size * board.size < self.moveCount()
    def lastHistory( self ):
        return self.history[-1]
            
        
class Point:
    def __init__(self, coordinate, value):
        self.coordinate = coordinate
        self.value = value

class Array:
    def __init__(self, size):
        self.matrix = []
        for i in range(size):
            self.matrix.append( [Point((-1,-1),-1)] * size )
    def get(self, row, col):
        return self.matrix[row][col]
    def construct(self, pointList):
        for p in pointList:
            self.matrix[p.coordinate[ROW]][p.coordinate[COL]] = p
    def printValues(self):
        for row in self.matrix:
            for point in row:
                print point.value,
                print "\t",
            print

class SpiralArrayTest(unittest.TestCase):
    def setUp(self):
        self.size = 10
        self.board = Board(self.size)
        self.board.setBoundary(start=0, end=self.size)
        self.mover = Mover()
        self.array = Array( self.size )


class DirectionTest(SpiralArrayTest):
    def testNext(self):
        self.assertEquals( (1,0), Down().next( (0,0) ) )

    def testPrevious(self):
        self.assertEquals( (0,0), Down().previous( (1,0) ) )

class BoardTest(SpiralArrayTest):
    def testIsWall(self):
        self.assertEquals( False, self.board.isWall( (0,0) ) )
        self.assertEquals( True, self.board.isWall( (-1,0) ) )
        self.assertEquals( True, self.board.isWall( (0,-1) ) )
        self.assertEquals( True, self.board.isWall( (0,self.size) ) )
        self.assertEquals( True, self.board.isWall( (self.size,0) ) )

class MoverTest(SpiralArrayTest):
    def testGoStraightRight(self):
        self.mover.goStraight(Right(), self.board)
        self.assertEquals( (0,self.size-1), self.mover.position() )

    def testStorePointCoordinate(self):
        self.mover.goStraight( Down(), self.board )
        points = self.mover.getHistory()
        self.assertEquals( (self.size-1,0), points.pop().coordinate )
        self.assertEquals( (self.size-2,0), points.pop().coordinate )
    def testStoreMoveCount(self):
        self.mover.setPosition((self.size-1, self.size-1))
        self.mover.goStraight( Left(), self.board )
        points = self.mover.getHistory()
        self.assertEquals( self.size, points.pop().value )
        self.assertEquals( self.size-1, points.pop().value )
    def testStoreMovementWhenTurnRoundAndBoudaryNotOne(self):
        self.mover.setPosition( (0,0) )
        self.mover.turnRound(self.board)
        points = self.mover.getHistory()
        self.assertEquals( (self.size-1) * 4 , len( points ) )
    def testStoreMovementWhenTurnRoundAndBoudaryOne(self):
        self.board = Board( initialSize = 1 )
        self.mover.setPosition( (0,0) )
        self.mover.turnRound(self.board)
        points = self.mover.getHistory()
        self.assertEquals( 1 , len( points ) )
    def testTurnRoundWithBoundary0(self):
        self.mover.setPosition( (0,0) )
        self.mover.turnRound(self.board)
        self.assertEquals( (1,0), self.mover.lastHistory().coordinate )
    def testIsFinished(self):
        self.board = Board( initialSize = 1 )
        self.assertEquals( False, self.mover.isFinished( self.board ) )
        self.mover.turnRound( self.board )
        self.assertEquals( True, self.mover.isFinished( self.board ) )
    def testRun(self):
        self.board = Board( initialSize = 3 )
        self.mover.setPosition( (0,0) )
        self.mover.turnRound(self.board)
        self.assertEquals( (1,0), self.mover.lastHistory().coordinate )
        self.assertEquals( (1,1), self.mover.position() )

        self.board.decreaseBoundary(1)
        self.mover.turnRound(self.board)
        self.assertEquals( (1,1), self.mover.lastHistory().coordinate )

class ArrayTest(SpiralArrayTest):    
    def testConstructArray(self):
        self.mover.setPosition( (0,0) )
        self.mover.turnRound(self.board)
        self.array.construct( self.mover.getHistory() )
        self.assertEquals( (0,0), self.array.get(0,0).coordinate )
        self.assertEquals( (self.size-1, self.size-1),
                           self.array.get(self.size-1, self.size-1).coordinate )
        

    
if __name__ == "__main__":
    unittest.main()
    inputSize = input()
    board = Board(inputSize)
    mover = Mover()
    mover.setPosition((0,0))
    while ( not mover.isFinished( board ) ):
        mover.turnRound(board)
        board.decreaseBoundary(1)
    array = Array(inputSize)
    array.construct(mover.getHistory())
    array.printValues()

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:05
Processing time 0.0103 sec