각 .
4x5 array,
게 , (과 고) →→→ ↓↓↓↓↓ ←←← ↑↑↑ →→ ↓↓ ← ↑ . 기 간 규 기 ( ) 과 .
4x5 array,
| 1 | 2 | 3 | 4 |
| 14 | 15 | 16 | 5 |
| 13 | 20 | 17 | 6 |
| 12 | 19 | 18 | 7 |
| 11 | 10 | 9 | 8 |
게 , (과 고) →→→ ↓↓↓↓↓ ←←← ↑↑↑ →→ ↓↓ ← ↑ . 기 간 규 기 ( ) 과 .
= +1
간 고, 꿔 값 간.
근 , 게 .
~cpp
# -*- coding: UTF-8 -*
import unittest
def makeRotList(row, col):
rotList = []
row -= 1
col -= 1
rotList.append(row)
while True:
#cols
rotList.append(col)
if col == 1:
rotList.append(row)
break
#rows
rotList.append(row)
if row == 1:
rotList.append(col-1) # ( ) 기 1
break
row-=1
col-=1
return rotList
def makeSpirialArray(rows, cols):
s = Sequence()
SpirialArray = []
for i in range(rows):
SpirialArray += [[0]*cols]
index = [0,0]
value = 0
SpirialArray[index[0]][index[1]] = value + 1
value += 1
rotList = makeRotList(rows, cols)
for i in rotList:
dir = s.next()
for j in range(i):
index[0] += dir[0]
index[1] += dir[1]
SpirialArray[index[0]][index[1]] = value + 1
value += 1
print index
return SpirialArray
class Sequence:
def __init__(self):
self.sequence = [[1,0],[0,1],[-1,0],[0,-1]]
self.idx = 3
def next(self):
if self.idx == 3:
self.idx = 0
else:
self.idx += 1
return self.sequence[self.idx]
class MyTest(unittest.TestCase):
def testNextSeqence(self):
s = Sequence()
self.assertEquals([1,0], s.next())
self.assertEquals([0,1], s.next())
self.assertEquals([-1,0], s.next())
self.assertEquals([0,-1], s.next())
self.assertEquals([1,0], s.next())
def testRotList(self):
expected = [5,3,5,2,4,1,3]
self.assertEquals(expected, makeRotList(6,4))
expected = [3,5,3,4,2,3,1,2]
self.assertEquals(expected, makeRotList(4,6))
expected = [2,2,2,1,1]
self.assertEquals(expected, makeRotList(3,3))
expected = [3,3,3,2,2,1,1]
self.assertEquals(expected, makeRotList(4,4))
def testArrayCreation(self):
testArr = [[0]*3]*3
expected = [[0,0,0],[0,0,0],[0,0,0]]
self.assertEquals(expected, testArr)
if __name__=='__main__':
#unittest.main(argv=('','-v'))
print makeSpirialArray(3,3)
print makeSpirialArray(6,4)










