Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Square spiral solution in Uncategorized category for Square Spiral by capback250
def p(reshetka):
aa = []
for x in zip(*reshetka)[::-1]:
aa.append(list(x))
return aa
def position(n):
size = 2*n + 1
matrix = [[0]*size for x in range(size)]
matrix[size/2][size/2] = 1
matrix[size/2-1][size/2] = 2
matrix[size/2-1][size/2+1] = 3
matrix[size/2][size/2+1] = 4
matrix[size/2+1][size/2+1] = 5
matrix[size/2+1][size/2] = 6
matrix[size/2+1][size/2-1] = 7
matrix[size/2][size/2-1] = 8
matrix[size/2-1][size/2-1] = 9
counter = 9
def increment(COUNT, left_param):
a=[]
leng = left_param*2
for x in range(leng):
a.append(COUNT+x+1)
return a
for l in range(2, n+1):
for c in range(4):
matrix[size/2-l][size/2-(l-1):size/2+l+1] = increment(counter, l)
counter += l*2
matrix = p(matrix)
return matrix
def find_distance(first, second):
field = position(20)
coord1 = []
coord2 = []
for string in range(len(field)):
for number, elem in enumerate(field[string]):
if elem == first:
coord1=[string+1, number+1]
if elem == second:
coord2=[string+1, number+1]
return abs(coord1[0]-coord2[0])+abs(coord1[1]-coord2[1])
Oct. 16, 2015