Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

99 lines
2.5 KiB
Markdown

---
id: 6a26df95efa55a2524399743
title: "Challenge 362: Nonogram Validator"
challengeType: 29
dashedName: challenge-362
---
# --description--
Given an array of clue numbers and an array of cells, determine whether the cells satisfy the nonogram clue.
- The clue is an array of numbers representing the lengths of consecutive filled cells, in order. For example, a clue of `[3, 2]` means there should be 3 consecutive filled cells followed by 2 consecutive filled cells, separated by at least one empty cell.
- The row is an array of 1s (filled) and 0s (empty).
# --hints--
`is_valid_nonogram([3, 2], [1, 1, 1, 0, 1, 1])` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_nonogram([3, 2], [1, 1, 1, 0, 1, 1]), True)`)
}})
```
`is_valid_nonogram([3, 2], [0, 1, 1, 1, 1, 1])` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_nonogram([3, 2], [0, 1, 1, 1, 1, 1]), False)`)
}})
```
`is_valid_nonogram([1, 1, 1, 1], [1, 0, 1, 0, 1, 0, 1, 0, 1])` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_nonogram([1, 1, 1, 1], [1, 0, 1, 0, 1, 0, 1, 0, 1]), False)`)
}})
```
`is_valid_nonogram([1, 1, 1, 1], [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0])` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_nonogram([1, 1, 1, 1], [0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0]), True)`)
}})
```
`is_valid_nonogram([3, 2, 3], [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0])` should return `True`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_nonogram([3, 2, 3], [0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0]), True)`)
}})
```
`is_valid_nonogram([3, 2, 3], [0, 0, 0, 1, 0, 0, 1, 0, 0, 0])` should return `False`.
```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertIs(is_valid_nonogram([3, 2, 3], [0, 0, 0, 1, 0, 0, 1, 0, 0, 0]), False)`)
}})
```
# --seed--
## --seed-contents--
```py
def is_valid_nonogram(clue, cells):
return clue
```
# --solutions--
```py
def is_valid_nonogram(clue, cells):
runs = []
count = 0
for i in range(len(cells) + 1):
if i < len(cells) and cells[i] == 1:
count += 1
elif count > 0:
runs.append(count)
count = 0
if len(runs) != len(clue):
return False
return all(runs[i] == clue[i] for i in range(len(runs)))
```