def langford_sequence(n):
if n % 4 in (1, 2):
raise ValueError('No Langford sequence exists.')
x = (n + 3) // 4
a, b, c, d = 2 * x - 1, 4 * x - 2, 4 * x - 1, 4 * x
p, q = range(1, a, 2), range(2, a, 2)
r, s = range(a + 2, b, 2), range(a + 1, b, 2)
l = [*reversed(s), *reversed(p), b, *p, c, *s]
if n % 4 == 0:
return l + [d, *reversed(r), *reversed(q), b, a, *q, c, *r, a, d]
else:
return l + [a, *reversed(r), *reversed(q), b, a, *q, c, *r]