New answers tagged

2

Jelly, 21 bytes æịþḶFµḢ;ⱮṖŒ!;€ṪƲIỊẠ€S Try it online! Takes forever for n>3 because it tries to generate all permutations of n**2-2 items. æịþḶFµḢ;ⱮṖŒ!;€ṪƲIỊẠ€S Monadic link. Input: n æịþḶF Generate complex numbers (1..n)+(0..n-1)i µḢ;ⱮṖŒ!;€ṪƲ Generate all permutations with 1st and last fixed: Ḣ;Ɱ ...


1

Python 3, 103 bytes r=range f=lambda A:[*zip(*[[max(i*(c[j-i]==i)for i in r(len(c)))for j in r(len(c))]for c in zip(*A)])] Try it online! This is more or less a one-to-one translation of my numpy answer. The same explanation and caveat apply.


4

Python 3+numpy, 73 bytes import numpy def f(A):t=numpy.c_[:len(A)];return(t*(A[t-t.T]==t)).max(1) Try it online! Note: this probably fails for double wrap around EDIT or, more generally, for matrix entries equal to or larger than the matrix height thanks @alephalpha for pointing this out end EDIT. I'm just not sure it is a requirement (this is my first ...


1

Wolfram Language (Mathematica), 63 bytes MapThread[Max,RotateRight[#/.x_/;x!=i:>0,i]~Table~{i,Max@#},2]& Try it online!


3

Python 3+numpy, 77 bytes import numpy b=0*a for i in range(1,a.max()+1):b[numpy.roll(a==i,i,axis=0)]=i Try it online! the roll function does it all, and i stole the zeros_like trick from @ovs


1

Pari/GP, 50 bytes m->polcoef(vecprod([x^2^i|i<-[0..#m-1]]*m),2^#m-1) Based on @miles's Mathematica answer. According to Wolfram MatheWorld, the permanent of a matrix \$A = (a_{ij})\$ is the coefficient of \$x_1 \cdots x_n\$ in the polynomial \$\prod_{i=1}^{n}\sum_{j=1}^na_{ij}x_j\$. This is exactly what miles's answer does. But in PARI/GP there isn't ...


1

jq, 28 bytes [transpose[]|sort]|transpose Try it online!


1

Charcoal, 23 bytes IEθEι⌈Eθ∧¬﹪⁻⁺§νμξκLθ§νμ Try it online! Link is to verbose version of code. Explanation: θ Input array E Map over rows ι Current row E Map over cells θ Input array E Map over rows §νμ Inner ...


1

Charcoal, 35 20 bytes WS⊞υι↑Eθ⭆²Φ⭆υ§νκ⁻Iνλ Try it online! Link is to verbose version of code. Takes input as a list of digit strings. Explanation: WS⊞υι Read the input. ↑Eθ⭆²Φ⭆υ§νκ⁻Iνλ Map over each column, taking the transpose and filtering out the 0s and 1s each time, so that the 1s are effectively sorted to the front, but then print the whole lot ...


1

Retina 0.8.2, 39 bytes +`(?<=(.)*)1(.*¶(?<-1>.)*(?(1)^))0 0$+1 Try it online! Takes input as a list of digit strings although commas can be redundantly included as per the example link. Explanation: .NET's balancing groups are used to ensure that the regular expression matches a 1 directly above a 0; the digits are thus exchanged. $+ is used ...


1

Nim, 138 bytes proc x(i:int)= for x in 1..i: for j in 1..x: stdout.write($x&" ") for j in x+1..i: stdout.write($j&" ") echo()


2

TI-Basic, 38 bytes Input N identity(N→[A] For(I,1,N For(J,1,N max(I,J→[A](I,J End End Output is stored as a matrix in [A].


1

Swift 5.5/Xcode 13.0, 215 bytes var c=[Int:Int]();let d=m.count;let e=m[0].count;for i in 0..<e{for j in 0..<d{c[i,default:0]+=m[j][i]}};var r=Array(repeating:Array(repeating:1,count:e),count:d);for k in c{for i in 0..<d-k.1{r[i][k.0]=0}};return r Try it online!


2

05AB1E, 19 18 14 bytes ˜εQy*yFÁ]øεø€à -5 bytes thanks to @ovs. Try it online or verify all test cases. Explanation: Z # Get the flattened maximum of the (implicit) input-matrix L # Pop and push a list in the range [1,max] ˜ # (`ZL` is now `˜`: flatten the (implicit) input-matrix) ε # Map over each: Q # Check ...


3

Core Maude, 248 244 236 bytes load linear mod D is inc MATRIX{Nat0}*(sort Matrix{Nat0}to M). var A B C D : Nat . op d : Nat M -> M . eq(A,B)|-> C ;(A,B)|-> D =(A,B)|-> max(C,D). eq d(A,(B,C)|-> D ; X:M)=(B,(C + D)rem A)|-> D ; d(A,X:M). eq d(A,X:M)= X:M[owise]. endm Example Session \||||||||||||||||||/ --- Welcome ...


2

Ruby, 155 bytes ->m{r,c,o=m.size,m[0].size,{} o.default=0 e="r.times{|y|c.times{|x|" eval e+"v=m[y][x] v>o[[(y+v)%r,x]]&&o[[(y+v)%r,x]]=v}}" eval e+"m[y][x]=o[[y,x]]}}" m} Try it online! We build an hash object with 2d-index as key. Then we iterate input and we set the key corresponding to landing position to the ...


5

Python 3 + numpy, 91 bytes def f(a):r,c=a.nonzero();v=a[r,c];r+=v;i=v.argsort();a*=0;a[r[i]%len(a),c[i]]=v[i];return a -23 due to @ovs Try it online! Much Different from the first solution. Takes a numpy array (slightly outdated explanation) Assume the array [[0 0 2 0 1] [0 2 1 1 0] [3 0 2 1 0] [0 0 0 0 0]] r,c=nonzero(a) First save the x-indices (rows)...


4

R, 82 bytes Or R>=4.1, 75 bytes by replacing the word function with \. function(m,k=nrow(m),t=0*m){t[((row(m)+m-1)%%k+1+col(m)*k-k)[i]]=m[i<-order(m)];t} Try it online! Inspired by @wasif's answer. Old solution (shorter for R>=4.1): R, 94 92 89 88 bytes Or R>=4.1, 74 bytes by replacing two function appearances with \s. function(m,k=nrow(m))...


1

Python 2, 157 bytes e=enumerate;l=input();j=[[0]*len(l[0])for _ in l] for x,y,z in sorted([((n+x)%len(l),m,x)for n,y in e(l)for m,x in e(y)],key=lambda e:e[2]):j[x][y]=z print j Try it online! -7 thanks to @ykcul


1

RAD, 4 bytes ⍉<⍉⍵ Try it online! Transpose the input (⍉⍵), sort each (<), then retranspose ⍉.


1

MY, 11 bytes ⎕⍉86ǵ'ƒ⇹(⍉← Try it online! ⎕⍉86ǵ'ƒ⇹(⍉← ⎕ - Input ⍉ - Transpose 86ǵ'ƒ⇹( - Sort each ⍉ - Transpose again ← - Output


7

J, 24 20 bytes [:>./-@,|."0 2,*,=/] Try it online! -4 thanks to xash! Solved independently, but seems very similar to ovs's APL solution. This one works for any number, because I didn't notice that the question only required handling 1-9. The following explanation is slightly out of date, but the high-level concept, which is the interesting part, ...


7

APL(Dyalog Unicode), 17 16 14 bytes SBCS -1 byte by looking at Jonah's J answer and another -2 bytes by implementing xash's suggestion on the same answer. ⌈/⊂(-⍤⊢⊖⊣×=)¨, Try it on APLgolf! I initially implemented a lot more "by hand", which ended up more than 3 times longer: {{(⊃⍺)@(⊂1↓⍺)⊢⍵}/((↓⊂∘⍒⌷⊢){k,m|(k←≢⍵)0+⊃⍺}⌸⍸⍵),⊂0⍴⍨m←⍴⍵} Try it on ...


5

JavaScript (ES6), 82 bytes m=>m.map((r,y)=>r.map((_,x)=>m.map(q=(r,Y)=>q=(Y+(v=r[x]))%m.length-y|v<q?q:v)|q)) Try it online! How? For each position \$(x,y)\$ in the input matrix \$M\$ of height \$h\$, we look for all values \$M_{x,Y}\$ on the same column such that: $$Y+M_{x,Y}\equiv y\pmod h$$ and keep the highest one.


6

Jelly,  16  12 bytes I thought 16 seemed a bit much! Zµ=Ɱ×ṙ"N»/)Z A monadic Link that accepts a list of lists of integers and yields a list of lists of integers. Try it online! How? Zµ=Ɱ×ṙ"N»/)Z - Link: matrix, M Z - transpose -> columns of M (top to bottom) µ ) - for each (c in columns of M): Ɱ - map (across v ...


1

TI-Basic, 54 bytes Input [A] dim([A] For(I,1,Ans(2 Matr►list([A],I,A SortA(ʟA For(J,1,dim(ʟA ʟA(J→[A](J,I End End Output is stored in [A], which was the inputted matrix.


1

Pari/GP, 15 bytes a->vecsort(a~)~ Try it online!


4

Python 3 + NumPy, 18 bytes lambda a:a.sort(0) Try it online! Input a np.array, output by modify it in-place.


1

JavaScript, 142 Bytes for(j=0,l=a.length,w=a[0].length,c=new Array(l),e=1;j<w;j++){for(i=0;i<l;i++){if(a[i][j]){p=l-e++;!c[p]&&(c[p]=new Array(w));c[p][j]=1}}e=1;} Where a is the input array and c is the output array. Might be cheating since I am pulling a vacuum on all the 0s rather then applying gravity to all the 1s. It works similar to the ...


Top 50 recent answers are included