Advertisement
here2share

# dict_sq_spiral_256.py

May 15th, 2025
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. # dict_sq_spiral_256.py
  2.  
  3. size = 16
  4.  
  5. def create_spiral_matrix(size):
  6.     x, y = size - 1, size - 1
  7.     n = 0
  8.     i = 0
  9.  
  10.     while 1:
  11.         matrix[x, y] = n
  12.         n += 1
  13.         t = 1 if matrix.get((x, y + 1), 0) == -1 else 0
  14.         for x0, y0 in [(-1, 0), (0, -1), (1, 0), (0, 1)][t:]: # Left, Up, Right, Down
  15.             x0, y0 = x + x0, y + y0
  16.             p = matrix.get((x0, y0), 0)
  17.             if p == -1:
  18.                 x, y = x0, y0
  19.                 break
  20.         if p != -1:
  21.             return matrix
  22.    
  23. matrix = {(i, j): -1 for i in range(size) for j in range(size)}
  24. spiral_matrix = create_spiral_matrix(size)
  25.  
  26. for j in range(size):
  27.     row = [f"{matrix[i, j]:3d}" for i in range(size)]
  28.     print(" ".join(row))
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement