Facebook | Phone | Battleships | where did i go wrong?
Anonymous User
4632

The question was similar to battleships on the board, but I was asked to return the coordinates
for example we have

[["X",".",".","X"],
 [".",".",".","X"],
 [".",".",".","X"]]

You may assume the following rules:

You receive a valid board, made of only battleships or empty slots.
Battleships can only be placed horizontally or vertically. In other words, they can only be made of the shape 1xN (1 row, N columns) or Nx1 (N rows, 1 column), where N can be of any size.
At least one horizontal or vertical cell separates between two battleships - there are no adjacent battleships

output should be [(0,0)] [(0,3), (1,3), (2,3)} representing 2 ships on the board

My solution

out = []
for x in range(len(board)):
    for y in range(len(board[x])):
        
        if board[x][y] == 'X':
            out.append((x,y))
return out
Comments (5)