Inpired by recent Stand-up Maths' video.
Wrapping a list
x can be seen as inserting "line-breaks" into
n-th element, or forming a matrix from
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
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
- Any reasonable input format is acceptable (list, string, array, etc.).
- As for output, please follow the defaults for decision-problem challenges.
xis guaranteed to be longer than number of elements in
- 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.
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)
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,1,2,2,3,3]; [,,] [1,1,3,4,5,6]; [,,]
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]; [,,]. (That will not work if the code just checks that the positions of
[5,6]are the same as the position of the first
[1,2,3,4,5,6]; [[2,3],[4,5]]. \$\endgroup\$
7, and the new second row is just
[3,4](it's not a full row). \$\endgroup\$
ô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\$