This is a sequel to Ragged list pattern matching. In this challenge, the wildcard may match a sequence of items of any length instead of just a single item.
Given a pattern and a ragged list of integers, your task is to decide whether the pattern matches the ragged list.
The pattern is also represented by a ragged list. But in addition to positive integers, it may contain a wildcard value.
Here is the rule for matching:
- A positive integer matches the same positive integer.
- The wildcard value matches a sequence of items (integer or list) of any length, including the empty sequence.
- A ragged list matches a ragged list if each item in the pattern matches the corresponding item in the list.
For example, if we write the wildcard as 0
, then the pattern [0, [4, [5], 0]]
matches the ragged list [[1, 2], [3], [4, [5]]]
: here the first 0
matches the sequence [1, 2], [3]
, and the second 0
matches the empty sequence.
You may choose any fixed value as the wildcard, as long as it is consistent.
This is code-golf, so the shortest code in bytes wins.
This is decision-problem. You may use your language's convention for truthy/falsy, or use two distinct, fixed values to represent true or false.
Testcases
Here I use 0
to represent the wildcard. The input here are given in the order pattern, ragged list
.
Truthy
[], []
[0], []
[0], [1, 2]
[0], [[[]]]
[0, 0], [1, 2, 3]
[1, 0], [1, [[2, 3]]]
[1, 0, 2], [1, 2, 2, 2]
[1, 0, [2, 3]], [1, [2, 3]]
[1, [2, 0], 4], [1, [2, 3], 4]
[0, [4, [5], 0]], [[1, 2], [3], [4, [5]]]
Falsy
[1], []
[[]], []
[[0]], [3]
[[4]], [4]
[1, 0], [2, 1]
[[0]], [[1], [2]]
[1, 0, 2], [1, 2, 3]
[1, 0, 2, 0], [1, [3, 2, 3]]
[1, [0, 2], 4], [1, [2, 3], 4]
[[0], [4, [5], 0]], [[1, 2], [3], [4, [5]]]
[1, 0, 2, 0] [1, [3, 2, 3]]
\$\endgroup\$