All problems
Count Battleships
Problem
Count the battleships on a board of X and dot characters, where a ship is a maximal horizontal or vertical run of X and ships never touch each other.
Count only the topmost or leftmost cell of each ship, detected by having no X above and no X to the left. Return 0 for an empty board.
Hint
Each ship has exactly one cell with no ship part above or to its left.
Test cases
case 1
in: [[["X",".",".","X"],[".",".",".","X"],[".",".",".","X"]]]
expected: 2
case 2
in: [[["."]]]
expected: 0
case 3
in: [[["X"]]]
expected: 1
case 4
in: [[["X","X"],[".","X"]]]
expected: 1
case 5
in: [[]]
expected: 0
Starter codePython
def count_battleships(board):
# Your code here
pass5 test cases · run your code against them in the editor