I have just started my Python adventure a few days ago.
Recently, I wrote a simple boggle solver and it works well. However, I find this particular block of code rather clunky. Is there a better (i.e. more pythonic) way of writing it?
The purpose of the code is to return all neighbours of (x,y), on 4x4 board. So, either 8 (middle), 5 (edge) or 3 (corner) neighbours.
- Code: Select all
def get_neighbours(x, y):
offsets = ([-1,-1], [-1,0], [-1,1], [0,-1], [0,1], [1,-1], [1,0], [1,1])
ret_val = list()
for o in offsets:
if x + o >= 0 and y + o >= 0 and x + o <=3 and y + o <= 3:
ret_val.append([x + o, y + o])