fork download
  1. # ========================================================================== #
  2. # These globals should be part of some library, hidden from the user
  3. # The only exposed functions are: get_position, get_world_dimensions,
  4. # move, measure, swap, and the directions (which should ideally be an enum)
  5. _grid = [[4, 5, 2, 2],
  6. [1, 3, 6, 4]]
  7. _height = len(_grid)
  8. _length = len(_grid[0])
  9. def get_world_dimensions():
  10. return (_length, _height)
  11. _x = 0
  12. _y = 0
  13. def get_position():
  14. return (_x, _y)
  15.  
  16. North = "N"
  17. South = "S"
  18. East = "E"
  19. West = "W"
  20.  
  21. # Return the value of the current cell, or the one directly in the specified direction
  22. def measure(direction=None):
  23. if direction is None:
  24. return _grid[_y][_x]
  25. # Hacky solution to get the value in that direction
  26. # (probably not how it would actually be implemented)
  27. x, y = get_position()
  28. move(direction)
  29. val = measure()
  30. move_to(x, y)
  31. return val
  32.  
  33. # Move one cell in the specified direction
  34. def move(direction):
  35. global _x, _y
  36. if direction == North:
  37. _y = (_y + 1) % _height
  38. elif direction == South:
  39. _y = (_y - 1) % _height
  40. elif direction == East:
  41. _x = (_x + 1) % _length
  42. elif direction == West:
  43. _x = (_x - 1) % _length
  44. else:
  45. raise ValueError(f"{direction} is not a valid direction to move in")
  46.  
  47. # Move the values of the current cell and the one in the specified direction
  48. # But not wrapping around the edges
  49. def swap(direction):
  50. # also kinda hacky, but this is not the point
  51. if direction == North and _y + 1 < _height:
  52. _grid[_y][_x], _grid[_y + 1][_x] = _grid[_y + 1][_x], _grid[_y][_x]
  53. elif direction == South and _y > 0:
  54. _grid[_y][_x], _grid[_y - 1][_x] = _grid[_y - 1][_x], _grid[_y][_x]
  55. elif direction == East and _x + 1 < _length:
  56. _grid[_y][_x], _grid[_y][_x + 1] = _grid[_y][_x + 1], _grid[_y][_x]
  57. elif direction == West and _x > 0:
  58. _grid[_y][_x], _grid[_y][_x - 1] = _grid[_y][_x - 1], _grid[_y][_x]
  59. else:
  60. raise ValueError(f"{direction} is not a valid direction to swap in from position {(_x, _y)}")
  61.  
  62.  
  63.  
  64. # ========================================================================== #
  65.  
  66. # Not a part of the API, but convenient for me here
  67. # The user could make something with the same effect, through get_position() and move()
  68. def move_to(x, y):
  69. global _x, _y
  70. _x = x
  71. _y = y
  72.  
  73. # ========================================================================== #
  74. # My solution
  75.  
  76. # assumes initial position is (0,y) for some y, then this sorts row number y
  77. # and returns to (0, y)
  78. def sort_row():
  79. m, n = get_world_dimensions()
  80. move(East)
  81. for i in range(1, m):
  82. val = measure()
  83. k = i
  84. while k > 0 and measure(West) > val:
  85. swap(West)
  86. move(West)
  87. k -= 1
  88. for j in range(k, i+1):
  89. move(East)
  90.  
  91. # assumes initial position is (0, y), sorts all rows, and returns to (0, y)
  92. def sort_all_rows():
  93. m, n = get_world_dimensions()
  94. for i in range(n):
  95. sort_row()
  96. move(North)
  97.  
  98. # assumes initial position is (x, 0) for some x, then this sorts column number x
  99. # and returns to (x, 0)
  100. def sort_col():
  101. m, n = get_world_dimensions()
  102. move(North)
  103. for i in range(1, n):
  104. val = measure()
  105. k = i
  106. while k > 0 and measure(South) > val:
  107. swap(South)
  108. move(South)
  109. k -= 1
  110. for j in range(k, i+1):
  111. move(North)
  112.  
  113. # assumes initial position is (x, 0), sorts all columns, and returns to (x, 0)
  114. def sort_all_cols():
  115. m, n = get_world_dimensions()
  116. for i in range(m):
  117. sort_col()
  118. move(East)
  119.  
  120. def sort_rows_then_cols():
  121. move_to(0, 0)
  122. sort_all_rows()
  123. sort_all_cols()
  124.  
  125.  
  126. print("Before")
  127. print(_grid)
  128.  
  129. sort_rows_then_cols()
  130.  
  131. print("After")
  132. print(_grid)
Success #stdin #stdout 0.03s 9560KB
stdin
Standard input is empty
stdout
Before
[[4, 5, 2, 2], [1, 3, 6, 4]]
After
[[1, 2, 4, 5], [2, 3, 4, 6]]