Skip to content

Instantly share code, notes, and snippets.

@angch
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angch/f4400ec149471eb2bd2a to your computer and use it in GitHub Desktop.
Save angch/f4400ec149471eb2bd2a to your computer and use it in GitHub Desktop.
def spiral(max_x, max_y):
matrix = []
for i in range(0, max_y):
matrix.append([0] * max_x)
x, y, n, xdir, ydir = 0, 0, 0, 1, 0
while 1:
n = n + 1
matrix[y][x] = n
x, y = x + xdir, y + ydir
if (x < 0 or x >= max_x or y < 0 or y >= max_y or matrix[y][x] > 0):
x, y = x - xdir, y - ydir
xdir, ydir = -ydir, xdir
x, y = x + xdir, y + ydir
if (matrix[y][x] > 0):
break
for i in matrix:
for j in i:
print '{0:2}'.format(j),
print
spiral(5, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment