fork download
  1.  
  2. from itertools import permutations, compress
  3.  
  4. m = 3
  5. n = 3
  6.  
  7. print(
  8. "\n".join(
  9. map(
  10. #pretty print, for output readability
  11. lambda single_division: "{" +
  12. ", ".join(
  13. map(
  14. lambda single_set:
  15. "{" +
  16. ', '.join(
  17. map(
  18. str,
  19. single_set
  20. )
  21. ) +
  22. "}",
  23. single_division
  24. )
  25. ) +
  26. "}",
  27. #actual work here
  28. filter(
  29. lambda single_division: len(set(map(sum, single_division))) == 1,
  30.  
  31. {
  32. frozenset(
  33. frozenset(
  34. compress(
  35. range(1,m*n+1),
  36. map(lambda x: x == i, perm)
  37. )
  38. )
  39. for i in range(m)
  40. )
  41. for perm in set(
  42. permutations(
  43. [i for j in range(n) for i in range(m)]
  44. )
  45. )
  46. }
  47. )
  48. )
  49. )
  50. )
Success #stdin #stdout 0.21s 5744KB
stdin
Standard input is empty
stdout
{{1, 5, 9}, {8, 3, 4}, {2, 6, 7}}
{{9, 2, 4}, {3, 5, 7}, {8, 1, 6}}