fork download
  1. def func():
  2.  
  3. def ok(level):
  4.  
  5. return True
  6.  
  7. def print_sol():
  8.  
  9. max = stack[1]
  10.  
  11. for i in range(2, n + 1):
  12.  
  13. if max < stack[i]:
  14.  
  15. max = stack[i]
  16.  
  17. for i in range(1, max+1):
  18.  
  19. print("{", end = " ")
  20.  
  21. for j in range(1, n + 1):
  22.  
  23. if i == stack[j]:
  24.  
  25. print(j, end = " ")
  26.  
  27. print("}", end = "")
  28.  
  29. print()
  30.  
  31. def partition(level):
  32. if level > n:
  33. print_sol()
  34. else:
  35. for i in range(1, n + 1 ):
  36. if stack[level] < stack[ level - 1 ] + 1:
  37. stack[level] = i
  38. if ok(level):
  39. partition( level + 1 )
  40. n = 5
  41. stack = [0] * (n+1)
  42. partition(1)
  43.  
  44. func()
  45.  
Success #stdin #stdout 0.04s 9536KB
stdin
Standard input is empty
stdout
{ 1 2 3 4 5 }
{ 1 2 3 4 }{ 5 }
{ 1 2 3 5 }{ 4 }
{ 1 2 3 }{ 4 5 }
{ 1 2 3 }{ 4 }{ 5 }
{ 1 2 5 }{ 3 }{ 4 }
{ 1 2 }{ 3 5 }{ 4 }
{ 1 2 }{ 3 }{ 4 5 }
{ 1 2 }{ 3 }{ 4 }{ 5 }
{ 1 5 }{ 2 }{ 3 }{ 4 }
{ 1 }{ 2 5 }{ 3 }{ 4 }
{ 1 }{ 2 }{ 3 5 }{ 4 }
{ 1 }{ 2 }{ 3 }{ 4 5 }
{ 1 }{ 2 }{ 3 }{ 4 }{ 5 }