Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Nonogram Encode by mortonfox
from re import findall
def nonogram_encode(data: list[str]) -> list:
count_blocks = lambda cells: [len(blk) for blk in findall(r'X+', cells)]
cols = [count_blocks(''.join(col)) for col in zip(*data)]
max_len = max(map(len, cols))
cols = [list(col) for col in zip(*[[0] * (max_len - len(col)) + col for col in cols])]
rows = [count_blocks(row) for row in data]
max_len = max(map(len, rows))
rows = [[0] * (max_len - len(row)) + row for row in rows]
return [cols, rows]
print("Example:")
print(nonogram_encode([" X X ", "X X X", " X X "]))
assert nonogram_encode([" X X ", "X X X", " X X "]) == [
[[0, 1, 0, 1, 0], [1, 1, 1, 1, 1]],
[[0, 1, 1], [1, 1, 1], [0, 1, 1]],
]
assert nonogram_encode(["X"]) == [[[1]], [[1]]]
print("The mission is done! Click 'Check Solution' to earn rewards!")
Sept. 9, 2022