Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/tasks/spiral-matrix/oracle.py # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / tasks / spiral-matrix / oracle.py` 22 lines ยท 831 bytes Raw Blame History 1 # Independent reference for "Spiral matrix" (Python 3, standard library only). 2 # expected.txt is this program's stdout; omc never produced it. 3 # peel the outer ring and recurse on the inner square: ring numbers go right, down, left, up 4 def spiral(n): 5 m = [[0] * n for _ in range(n)] 6 k = 0 7 for layer in range((n + 1) // 2): 8 lo, hi = layer, n - 1 - layer 9 if lo == hi: 10 m[lo][lo] = k 11 break 12 cells = ([(lo, c) for c in range(lo, hi)] + [(r, hi) for r in range(lo, hi)] + 13 [(hi, c) for c in range(hi, lo, -1)] + [(r, lo) for r in range(hi, lo, -1)]) 14 for r, c in cells: 15 m[r][c] = k 16 k += 1 17 return m 18 for i, n in enumerate((5, 8)): 19 if i: 20 print() 21 for row in spiral(n): 22 print("".join(f"{v:3}" for v in row))