fork(4) download
  1.  
  2. from itertools import product
  3.  
  4. Alphabet = [1, 2, 3, 4]
  5. Forbidden = set([(1,3),(3,1),(2,4),(4,2)])
  6.  
  7. def valid(p, discard):
  8. p_L = list(p)
  9. p_shift = set(zip(p_L, p_L[1:]+p_L[:1]))
  10. return len(p_shift & discard) == 0
  11.  
  12. def L_n(n):
  13. if n < 0:
  14. return []
  15. else:
  16. return [ list(p) for p in product(Alphabet, repeat=n)
  17. if valid(p, Forbidden) ]
  18.  
  19. for item in L_n(3): print(item, end='\t')
  20. print()
  21.  
  22.  
Success #stdin #stdout 0.02s 9944KB
stdin
Standard input is empty
stdout
[1, 1, 1]	[1, 1, 2]	[1, 1, 4]	[1, 2, 1]	[1, 2, 2]	[1, 4, 1]	[1, 4, 4]	[2, 1, 1]	[2, 1, 2]	[2, 2, 1]	[2, 2, 2]	[2, 2, 3]	[2, 3, 2]	[2, 3, 3]	[3, 2, 2]	[3, 2, 3]	[3, 3, 2]	[3, 3, 3]	[3, 3, 4]	[3, 4, 3]	[3, 4, 4]	[4, 1, 1]	[4, 1, 4]	[4, 3, 3]	[4, 3, 4]	[4, 4, 1]	[4, 4, 3]	[4, 4, 4]