SHARE
TWEET

Untitled

a guest Sep 17th, 2011 183 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import factorial
  2. from random import shuffle
  3.  
  4. def loop(k, n, string):
  5.     if k > 0:
  6.         for i in range(n-k):
  7.             loop(k-1, n, string)
  8.             seg = string[-n:-n+k]
  9.             seg1 = list(reversed(seg[-2:]))
  10.             seg2 = seg[:-2]
  11.             shuffle(seg2)
  12.             string += seg1 + seg2
  13.         loop(k-1, n, string)
  14.  
  15. def find_string(alphabet):
  16.     n = len(alphabet)
  17.     string = list(alphabet)
  18.     loop(n-1, n, string)
  19.     return string
  20.  
  21. def check_string(string, alphabet):
  22.     n = len(alphabet)
  23.     perm_list = set()
  24.     for i in range(len(string)-n+1):
  25.         perm = string[i:i+n]
  26.         if sorted(perm) == sorted(alphabet):
  27.             perm_list.add(tuple(perm))
  28.     return len(perm_list) == factorial(n)
  29.  
  30. alphabet = [1,2,3,4,5,6]
  31. for i in range(20):
  32.     string = find_string(alphabet)
  33.     print(''.join(str(n) for n in string), check_string(string, alphabet))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top