def dirReduc(arr):
DIRS = {'NORTH': 1, 'SOUTH': -1, 'WEST': 1, 'EAST': -1}
dirs1 = ['WEST', 'EAST']
dirs2 = ['NORTH', 'SOUTH']
temparr = []
previosdirs = []
i = 0
while i <= len(arr):
if previosdirs == []:
if arr[i] in dirs1:
previosdirs = dirs1
elif arr[i] in dirs2:
previosdirs = dirs2
steps = DIRS[arr[i]]
else:
if i < len(arr) and arr[i] in previosdirs:
steps += DIRS[arr[i]]
else:
if steps > 0:
j = 0
elif steps < 0:
j = 1
temparr.extend([previosdirs[j]] * abs(steps))
if i < len(arr):
if arr[i] in dirs1:
previosdirs = dirs1
else:
previosdirs = dirs2
steps = DIRS[arr[i]]
i += 1
return temparr
оптимизируй это