I saw interesting problem of generating spiral of numbers and realized I could reuse my code for rotation I posted earlier.
Spiral numbers
""" Generate number range spiral of dimensions m x n
"""
def rot_right(a):
return zip(*a[::-1])
def spiral(m, n, start = 1):
""" Generate number range spiral of dimensions m x n
"""
if n == 0:
yield ()
else:
yield tuple(range(start, m + start))
for row in rot_right(list(spiral(n-1, m, m + start))):
yield row
for m, n in (0,5), (6,0), (7,1), (1,4), (6, 7):
print('\nSpiral %i x %i' % (m, n))
for row in spiral(m,n):
print(''.join('%4i' % i for i in row))
""" Output:
Spiral 0 x 5
Spiral 6 x 0
Spiral 7 x 1
1 2 3 4 5 6 7
Spiral 1 x 4
1
2
3
4
Spiral 6 x 7
1 2 3 4 5 6
22 23 24 25 26 7
21 36 37 38 27 8
20 35 42 39 28 9
19 34 41 40 29 10
18 33 32 31 30 11
17 16 15 14 13 12
"""
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.