| Bytes | Lang | Time | Link |
|---|---|---|---|
| 062 | JavaScript Node.js | 240825T233310Z | l4m2 |
| 056 | Wolfram Language Mathematica | 201004T215558Z | att |
| 012 | Uiua | 231105T235145Z | Bubbler |
| 020 | Uiua 0.1.0 | 231105T091339Z | Pseudo N |
| 154 | Scala | 231104T053507Z | 138 Aspe |
| 072 | Ruby | 231104T051943Z | Value In |
| 022 | K ngn/k | 231104T033845Z | doug |
| 014 | J | 231103T155155Z | Jonah |
| 6875 | Vyxal | 231103T144141Z | pacman25 |
| 110 | Ruby | 161126T023521Z | Cary Swo |
| 008 | Husk | 201004T050053Z | Razetime |
| 084 | R | 161124T104849Z | Billywob |
| 105 | JavaScript ES6 100 101 | 161123T211859Z | edc65 |
| 085 | Mathematica | 161123T235939Z | user6198 |
| 092 | Mathematica | 161123T230549Z | Greg Mar |
| 093 | Mathematica 93 Bytes | 161123T224837Z | Kelly Lo |
| 096 | Python 2 | 161123T210918Z | Karl Nap |
| 011 | Jelly | 161123T213011Z | lynn |
| 017 | MATL | 161123T204408Z | Luis Men |
| 116 | JavaScript ES6 | 161123T205556Z | Neil |
| 013 | 05AB1E | 161123T202554Z | Oliver N |
JavaScript (Node.js), 62 bytes
x=>x.map(_=>x.map(g=_=>x[i=i?i-1:j++]*x[j+~i]?x[i]:g()),i=j=1)
Wolfram Language (Mathematica), 60 58 56 bytes
ListConvolve[#,Less/@#,All,,Pick,##&]~Partition~Tr[1^#]&
ListConvolve[ , , ,, , ] convolve
# list length of input
# with input as kernel
All with maximum overhang
Less/@ Pick by taking from kernel where overlap
##& flattened
~Partition~Tr[1^#] reshape
All is not documented as a third argument to ListConvolve. It behaves like {1,-1}.
Uiua, 12 bytes
⊏⍜♭⍏⊞+.⊃⇡▽⧻.
⊏⍜♭⍏⊞+.⊃⇡▽⧻. input: vector V of length N
⧻. keep V and push N
⊃⇡▽ consume both and push [0..N-1], V cycled N times
⊞+. self outer product of range by addition
⍜♭⍏ flatten, replace the array with its sorting order, and
put it back into the same sized matrix
⊏ for each number in the matrix, select the item at that index
in "V cycled N times"
The above is a slightly golfuscated version of the code below:
Uiua, 14 bytes
⍜∩♭'⊏⍏⊞+.⍏.↯⧻.
⍜∩♭'⊏⍏⊞+.⍏.↯⧻. input: a vector of integers of length N
↯⧻. make the input a matrix by copying the input N times -> X
⍏ rise; sorting order of the rows (same as range N)
⊞+. self outer product by addition -> Y
⍜∩♭ . operate on X and Y flattened and assemble back..
'⊏⍏ reorder elements of X by sorted order of elements of Y
Uiua (0.1.0), 20 bytes
↯~√⧻./⊐⊂≐(□↘)-⇡×2.⧻.
Explanation
⧻. # (nondestructively) get array length
-⇡×2. # construct a range from len down to -len + 1
≐(□↘) # for each element of the range, drop that many from the array
/⊐⊂ # join all resulting arrays together
√⧻. # get square root of length, suppose we call it N...
↯~ # ...then this reshapes the array to MxN, where M is inferred
Scala, 154 bytes
Golfed version. Try it online!
a=>{val n=a.size;var L,M=List[Int]();for(i<-0 to n-1){L=L:::a.slice(0,i+1).reverse.toList;M=M:::a.slice(i+1,n).reverse.toList};(L::: M).grouped(n).toList}
Ungolfed version. Try it online!
object Main {
def main(args: Array[String]): Unit = {
val a = Array(1, 2, 3, 4, 5)
val n = a.length
var L, M = List.empty[Int]
for (i <- 0 until n) {
L = L ::: a.slice(0, i+1).reverse.toList
M = M ::: a.slice(i+1, n).reverse.toList
}
val zipped = (L ::: M).grouped(n).toList
println(zipped)
}
}
Ruby, 72 bytes
->a,*r{t=[]
a.map{r+=t=[_1]+t}
r+=t while t.pop
[*r.each_slice(a.size)]}
J, 14 bytes
#($$&;</.)@#,:
#...#,:Duplicate the input n times, where n is the input length. We require,:to make the duplication work at the "row level" rather than the element level.</.Box every diagonal of the resulting n by n matrix. J's/.does the heavy lifting here.$$&;Unbox that result;into a single list and reshape it$to the shape of duplicated input$.
Vyxal, 55 bitsv2, 6.875 bytes
LẋÞ`f?Lẇ
Bitstring:
0110000101111000101000101100100011000011001101100100000
LẋÞ`f?Lẇ
# implicit input
Lẋ # repeated [length] times
Þ` # antidiagonals
f?Lẇ # flatten and wrap in chunks the size of the length
💎
Created with the help of Luminespire.
Ruby, 110 bytes
n=a.size
b=[*(0...n)]
b.product(b).group_by{|i,j|i+j}.flat_map{|_,f|f.sort.map{|i,j|a[i][j]}}.each_slice(n).to_a
#=> [[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]]
The sort operation may not be required, but the doc for Enumerable#group_by does not guarantee the ordering of values in the hash values (which are arrays), but current versions of Ruby provide the ordering one would expect and the ordering I would need if sort were removed from my code.
The steps are as follows.
n=a.size
#=> 5
b=[*(0...n)]
#=> [0, 1, 2, 3, 4]
c = b.product(b)
#=> [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 1], [1, 2], [1, 3],
# [1, 4], [2, 0], [2, 1], [2, 2], [2, 3], [2, 4], [3, 0], [3, 1], [3, 2],
# [3, 3], [3, 4], [4, 0], [4, 1], [4, 2], [4, 3], [4, 4]]
d=c.group_by{|i,j|i+j}
#=> {0=>[[0, 0]],
# 1=>[[0, 1], [1, 0]],
# 2=>[[0, 2], [1, 1], [2, 0]],
# 3=>[[0, 3], [1, 2], [2, 1], [3, 0]],
# 4=>[[0, 4], [1, 3], [2, 2], [3, 1], [4, 0]],
# 5=>[[1, 4], [2, 3], [3, 2], [4, 1]],
# 6=>[[2, 4], [3, 3], [4, 2]],
# 7=>[[3, 4], [4, 3]],
# 8=>[[4, 4]]}
e=d.flat_map{|_,f|f.sort.map{|i,j|a[i][j]}}
#=> [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]
f=e.each_slice(n)
#=> #<Enumerator: [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]:each_slice(5)>
Lastly, f.to_a returns the array shown earlier.
Husk, 9 8 bytes
CL¹Σ∂RL¹
-1 byte from Zgarb.
Explanation
CL¹Σ∂*L¹;
¹ input(appends a ¹ to the end)
; enclosed
*L repeated length times
∂ antidiagonals
Σ flattened into a single list
C cut into pieces of size
L¹ of the length of the input
R, 84 bytes
t(matrix(unlist(split(m<-t(matrix(rev(x<-scan()),l<-sum(1|x),l)),row(m)-col(m))),l))
Reads input from stdin and outputs/returns an R-matrix.
reversed_x <- rev(x<-scan()) # Read input from stdin and reverse
m <- t(matrix(reversed_x,l<-sum(1|x),l)) # Repeat and fit into matrix
diag_list <- split(m,row(m)-col(m)) # Split into ragged list of diagonals
t(matrix(unlist(diag_list),l)) # Flatten and transform back to matrix
Explained
The most interesting aspect about this answer is how the diagonals are retrieved. In general an object can be split up using the split function if supplied an object containing factors upon which the object is split into. To create these factors we can use col and row which return a matrix containing the column and row indices respectively. By taking the differences: row(m)-col(m) we get a matrix like:
[,1] [,2] [,3] [,4] [,5]
[1,] 0 -1 -2 -3 -4
[2,] 1 0 -1 -2 -3
[3,] 2 1 0 -1 -2
[4,] 3 2 1 0 -1
[5,] 4 3 2 1 0
in which each diagonal is uniquely identified. We can now split based on this matrix and turn it into a ragged list by applying split:
$`-4`
[1] 1
$`-3`
[1] 2 1
$`-2`
[1] 3 2 1
$`-1`
[1] 4 3 2 1
$`0`
[1] 5 4 3 2 1
$`1`
[1] 5 4 3 2
$`2`
[1] 5 4 3
$`3`
[1] 5 4
$`4`
[1] 5
(Note how the name of each vector correspond to the diagonal values in the matrix above).
The last step is just to flatten and turn it into a matrix of the form:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 1 3 2
[2,] 1 4 3 2 1
[3,] 5 4 3 2 1
[4,] 5 4 3 2 5
[5,] 4 3 5 4 5
JavaScript (ES6) 100 101 105
a=>eval("for(u=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];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=>eval("for(u=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];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>
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.
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.
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.
Here's the matrix plot with a random input vector. Obviously some structure still exists.
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
Jelly, 11 bytes
WẋLŒDUṙLFsL
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)
MATL, 17 bytes
!Gg*tRwZRhPXzGne!
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]
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...
05AB1E, 13 bytes
.p¹.sR¦«í˜¹gä
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

