fork download
  1. # Assumptions:
  2. # 1) Each row has the same number of columns (the input is always rectangular; a square is a special case).
  3. # 2) The input is a 2D matrix; single-row or single-column matrices are allowed.
  4.  
  5. # Note: Multiple test cases run at the end of the file.
  6.  
  7. def clockwiseMatrix(input):
  8. output = []
  9. # Go over top row
  10. while len(input) > 0 and len(input[0]) > 0:
  11. # iterating over top row
  12. top_row_arr = input.pop(0)
  13. output.extend(top_row_arr)
  14.  
  15. # in case list all items in array were iterated on, end loop
  16. if len(input) == 0 or len(input[0]) == 0:
  17. break
  18.  
  19. # iterating over last column reversed
  20. for row in input:
  21. last_col = row.pop(-1)
  22. output.append(last_col)
  23.  
  24. # in case list all items in array were iterated on, end loop
  25. if len(input) == 0 or len(input[0]) == 0:
  26. break
  27.  
  28. # iterating over bottom row
  29. botton_row_arr = input.pop(-1)
  30. botton_row_arr = botton_row_arr[::-1]
  31. output.extend(botton_row_arr)
  32.  
  33. # in case list all items in array were iterated on, end loop
  34. if len(input) == 0 or len(input[0]) == 0:
  35. break
  36.  
  37. # iterating over first column reversed
  38. for row_idx in range(len(input) -1, -1, -1):
  39. first_col = input[row_idx].pop(0)
  40. output.append(first_col)
  41.  
  42. # in case list all items in array were iterated on, end loop
  43. if len(input) == 0 or len(input[0]) == 0:
  44. break
  45.  
  46. return output
  47.  
  48. test_1 = [[2, 3, 4, 8],
  49. [5, 7, 9, 12],
  50. [1, 0, 6, 10]]
  51.  
  52. test_2 = [[1, 2, 3, 4, 5],
  53. [14, 15, 16, 17, 6],
  54. [13, 20, 19, 18, 7],
  55. [12, 11, 10, 9, 8],
  56. ]
  57.  
  58. test_3 = [[1, 2, 3, 4, 5],
  59. [16, 17, 18, 19, 6],
  60. [15, 24, 25, 20, 7],
  61. [14, 23, 22, 21, 8],
  62. [13, 12, 11, 10, 9],
  63. ]
  64.  
  65. test_4 = [[1, 2, 3, 4, 5]]
  66.  
  67. test_5 = [[1],
  68. [2],
  69. [3],
  70. [4],
  71. [5],
  72. [6],
  73. [7]]
  74.  
  75.  
  76. test_8 = [['g', 'r', 'e', 'a', 't'],
  77. ['s', 's', '!', '!', ' '],
  78. ['e', 'c', 'c', 'u', 's']
  79. ]
  80.  
  81. print('test_1 (example provided) -', clockwiseMatrix(test_1)) # Expected [2, 3, 4, 8, 12, 10, 6, 0, 1, 5, 7, 9]
  82. print('test_2 (rectangle) -', clockwiseMatrix(test_2)) # Expected [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
  83. print('test_3 (Big Square) -', clockwiseMatrix(test_3)) # Expected [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
  84. print('test_4 (single row) -', clockwiseMatrix(test_4)) # Expected [1, 2, 3, 4, 5]
  85. print('test_5 (single column) -', clockwiseMatrix(test_5)) # Expected [1, 2, 3, 4, 5, 6, 7]
  86. print('test_6 (single emptry_arr) -', clockwiseMatrix([])) # Expected []
  87. print('test_7 (single emptry_arrays) -', clockwiseMatrix([[],[]])) # Expected []
  88. print('test_8 (non int values) -', clockwiseMatrix(test_8)) # Expected []
Success #stdin #stdout 0.11s 14004KB
stdin
Standard input is empty
stdout
test_1 (example provided) - [2, 3, 4, 8, 12, 10, 6, 0, 1, 5, 7, 9]
test_2 (rectangle) - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
test_3 (Big Square) - [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
test_4 (single row) - [1, 2, 3, 4, 5]
test_5 (single column) - [1, 2, 3, 4, 5, 6, 7]
test_6 (single emptry_arr) - []
test_7 (single emptry_arrays) - []
test_8 (non int values) - ['g', 'r', 'e', 'a', 't', ' ', 's', 'u', 'c', 'c', 'e', 's', 's', '!', '!']