Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- init_table = lambda ncols, nrows: [ [ None ] * ncols for i in xrange(nrows) ]
- def iter_spiral(ncols, nrows):
- row, col = 0, 0
- # RED_FLAG: this is a HACK so that enumerate can be used as the
- # counter and so we can skip the initial '0' value.
- # Bad Doug! (not Hellman... the other one)
- yield row, col
- rowstart, rowstop = 0, nrows
- colstart, colstop = 0, ncols
- while rowstart < rowstop and colstart < colstop:
- for col in xrange(colstart, colstop):
- yield row, col
- rowstart += 1
- for row in xrange(rowstart, rowstop):
- yield row, col
- colstop -= 1
- for col in xrange(colstop-1, colstart-1, -1):
- yield row, col
- rowstop -= 1
- for row in xrange(rowstop-1, rowstart-1, -1):
- yield row, col
- colstart += 1
- def spiral(ncols, nrows):
- ## Initialize an empty table
- table = init_table(ncols, nrows)
- ## Fill it in with values
- for val, (row, col) in enumerate(iter_spiral(ncols, nrows)):
- table[row][col] = val
- ## Print it out
- for row in table:
- for cell in row:
- print "%4d" % cell,
- print
- spiral(*[int(v) for v in sys.argv[1:3]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement