12
\$\begingroup\$

Let's say we have a 2-D list, like the following one:

\$ \begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} & \color{red}{4} \\ \color{blue}{5} & \color{blue}{6} & \color{blue}{7} & \color{red}{8} \\ \color{green}{9} & \color{green}{10} & \color{blue}{11} & \color{red}{12} \end{bmatrix} \$

Notice how the top row and the right column is red. For the purposes of this question, let's call this a J-bracket. The 2nd J-bracket is highlighted in blue, and the 3rd J-bracket is highlighted in green. Your challenge is not to find the J-brackets, but given the J-bracket list, you need to return the original 2-D list.

In many cases, there will be more than one 2-D list possible based on the J-brackets. In this case, you can return either of the possible 2-D lists, or you can return a list of the possiblities.

Let's say you are given the list [[1,2,3,4,5,6], [1,2,3,4], [1,2]]. This means the 1st J-bracket is [1,2,3,4,5,6], the 2nd J-bracket is [1,2,3,4], and the 3rd one is [1,2]. There are two different possible matrixes that can be created from these J-brackets:

\$ \begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} & \color{red}{4} \\ \color{blue}{1} & \color{blue}{2} & \color{blue}{3} & \color{red}{5} \\ \color{green}{1} & \color{green}{2} & \color{blue}{4} & \color{red}{6} \end{bmatrix} \$

OR:

\$ \begin{bmatrix} \color{red}{1} & \color{red}{2} & \color{red}{3} \\ \color{blue}{1} & \color{blue}{2} & \color{red}{4} \\ \color{green}{1} & \color{blue}{3} & \color{red}{5} \\ \color{green}{2} & \color{blue}{4} & \color{red}{6} \end{bmatrix} \$

Test cases

[[1,2,3,4,5], [1,2,3], [1]] => [[1,2,3], [1,2,4], [1,3,5]]
[[1,2,3,4,5,6], [1,2,3,4], [1,2]] => [[1,2,3], [1,2,4], [1,3,5], [2,4,6]]
                                  OR [[1,2,3,4], [1,2,3,5], [1,2,4,6]]
[[6,8,9,6,5,8,2],[1,7,4,3,2],[2,3,1]] => [[6,8,9,6,5], [1,7,4,3,8], [2,3,1,2,2]]
                                      OR [[6,8,9], [1,7,6], [2,4,5], [3,3,8], [1,2,2]]
[] => []

Rules

✝ The name J-bracket was robbed from this question.

\$\endgroup\$
1
  • \$\begingroup\$ Sandbox \$\endgroup\$
    – naffetS
    Apr 18, 2022 at 2:54

10 Answers 10

7
\$\begingroup\$

APL(Dyalog Unicode), 20 bytes SBCS

{(↓∘⍺⍪⍵,∘⍪↑∘⍺)-≢⍵}/⊆

Try it on APLgolf!

Nest if simple (if the input is []). This is necessary to deal with the empty case.
/ Reduce the list of J-brackets from right to left with:
{ ... } Takes the current matrix as ⍵ and the new J-bracket as ⍺.
-≢⍵ Number of rows, negated
( ... ) Call the tacit function with this as a right argument.
↑∘⍺ Take that many values from ⍺ (taking a negative amount gets values from the end)
⍵,∘⍪ Append to ⍵ as a column.
↓∘⍺⍪ Drop -≢⍵ values from ⍺, prepend that as a row.  

\$\endgroup\$
6
\$\begingroup\$

J, 33 26 bytes

((}.~-@#),],.({.~-@#))&.>/

Try it online!

A single reduction, where each iteration adds another "J layer". Consider left and right parts like 1 2 3 4 (...) 8 9:

  • ({.~-@#) From the tail of the left list take as many elements as are in the right list:

    3 4
    
  • ],. Zip with right list

    8 3
    9 4
    
  • (}.~-@#), Append the remaining front elements of the left list:

    1 2
    8 3
    9 4
    
\$\endgroup\$
5
\$\begingroup\$

Curry (PAKCS), 60 bytes

f[]=[]
f((a++b):c)=a:f c!b
[]![]=[]
(a:b)!(c:d)=(a++[c]):b!d

Try it online!

\$\endgroup\$
5
\$\begingroup\$

K (ngn/k), 41 40 29 27 26 bytes

{((,i#y),x),'(i:#*x)_y}/|:

Try it online!

Simply aping ovs' APL solution.

\$\endgroup\$
5
  • \$\begingroup\$ Removed a stray byte \$\endgroup\$
    – doug
    Apr 18, 2022 at 13:43
  • \$\begingroup\$ @coltim points out that I can simply recurse on the reversed argument. \$\endgroup\$
    – doug
    Apr 18, 2022 at 14:01
  • \$\begingroup\$ @coltim Found two more. (He's pretty good at this..) \$\endgroup\$
    – doug
    Apr 18, 2022 at 14:06
  • \$\begingroup\$ Stray byte leaked back in. Thanks @PyGamer0. \$\endgroup\$
    – doug
    Apr 18, 2022 at 14:11
  • 2
    \$\begingroup\$ The convention here for giving credit for help is, instead of using comments, to update the original post and add something like "-15 bytes thanks to Coltim" right under the TIO link. \$\endgroup\$
    – Jonah
    Apr 18, 2022 at 15:16
2
\$\begingroup\$

PARI/GP, 60 bytes

a->matrix(if(w=#a,#a[1]+1-w),w,i,j,a[x=min(w-j+1,i)][i+j-x])

Attempt This Online!

Returns the one with height ≥ width, e.g. [[1,2,3,4,5,6], [1,2,3,4], [1,2]] => [[1,2,3], [1,2,4], [1,3,5], [2,4,6]].


PARI/GP, 62 bytes

a->matrix(#a,w=if(#a,#a[1]+1-#a),i,j,a[x=min(w-j+1,i)][i+j-x])

Attempt This Online!

Returns the one with width ≥ height, e.g. [[1,2,3,4,5,6], [1,2,3,4], [1,2]] => [[1,2,3,4], [1,2,3,5], [1,2,4,6]].


In both case, the formula is \$output[i,j]=input[i+\min(w-i-j+1,0),j-\min(w-i-j+1,0)]\$, where \$w\$ is the width of the output matrix.

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 17 bytes

⮌A«Fυ⊞κ⊟ι⊞υι»I⮌υ

Try it online! Link is to verbose version of code. Outputs in Charcoal's default one-element-per-line format. Explanation: Based on my original answer to Find the J twin which has since been superseded by an alternative approach.

⮌A«

Loop over the J-brackets in reverse order.

Fυ⊞κ⊟ι

Append the end elements of the bracket to the existing rows of the predefined empty array.

⊞υι

Append the remainder of the bracket to the predefined empty array.

»I⮌υ

Output the reverse of the final array.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (ES6), 69 bytes

A reduceRight() with a recursive callback function.

a=>a.reduceRight(g=(p,c,i)=>i--?g(p,[...c,a[i].pop()],i):[c,...p],[])

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python, 69 bytes

f=lambda L:L and L[-1:1]or[*zip(*zip(L[0],*f(L[1:])),L[0][-len(L):])]

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 14 bytes

ȯWṚUṭḢ;¥Ɱ¥@/ṚU

Try it online! (The TIO comes with a footer that prints the Python repr, to ensure empty arrays are visible; but the bare link works as a function.)

This is very likely to be suboptimal, since I do four (!) reversals of arrays, but I couldn't find a better solution with Jelly's functions. There could also be a shorter way to handle [] than ȯW, and I might be able to drop the @. However, it feels like this challenge really happened to hit some weak points in Jelly, as the core of the solutions is tiny.

  • The first (and the @) gives us a right-associative reduce. There's no single-byte right-associative reduce in Jelly.
  • The @ also allows us to use / to pop stuff from the new J-bracket, since we can only apply monads to the left arg of dyadic chains.
  • The last reverses the order of the output lines. Since things popped from the J-bracket's end go to the bottom, we have build the output backwards. Also, unfortunately, there's no prepend to like , so we have to append the "rest" of the J-bracket to the bottom of the matrix (; would require a W).
  • The U's reverse each input and output line. ȯWṚṭṪṭ¥Ɱ¥@/Ṛ is very close to correct, but unfortunately gets the innermost J-bracket in the wrong order. I'd need to U or W it somehow to make the output correct.
ȯW                ȯr Wrap (replace [] with [[]])
  Ṛ               Ṛeverse order of J-brackets
   U              Upend (reverse) each J-bracket
           /      reduce lines with:
          @         (swap args: left=next J-bracket, right=matrix)
        Ɱ           Ɱap over lines of matrix:
     Ḣ                remove Ḣead (first) of J-bracket
      ;               append that to matrix line
    ṭ               ṭack (append) remaining J-bracket to matrix
            Ṛ     Ṛeverse order of lines
             U    Upend (reverse) each line
\$\endgroup\$
1
\$\begingroup\$

R, 84 83 bytes

function(j){m[]=unsplit(j,pmin(row(m<-matrix(unlist(j),length(j))),rev(col(m))))
m}

Try it online!

The unsplit strategy corresponding to this answer.

-1 thanks to pajonk.

\$\endgroup\$
2
  • \$\begingroup\$ I was waiting for you to post it :) BTW, -1 byte \$\endgroup\$
    – pajonk
    Apr 19, 2022 at 8:54
  • \$\begingroup\$ @pajonk you shouldn't wait. Also this is annoyingly long; perhaps another approach is warranted. \$\endgroup\$
    – Giuseppe
    Apr 19, 2022 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.