fork download
  1. # permutations => partitions
  2.  
  3. def perm():
  4. n = 3
  5. sol = [0] * (n+1)
  6. def ok(k):
  7. for i in range(1, k):
  8. if sol[i] == sol[k]:
  9. return 0
  10. return 1
  11. def ok(k):
  12. for i in range(1, k):
  13. if sol[i] == sol[k]:
  14. return 0;
  15. return 1
  16. def display():
  17. for i in range(1, n+1):
  18. print(sol[i], end = " ")
  19. print()
  20. def bkt(k):
  21. if k == n + 1:
  22. display()
  23. else:
  24. for i in range(1,n+1):
  25. sol[k] = i;
  26. if ok(k):
  27. bkt(k+1)
  28. bkt(1)
  29. perm()
  30.  
  31. print("-"*5)
  32.  
  33. def main():
  34. n = 3
  35. sol = [0] * (n+1)
  36. def ok(k):
  37. for i in range(1, k):
  38. if sol[i] == sol[k]:
  39. return 0
  40. return 1
  41. def getMax(k):
  42. max = sol[1]
  43. for i in range(2, k):
  44. if sol[i] > max:
  45. max = sol[i]
  46. return max
  47. def display():
  48. for i in range(1, n+1):
  49. print(sol[i], end = " ")
  50. print()
  51. def bkt(k):
  52. if k == n + 1:
  53. display()
  54. else:
  55. for i in range(1,getMax(k)+2):
  56. sol[k] = i;
  57. bkt(k+1)
  58. bkt(1)
  59. main()
  60.  
Success #stdin #stdout 0.04s 9612KB
stdin
Standard input is empty
stdout
1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 1 2 
3 2 1 
-----
1 1 1 
1 1 2 
1 2 1 
1 2 2 
1 2 3