Inpired by recent Stand-up Maths' video.
Task
Wrapping a list x
can be seen as inserting "line-breaks" into x
every n
-th element, or forming a matrix from x
with n
columns (feeding rows first). To be perfectly rigorous in the definition of x
wrapped into a matrix, we can pad the last row with 0
s.
Given a list x
of positive digits and a matrix M
of positive digits, determine whether x
can be wrapped into any matrix that contains M
as a submatrix. In other words, is there any n
such that wrapping x
into a matrix of n
columns results in a matrix that contains M
?
Rules
- Any reasonable input format is acceptable (list, string, array, etc.).
- As for output, please follow the defaults for decision-problem challenges.
x
is guaranteed to be longer than number of elements inM
.- You don't need to handle empty inputs.
- Some wrappings may result in the final line shorter - that's fine.
- This is code-golf, so make your code as short as possible.
Examples
x=1231231
M=23
31
Possible wrappings:
n>=7
1231231
n=6
123123
1
n=5
12312
31
n=4
1231
231
n=3
123
123
1
n=2
12
31
23
1
n=1
1
2
3
1
2
3
1
Output: True (4th wrapping)
x=1231231
M=23
32
Possible wrappings: same as above
Output: False (none of the wrappings contain M as submatrix)
Test cases
Truthy
x; M
[1,2,3,1,2,3,1]; [[2,3],[3,1]]
[3,4,5,6,7,8]; [[3,4,5],[6,7,8]]
[3,4,5,6,7,8]; [[3,4],[7,8]]
[1,2,3]; [[1,2,3]]
[1,2,3]; [[1],[2],[3]]
[1,1,2,2,3,3]; [[1],[2],[3]]
[1,1,3,4,5,6]; [[1],[4],[6]]
Falsey
x; M
[1,2,3,1,2,3,1]; [[2,3],[3,2]]
[1,1,2,2,3,3]; [[1,2,3]]
[1,2,3]; [[4,5,6]]
[1,2,3,4,5,6]; [[2,3],[4,5]]
[1,1,3,4,5,6]; [[1],[4],[6]]
. (That will not work if the code just checks that the positions of4
in[3,4]
and6
in[5,6]
are the same as the position of the first1
in[1,1]
.) \$\endgroup\$[1,2,3,4,5,6]; [[2,3],[4,5]]
. \$\endgroup\$6
and7
, and the new second row is just[7,8]
, underneath[3,4]
(it's not a full row). \$\endgroup\$ä
instead ofô
in the program I had linked in my now deleted comment.. :/ Ignore what I said. (I've actually been able to find a slightly shorter approach, and have posted my answer. And I've deleted my comment above to reduce potential confusion, since I was just blind.) \$\endgroup\$