Airbnb questions

Board Score

AirbnbEasy

A board game is scored from a grid of tiles. Each tile is a two-character string: a capital letter for its area type, followed by a digit for the number of crowns on it, for example "G2" is a type G tile with 2 crowns.

A region is a maximal group of tiles of the same area type that are connected through shared sides (up, down, left, right; touching only at a corner does not connect). The crown digits do not matter for connecting, only the letter. The score of a region is the number of tiles in it multiplied by the total crowns on those tiles. A region with no crowns scores 0. The score of the board is the sum of the scores of all its regions.

Write board_score(board), where board is a rectangular list of rows of tile strings, and return the score of the board.

Part 1: Score the board

For example, take this 5 by 5 board:

G1 G2 W0 W1 S1
G2 G3 W0 W1 S1
S2 S3 S1 G1 S1
G1 G2 W0 W1 S1
G1 G2 W0 W1 S1

It has seven regions, scoring 32, 8, 25, 18, 1, 24 and 8, so the result is 116.

  1. Example 1
    board
    [["G1","G2","W0","W1","S1"],["G2","G3","W0","W1","S1"],["S2","S3","S1","G1","S1"],["G1","G2","W0","W1","S1"],["G1","G2","W0","W1","S1"]]
    Output
    116

    Why: The regions are: top-left G (4 cells, 8 crowns, 32), top W (4 cells, 2 crowns, 8), the S column on the right edge (5 cells, 5 crowns, 25), the row S2 S3 S1 (3 cells, 6 crowns, 18), the lone G1 (1 cell, 1 crown, 1), bottom-left G (4 cells, 6 crowns, 24) and bottom W (4 cells, 2 crowns, 8). The total is 32 + 8 + 25 + 18 + 1 + 24 + 8 = 116.

Constraints

  • 1 <= len(board) <= 100
  • 1 <= len(board[i]) <= 100
  • Every row has the same length.
  • 'A' <= board[i][j][0] <= 'Z'
  • '0' <= board[i][j][1] <= '9'
Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question
Board Score: Airbnb interview question | LoopCoach