Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Take a not nested array in as input. Turn it into a matrix by:

Let's say my array is [1, 2, 3, 4, 5]

First, I repeat that array 5 times: (the length)

[[1, 2, 3, 4, 5],
 [1, 2, 3, 4, 5],
 [1, 2, 3, 4, 5],
 [1, 2, 3, 4, 5],
 [1, 2, 3, 4, 5]]

Then, I read it along the diagonals:

[[1],
 [2, 1],
 [3, 2, 1],
 [4, 3, 2, 1],
 [5, 4, 3, 2, 1],
 [5, 4, 3, 2],
 [5, 4, 3],
 [5, 4],
 [5]]

I flatten this array and split it into pieces of five (the length):

[[1, 2, 1, 3, 2],
 [1, 4, 3, 2, 1],
 [5, 4, 3, 2, 1],
 [5, 4, 3, 2, 5],
 [4, 3, 5, 4, 5]]

This is code golf. Fewest bytes wins.

share|improve this question
    
Next time, please CAPITALIZE things. – Oliver 4 hours ago
    
How does this work if the original array has a length other than 5? – ais523 4 hours ago
    
@ais523 I'm assumming its the same thing, you just replace 'five' with the length – Oliver 4 hours ago
    
Can we assume the numbers always be positive integers? – Luis Mendo 4 hours ago
6  
@JohnCena You shouldn't accept the first answer, you need to give the post some time to gain traction and some more answers. – Kade 4 hours ago
up vote 0 down vote accepted

05AB1E, 13 bytes

.p¹.sR¦«í˜¹gä

Try it online!

Explanation:

                # Implicit input
 .p             # Get prefixes
   ¹            # Get first input
    .s          # Get suffixes
      R         # Reverse
       ¦        # Remove first element
        «       # Concatenate
         í      # Reverse every one
          ˜     # Flatten
           ¹gä  # Split into pieces of the length
                # Implicit print
share|improve this answer
    
don't you need to print it – user62265 4 hours ago
    
and how did you request input – user62265 4 hours ago
    
@JohnCena 05AB1E uses implicit input and output, so at the end, it will automatically print. At the start, there is nothing on the stack, so it asks for input. – Oliver 4 hours ago
1  
The Output does not really match the desired output. It is no matrix and the numbers don't match. – Karl Napf 4 hours ago
1  
Well, it is a matrix, but the numbers are not correct (or tryitonline.net computes wrong) – Karl Napf 4 hours ago

Jelly, 11 bytes

WẋLŒDUṙLFsL

Try it online!

Explanation

               Input: array z
WẋL            length(z) copies of z
   ŒD          Diagonals (starting with main diagonal)
     U         Reverse each
      ṙL       Rotate left length(z) places
               (now the top-left diagonal is in front)
        F      Flatten
         sL    Split into chunks of size length(z)
share|improve this answer
    
Hmm when I tried it with L it did weird stuff, hence I used the register :/ I just tried it again and it works... basically the same so I guess I shall just remove mine. – Jonathan Allan 3 hours ago
1  
Of course Jelly has "diagonals" built in.... :) – Greg Martin 2 hours ago

Python 2, 105 96 bytes

-1 and -4 and -4 bytes thanks to Flp.Tkc

a=input()
n=len(a)
L,M=[],[]
for i in range(n):L+=a[i::-1];M+=a[:i:-1]
print zip(*[iter(L+M)]*n)

The for loop adds the items like in the description, the real magic happens in the zip which is from here

share|improve this answer
    
sorry for the spam, but now that R is only used once, you can just put it there directly :P – Flp.Tkc 3 hours ago
    
@Flp.Tkc no problem, i am happy :) – Karl Napf 3 hours ago

JavaScript (ES6) 101 105

a=>(u=>{for(r=[],i=l=a.length;i+l;i--)for(j=l;j--;v&&((u%=l)||r.push(s=[]),s[u++]=v))v=a[j-i]})(0)||r

Less golfed

a => {
  u = 0
  for(r=[], i=l=a.length; i+l>0; i--)
    for(j=l; j--; )
    {
      v = a[j-i]
      if (v) 
      {
        u %= l
        if (u==0) r.push(s=[])
        s[u++] = v
      }
    }
  return r
}

Test

F=
a=>(u=>{for(r=[],i=l=a.length;i+l;i--)for(j=l;j--;v&&((u%=l)||r.push(s=[]),s[u++]=v))v=a[j-i]})(0)||r

function update() {
  var a=I.value.match(/\d+/g)
  if (a) {
    var r=F(a)
    O.textContent = r.join`\n`
  }
}

update()
<input id=I value='1 2 3 4 5' oninput='update()'>
<pre id=O></pre>

share|improve this answer
1  
Wow, that's a very clever way to avoid the return. You should post a tip about that in the ES6 tip thread. – ETHproductions 3 hours ago

MATL, 17 bytes

!Gg*tRwZRhPXzGne!

Try it online!

How it works

The following explanation uses input [1 2 3 4 5] as an example. To visualize the intermediate results, insert % (comment symbol) after any statement in the code.

Note that ; is the row separator for matrices. So [1 2] is a row vector, [1; 2] is a column vector, and [1 0; 0 1] is the 2×2 identity matrix.

!     % Implicitly input a row vector. Transpose. Gives a column vector
      % STACK: [1; 2; 3; 4; 5]
Gg    % Push input with all (nonzero) values replaced by ones
      % STACK: [1; 2; 3; 4; 5], [1 1 1 1 1]
*     % Multiply, with broadcast. Gives a square matrix
      % STACK: [1 1 1 1 1;
                2 2 2 2 2;
                3 3 3 3 3;
                4 4 4 4 4;
                5 5 5 5 5]
tR    % Duplicate. Upper triangular part
      % STACK: [1 1 1 1 1;
                2 2 2 2 2;
                3 3 3 3 3;
                4 4 4 4 4;
                5 5 5 5 5],
               [1 1 1 1 1
                0 2 2 2 2;
                0 0 3 3 3;
                0 0 0 4 4;
                0 0 0 0 5]
wZR   % Swap. Lower triangular part, below main diagonal 
      % STACK: [1 1 1 1 1;
                0 2 2 2 2;
                0 0 3 3 3;
                0 0 0 4 4;
                0 0 0 0 5],
               [0 0 0 0 0;
                2 0 0 0 0;
                3 3 0 0 0;
                4 4 4 0 0;
                5 5 5 5 0]
h     % Concatenate horizontally
      % STACK: [1 1 1 1 1 0 0 0 0 0;
                0 2 2 2 2 2 0 0 0 0;
                0 0 3 3 3 3 3 0 0 0;
                0 0 0 4 4 4 4 4 0 0;
                0 0 0 0 5 5 5 5 5 0]
P     % Flip vertically
      % STACK: [0 0 0 0 5 5 5 5 5 0;
                0 0 0 4 4 4 4 4 0 0;
                0 0 3 3 3 3 3 0 0 0;
                0 2 2 2 2 2 0 0 0 0;
                1 1 1 1 1 0 0 0 0 0]
Xz    % Column vector of nonzeros, taken in column-major order
      % STACK: [1;2;1;3;2;1;4;3;2;1;5;4;3;2;1;5;4;3;2;5;4;3;5;4;5]
Gne   % Reshape into a matrix with as many rows as input size
      % STACK: [1 1 5 5 4;
                2 4 4 4 3;
                1 3 3 3 5;
                3 2 2 2 4;
                2 1 1 5 5]
 !    % Transpose. Implicitly display
      % STACK: [1 2 1 3 2;
                1 4 3 2 1;
                5 4 3 2 1;
                5 4 3 2 5;
                4 3 5 4 5]
share|improve this answer

JavaScript (ES6), 116 bytes

a=>a.map(_=>b.splice(0,a.length),b=[].concat(...a.map((_,i)=>a.slice(~i)),...a.map((_,i)=>a.slice(0,~i))).reverse())

Well, it's a start...

share|improve this answer
    
@ETHproductions No, I need to flatten both arrays. – Neil 49 mins ago

Mathematica 93 Bytes

Partition[Flatten[Table[If[i>n,#[[n;;(i-n+1);;-1]],#[[i;;1;;-1]]],{i,1,2(n=Length@#)-1}]],n]&

Here's how I'd ordinarily write this code (109 Bytes):

Partition[Reverse@Flatten[Table[Reverse@Diagonal[ConstantArray[Reverse@#,n],k],{k,-(n=Length@#)+1,n-1}]],n]&

This matrix plot gives a good idea from the structure due to a sequentially increasing input vector.

enter image description here

Here's the matrix plot with a random input vector. Obviously some structure still exists.

enter image description here

share|improve this answer

Mathematica, 92 bytes

n=NestList[#2,(r=Reverse)@#,(l=Length@#)-1]&;{Most@r[#~n~Rest],#~n~Most}~ArrayReshape~{l,l}&

Unnamed function taking a list as its argument. There might be other structures to such a function, but hopefully I golfed this structure pretty good....

The first part n=NestList[#2,(r=Reverse)@#,(l=Length@#)-1]& defines a function n of two arguments: the first is a list of length l, and the second is a function to apply to lists. n applies that function l-1 times to the reversed argument list, saving all the results in its output list. (Defining r and l along the way is just golfing.)

n is called twice on the original list, once with the function being Rest (drop the first element of the list) and once with the function being Most (drop the last element). This produces all the desired sublists, but the whole list is there twice (hence the extra Most) and the first half is there in backwards order (hence the r[...]). Finally, ~ArrayReshape~{l,l} forgets the current list structure and forces it to be an lxl array.

share|improve this answer

Mathematica, 85 bytes

Literally performing the steps suggested:

(l=Length@#;Partition[Flatten@Table[Reverse@Diagonal[Table[#,l],i],{i,-l+1,l-1}],l])&

My gut says that there should be a clever way to use Part to do this shorter, but every attempt I've made has been longer than 85 bytes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.