# Spiral Matrix

## Akshay Bist 12th of May 2012

```#!/usr/bin/env python
#
# The problem is defined here - http://rosettacode.org/wiki/Spiral_matrix

def make_spiral(n):
spiral= []
for i in range(n):
spiral.append([])
for j in range(n):
spiral[i].append(None)
maxi=maxj=n-1
mini=minj=i=j=0
for c in range(1, n*n+1):
spiral[i][j]= c
#print c, i, j, mini, minj, maxi, maxj
if i==mini and j<maxj:
j+= 1
elif j==maxj and i<maxi:
i+= 1
elif i==maxi and j>minj:
j-= 1
elif j==minj and i>mini:
i-= 1
if i==maxi and j==maxj:
mini+= 1
elif i==maxi and j==minj:
maxj-= 1
elif i==mini and j==minj:
maxi-= 1
elif i==mini and j==maxj and mini>0:
minj+= 1
return spiral

for i in range(1, 7):
s= make_spiral(i)
for r in s:
print r```
227
Views
0
3

Want to join Code Pad Request An Invite

Suppo - Create, Customize & Host
Your App Support Page at Suppo

### Snippet Details

• Status: Public
• Description: Don't think it's a very good algorithm, but it gets the job done.
• Saved by: Akshay Bist
• Type: Python
• Date: 12th May 2012
• Characters: 778
• Views: 227
• Likes: 0