fork download
  1. # your code goes here
  2.  
  3. examples = [
  4. [
  5. [ 0, 1, 2, 3 ],
  6. [ 4, 5, 6, 7 ],
  7. [ 8, 9, 10, 11 ],
  8. [ 12, 13, 14, 15 ],
  9. ],
  10. [
  11. [ 1, 2, 3 ],
  12. [ 4, 5, 6 ],
  13. [ 7, 8, 9 ],
  14. ],
  15. [ [ 123, 321, 111, 222 ] ],
  16. [
  17. [ 1, 2, 3, 4 ],
  18. [ 2, 3, 4, 5 ],
  19. [ 3, 4, 5, 6 ],
  20. ]
  21. ]
  22.  
  23. def print_matrix(matrix):
  24. for row in matrix:
  25. print(' '.join(f'{value:-4}' for value in row))
  26.  
  27. def rotate_matrix(matrix):
  28. return list(map(list, map(reversed, zip(*matrix))))
  29.  
  30. for matrix in examples:
  31. print("before:")
  32. print_matrix(matrix)
  33. print("after:")
  34. print_matrix(rotate_matrix(matrix))
  35. print()
  36.  
Success #stdin #stdout 0.02s 9172KB
stdin
Standard input is empty
stdout
before:
   0    1    2    3
   4    5    6    7
   8    9   10   11
  12   13   14   15
after:
  12    8    4    0
  13    9    5    1
  14   10    6    2
  15   11    7    3

before:
   1    2    3
   4    5    6
   7    8    9
after:
   7    4    1
   8    5    2
   9    6    3

before:
 123  321  111  222
after:
 123
 321
 111
 222

before:
   1    2    3    4
   2    3    4    5
   3    4    5    6
after:
   3    2    1
   4    3    2
   5    4    3
   6    5    4