StrictThemes

Spiral Matrix

#!/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
Don't think it's a very good algorithm, but it gets the job done.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.