Guest User

Untitled

a guest
Nov 23rd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. from collections import namedtuple
  2. from itertools import product
  3.  
  4. Cell = namedtuple('Cell', ['x', 'y'], verbose=True)
  5.  
  6. def get_neighbors(coordinate):
  7. row = ord(coordinate[0])
  8. col = int(coordinate[1:])
  9.  
  10. coordinates = [Cell(x, y) for x, y in product(range(-1, 2), range(-1, 2))]
  11.  
  12. coordinates = [cell for cell in coordinates if cell != Cell(0, 0)]
  13.  
  14. coordinates = [Cell(row + cell.x, col + cell.y) for cell in coordinates]
  15.  
  16. coordinates = [cell for cell in coordinates if ord('A') <= cell.x <= ord('Y')]
  17. coordinates = [cell for cell in coordinates if 0 <= cell.y <= 24]
  18.  
  19. return [f"{chr(cell.x)}{cell.y}" for cell in coordinates]
Add Comment
Please, Sign In to add comment