fork download
  1. def func2():
  2.  
  3. def solution(level):
  4. for i in range(1, level+1):
  5. print(stack[i], end="")
  6. print()
  7.  
  8. def solve(level):
  9. if level <= n:
  10. for i in range(stack[level-1]+1,n+1):
  11. stack[level] = i
  12. solution(level)
  13. solve(level+1)
  14.  
  15. n = 3
  16. stack = [0] * (n+1)
  17. solve(1)
  18.  
  19. func2()
  20.  
  21. def func():
  22. def solve(working_set, level, n):
  23. if level == n:
  24. s = {k for k in working_set if working_set[k] == 1}
  25. solutions.append(s)
  26. else:
  27. level+=1
  28. for i in [0,1]:
  29. working_set[level] = i
  30. solve(working_set, level, n)
  31. n = 3
  32. solutions = []
  33. solve({}, 0, n)
  34. print(solutions)
  35. func()
  36.  
  37. def func3():
  38.  
  39. def init():
  40. stack[level] = -1
  41.  
  42. def succ():
  43. if stack[level] < 1:
  44. stack[level]+=1
  45. return True
  46. else:
  47. return False
  48.  
  49. def valid():
  50. return True
  51.  
  52. def sol():
  53. return level == n
  54.  
  55. def printf():
  56. for i in range(1, n+1):
  57. if stack[i] == 1:
  58. print(i, end =" ")
  59. print()
  60.  
  61.  
  62. def solve():
  63. global level
  64. level = 1
  65. init()
  66. while level > 0:
  67. h = True
  68. v = False
  69. while h and not v:
  70. h = succ()
  71. if h:
  72. v = valid()
  73. if h:
  74. if sol():
  75. printf()
  76. else:
  77. level+=1
  78. init()
  79. else:
  80. level-=1
  81.  
  82. n = 3
  83. stack = [0] * (n+1)
  84. solve()
  85. func3()
Success #stdin #stdout 0.03s 9608KB
stdin
Standard input is empty
stdout
1
12
123
13
2
23
3
[set(), {3}, {2}, {2, 3}, {1}, {1, 3}, {1, 2}, {1, 2, 3}]

3 
2 
2 3 
1 
1 3 
1 2 
1 2 3