0

So the issue is that towards the end, the squares.remove line is working but above that, the squares.pop(i) isn't working. As you can see in the output, the tuples with -1 or 0 are still there.

Now I have tried this with the remove function as well. But nothing seems to be working.

Also, I have checked the if condition and that seems to be working.

Furthermore to check whether the of statement actually wa running or not, I had added a print("running") statement inside the if block above the pop statement, and the "running" was being printed.

Any help is greatly appreciated because I am completely baffled.

Code:

import itertools
def chessKnight(cell):
  cell = list(cell)
  cell[0] = ord(cell[0]) - 96
  cell[1] = int(cell[1])
  squares = list(itertools.product(range(cell[0]-2, cell[0]+3),range(cell[1]-2, cell[1]+3)))
    
  for i,square in enumerate(squares):
    if square[0] in [0, -1] or square[1] in [0, -1]:
      squares.pop(i)
    if list(square) == cell:
      squares.remove(square)

  print(squares)

chessKnight("a1")

Output:

[(-1, 0), (-1, 2), (0, -1), (0, 1), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 2), (2, 3), (3, 0), (3, 1), (3, 2), (3, 3)]
Arnav Mangla
  • 62
  • 1
  • 7

0 Answers0