Skip to content

Instantly share code, notes, and snippets.

@SCOTT-HAMILTON
Last active October 15, 2022 16:02
Show Gist options
  • Save SCOTT-HAMILTON/d390f34b4f273741e1e3a475adb95883 to your computer and use it in GitHub Desktop.
Save SCOTT-HAMILTON/d390f34b4f273741e1e3a475adb95883 to your computer and use it in GitHub Desktop.
Exo32 TP-4 info listes
from pprint import pprint
import math
def Exo32(n):
M = []
for i in range(n):
M += [[None] * n]
def fill(bounds, counter, i=None, j=None, reverse=False):
assert (i == None) ^ (j == None)
N = bounds[1] - bounds[0] + 1
for k in range(N):
pos = (0, 0)
if j == None:
pos = (i, bounds[0] + k)
else:
pos = (bounds[0] + k, j)
M[pos[0]][pos[1]] = counter + k if not reverse else counter + N - k - 1
return counter + N
line_bounds = [0, n - 1]
column_bounds = [1, n - 1]
counter = 1
i = 0
j = 0
while line_bounds[1] > line_bounds[0] or column_bounds[1] > column_bounds[0]:
counter = fill(line_bounds, counter, i=i)
j = line_bounds[1]
line_bounds[1] -= 1
counter = fill(column_bounds, counter, j=j)
i = column_bounds[1]
column_bounds[1] -= 1
counter = fill(line_bounds, counter, i=i, reverse=True)
j = line_bounds[0]
line_bounds[0] += 1
counter = fill(column_bounds, counter, j=j, reverse=True)
i = column_bounds[0]
column_bounds[0] += 1
if n % 2 != 0:
i = math.trunc(n / 2)
M[i][i] = counter
return M
pprint(Exo32(11))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment