fork download
  1. def Lex(num,x,y,z=''):
  2. """num = string length, x and y = placeholder for some char, z = pattern list """
  3. list = []
  4. palindromes = []
  5. pals = []
  6.  
  7. patterns = []
  8.  
  9. c = "%0" + str(num) +"d"
  10. for i in range(0, 2 ** num):
  11. list.append(str(c % int(bin(i)[2:])))
  12. for i in range(0,len(list)):
  13. list[i] = list[i].replace("0",x).replace("1",y)
  14. print str("%02d" % int(i+1)) + '. ' + list[i]
  15. if (list[i]==list[i][::-1]):
  16. palindromes.append(i+1)
  17. pals.append(list[i])
  18.  
  19. if len(z)>0:
  20. for n in range(0, len(z)):
  21. if list[i][n] == z[n]:
  22. patterns.append(list[i])
  23.  
  24. if len(palindromes)>0:
  25. print "\nPalindromes: " + str(palindromes) + "\n----\n\n"
  26. return pals,patterns
  27.  
  28. def LexUpTo(l,op,x,y,z=''):
  29. """print lex of length range"""
  30. output = []
  31. output2 = []
  32. if op == "<":
  33. for i in range(1, l):
  34. tmp = Lex(i,x,y,z)
  35. output += tmp[0]
  36. output2 += tmp[1]
  37.  
  38.  
  39. print "Sorted palindromes:\n"
  40. print sorted(output)
  41.  
  42. print "\n\nPatterns sorted by alpha, len:\n"
  43. print sorted(output2)
  44. print sorted(output2, key=len)
  45.  
  46. Lex(5,'x','y',['x'])
Success #stdin #stdout 0.08s 10840KB
stdin
Standard input is empty
stdout
01. xxxxx
02. xxxxy
03. xxxyx
04. xxxyy
05. xxyxx
06. xxyxy
07. xxyyx
08. xxyyy
09. xyxxx
10. xyxxy
11. xyxyx
12. xyxyy
13. xyyxx
14. xyyxy
15. xyyyx
16. xyyyy
17. yxxxx
18. yxxxy
19. yxxyx
20. yxxyy
21. yxyxx
22. yxyxy
23. yxyyx
24. yxyyy
25. yyxxx
26. yyxxy
27. yyxyx
28. yyxyy
29. yyyxx
30. yyyxy
31. yyyyx
32. yyyyy

Palindromes: [1, 5, 11, 15, 18, 22, 28, 32]
----