Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
zip and split solution in Clear category for Nonogram Encode by kdim
def nonogram_encode(data: list[str]) -> list:
rows = [[len(n) for n in row.strip().split()] for row in data] # get a numbers of 'Х'
maxlen = len(max(rows, key=len)) # get a max len of list
rows_clue = [[0] * (maxlen - len(n)) + n for n in rows] # add zeros, align a list
cols = [[len(n) for n in ''.join(col).strip().split()] for col in zip(*data)] # similarly for columns
maxlen = len(max(cols, key=len))
cols_clue = [list(i) for i in zip(*[[0] * (maxlen - len(n)) + n for n in cols])]
return [cols_clue, rows_clue]
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. 24, 2022
Comments: