import numpy as np
from itertools import zip_longest
lst = [[1, 2, 3], [4, 5, 6, 7, 8], [9, 10, 11]]
def neighbors(a, b, m=lst, r=1):
m = np.array(list(zip_longest(*m)))#.T
v = np.argwhere(m == b) - np.argwhere(m == a)
return np.abs(v).max() <= r
assert neighbors(1, 2) == True
assert neighbors(1, 6) == False
assert neighbors(5, 6) == True
assert neighbors(3, 6) == True
assert neighbors(7, 11) == True
assert neighbors(1, 9) == False
assert neighbors(7, 8) == True
def func(arr, a, b):
for elem1 in range(len(arr)):
for elem2 in range(len(arr[elem1])):
if arr[elem1][elem2] == a:
onePositionA = arr.index(arr[elem1])
twoPositionA = arr[elem1].index(arr[elem1][elem2])
elif arr[elem1][elem2] == b:
onePositionB = arr.index(arr[elem1])
twoPositionB = arr[elem1].index(arr[elem1][elem2])
if abs(onePositionA - onePositionB) <= 1 and abs(twoPositionA - twoPositionB) <= 1:
return True
else:
return False