Skip to content

Instantly share code, notes, and snippets.

@mgronhol
Last active November 2, 2015 10:02
Show Gist options
  • Save mgronhol/1b74d3a4ef4de7931016 to your computer and use it in GitHub Desktop.
Save mgronhol/1b74d3a4ef4de7931016 to your computer and use it in GitHub Desktop.
Generate sequence with unique 3-neighbourhood with no repetition
#!/usr/bin/env python
import random, itertools
def generate( symbols ):
out = ""
for s in symbols:
out += s
done = False
while not done:
found = False
partial = out[-2:]
for s in reversed(symbols):
if s not in partial:
candidate = partial + s
is_ok = True
for pc in itertools.permutations( candidate ):
spc = "".join(pc)
if spc in out:
is_ok = False
if is_ok:
out += s
found = True
break
if not found:
done = True
return out
result = generate("12345678")
print "result:", result
print "len:", len(result)
h = {}
for s in result:
if s not in h:
h[s] = 0
h[s] += 1
print h
# output:
#
# result: 123456785638746827634853745284178
# len: 33
# {'1': 2, '3': 4, '2': 3, '5': 4, '4': 5, '7': 5, '6': 4, '8': 6}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment