SHARE
TWEET

Untitled

a guest Sep 17th, 2011 6,196 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from math import factorial
  2.  
  3. def loop(k, n, string):
  4.     if k > 0:
  5.         for i in range(n-k):
  6.             loop(k-1, n, string)
  7.             string += reversed(string[-n:-n+k])
  8.         loop(k-1, n, string)
  9.  
  10. def find_string(alphabet):
  11.     n = len(alphabet)
  12.     string = list(alphabet)
  13.     loop(n-1, n, string)
  14.     return string
  15.  
  16. def check_string(string, alphabet):
  17.     n = len(alphabet)
  18.     perm_list = set()
  19.     for i in range(len(string)-n+1):
  20.         perm = string[i:i+n]
  21.         if sorted(perm) == sorted(alphabet):
  22.             perm_list.add(tuple(perm))
  23.     return len(perm_list) == factorial(n)
  24.  
  25. alphabet = [1,2,3]
  26. string = find_string(alphabet)
  27. print(string)
  28. print(check_string(string, alphabet))
  29. print(check_string([1, 2, 3, 1, 2, 1, 3, 2, 2], 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