Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # dict_sq_spiral_256.py
- size = 16
- def create_spiral_matrix(size):
- x, y = size - 1, size - 1
- n = 0
- i = 0
- while 1:
- matrix[x, y] = n
- n += 1
- t = 1 if matrix.get((x, y + 1), 0) == -1 else 0
- for x0, y0 in [(-1, 0), (0, -1), (1, 0), (0, 1)][t:]: # Left, Up, Right, Down
- x0, y0 = x + x0, y + y0
- p = matrix.get((x0, y0), 0)
- if p == -1:
- x, y = x0, y0
- break
- if p != -1:
- return matrix
- matrix = {(i, j): -1 for i in range(size) for j in range(size)}
- spiral_matrix = create_spiral_matrix(size)
- for j in range(size):
- row = [f"{matrix[i, j]:3d}" for i in range(size)]
- print(" ".join(row))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement