Sha256: 66d46b21da6863cf1486c7546e2d3dc4a81c9a7d09ee6f5e797c0e5834868226

Contents?: true

Size: 1.27 KB

Versions: 155

Compression:

Stored size: 1.27 KB

Contents

def board(inp):
    verify_board(inp)
    rowlen = len(inp[0])
    collen = len(inp)
    b = [list(r) for r in inp]
    for i1 in range(collen):
        for i2 in range(rowlen):
            if b[i1][i2] != ' ':
                continue
            cnt = inp[i1 - 1][i2 - 1:i2 + 2].count('*') + \
                inp[i1][i2 - 1:i2 + 2].count('*') + \
                inp[i1 + 1][i2 - 1:i2 + 2].count('*')
            if cnt == 0:
                continue
            b[i1][i2] = str(cnt)
    return ["".join(r) for r in b]


def verify_board(inp):
    # Null board or a null row
    if not inp or not all(r for r in inp):
        raise ValueError("Invalid board")
    # Rows with different lengths
    rowlen = len(inp[0])
    collen = len(inp)
    if not all(len(r) == rowlen for r in inp):
        raise ValueError("Invalid board")
    # Unknown character in board
    cset = set()
    for r in inp:
        cset.update(r)
    if cset - set('+- *|'):
        raise ValueError("Invalid board")
    # Borders not as expected
    if any(inp[i1] != '+' + '-' * (rowlen - 2) + '+'
           for i1 in [0, -1]) or any(inp[i1][i2] != '|'
                                     for i1 in range(1, collen - 1)
                                     for i2 in [0, -1]):
        raise ValueError("Invalid board")

Version data entries

155 entries across 155 versions & 1 rubygems

Version Path
trackler-2.2.1.64 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.63 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.62 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.61 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.60 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.59 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.58 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.57 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.56 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.55 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.54 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.53 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.52 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.51 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.50 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.49 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.48 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.47 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.46 tracks/python/exercises/minesweeper/example.py
trackler-2.2.1.45 tracks/python/exercises/minesweeper/example.py