Skip to content

Instantly share code, notes, and snippets.

@wsphillips
Created December 12, 2019 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wsphillips/c70681b4d57bedd67f7ec1808a0a4c2c to your computer and use it in GitHub Desktop.
Save wsphillips/c70681b4d57bedd67f7ec1808a0a4c2c to your computer and use it in GitHub Desktop.
Pytographic ordering algorithm
using Test
# Reference pattern for N = 13; should be able to generalize pattern to any int N
index1=[
1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6,7,8,9,10,11,12,13,13,13,13,13,13,13,13,13,
13,13,13,13,12,11,10,9,8,7,6,5,4,3,2,2,2,2,2,2,2,2,2,2,2,2,3,4,5,6,7,8,9,10,11,
12,12,12,12,12,12,12,12,12,12,12,11,10,9,8,7,6,5,4,3,3,3,3,3,3,3,3,3,3,4,5,6,7,
8,9,10,11,11,11,11,11,11,11,11,11,10,9,8,7,6,5,4,4,4,4,4,4,4,4,5,6,7,8,9,10,10,
10,10,10,10,10,9,8,7,6,5,5,5,5,5,5,6,7,8,9,9,9,9,9,8,7,6,6,6,6,7,8,8,8,7,7]
function pattern(N::Int64)
nrng::Vector{Int64} = 1:N
idx1::Vector{Int64} = ones(Int64, N^2)
stop::Int64 = 0
forward = true
for i in (N-1):-1:0
# Repeating integers
start = stop + 1
stop = start + i - 1
idx1[start:stop] .= forward ? nrng[1] : nrng[end]
# Incrementing/decrementing integers
start = stop + 1
stop = start + i
idx1[start:stop] = forward ? nrng : reverse(nrng)
nrng = forward ? nrng[2:end] : nrng[1:end-1]
forward = !forward
end
return idx1
end
@test pattern(13) == index1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment